Similar Threads:
1.newbie question about scope, variables, declarations of variables and option strict (as in perl)
Hello,
I am trying to understand the syntax error I receive in a code similar to
this.
1 require 'logger'
2
3 log = logger.new #some other logger settings are ignored.
4 def func
5 log.debug "a statement" # error is reported here when func is called
below
6 # some code
7 end
8
9 #some code continues
10 func
When func is called, an error is reported on line-5 saying that undefine
local variable log etc. I understand that functions create scopes and log is
seen as local variable which is not defined in that scope. As it is
qualified with no scope operator, interpreter thinks that it is local but
can not find definition of the log before it's usage but also in the
parameter list and I understand that. On the other hand, I can use log
without qualifying it with a scope symbol anywhere in the same file if it is
not in a function. I know that loops, if statements etc are built into the
language and do not create scope. Code blocks inherit the locals. So it is
meaningful that I can use it anywhere else. When I qualify log with $ as
$log, it becomes global and I no longer receive error. I have tried it
qualifying with @ etc. but the received the same error. What I am asking is,
what is scope of log?. What kind of variable is it? It is the local or
instance variable of what, Object? I know that func is private to the
Object. But what about log? How can I access it in a function without
making it global?
Is there a way to make variables local to a file as perl does with "my".
Is there a strict option that prevents unintended variable creation because
of typos. Is there a way force predeclaration of variables?
Thanks.
2.local variables and global variables
Hi,
How and what should be used to define a local variable and a global
variable as well.
and if i have used a sub-routine in a script and i want to use that
sub-routine in another script,
how can i do that.
regards
3.scope of the variable?
Hello All,
I have following question regarding accessing variable from other module:
In test.pm I have following:
#BEGIN OF THE TEST.PM
package test;
use strict;
use warnings;
#
# The object responsible for managing the database connections.
#
my $dbaccess = undef;
-somewhere else
$dbaccess = new xxxx::xxx::DBAccess( %dbURL);
….
….
#END OF THE TEST.PM
In test2.pm I have following:
#BEGIN OF THE TEST2.PM
package test2;
use strict;
use warnings;
….
# How to test::dbaccess ??
#END OF THE TEST2.PM
My question is how to access $dbaccess variable (object) defined and initialized in test.pm within test2.pm module?
Thanks,
Bogumil
4.Variable scope in wanted function
Greetings All -
I am having some difficulty with a module that is using File::Find. The
method is below.
The idea is to enter this method feeding it a file name and beginning
directory and then looking for all occasions of $file_name and push those
addresses into @a_files. This works fine until I need to use FindPath again
during the same session. What I'm finding is that while @a_files looses
scope within FindPath itself, it does not in ProcessFile. In other words,
when I exit FindPath and come back into it later, @a_files is an uninitiated
array. However when ProcessFile is called, @a_files has retained the values
it had from the last call to FindPath.
Am I making sense?
sub FindPath
{
#- Var Declaration And Initialization
my ($hr_self, $file_name, $file_path) = @_;
# Array to fill with file paths
my @a_files = ();
# Search file_path for the file
find(\&ProcessFile, $file_path);
#- The Subroutine To Process Files And Directories
sub ProcessFile
{if ($_ eq $file_name){push (@a_files, $File::Find::name);}}
# Return the paths found
return @a_files;
} # end FindPath
Peace -
Ron Goral
5.comparing and contrasing two approaches to variable scope
Assuming you had a script configuration variable that was used numerous
places in your script, further assume that you have "use strict;" in
your perl script.
You could declare the variable "my" and pass that variable to any
subroutines that needed it with @_.
Or you could declare that variable with "local" or some other "scope
defining declaration" so that it would be available automatically in any
and all subroutines called from the level you declared the var as "local"
My question concerns a little of form and a little of function
On the function front:
which approach is faster? With one you need a
my ($arg1, $arg2...) = @_;
inside each subroutine which should take some time to execute.
(I apologize if the syntax isn't correct for the above line, I don't
have my notes available and I'm questioning the use of the parenthesis,
but you should be able to get the idea, which is all I'm after)
On the form front, Ive noticed that the scripts are not as readable for
me, and by extension probably any other people who use my script.
What are your thoughts on choosing which approach to take? I'm sure
both are technically correct, but I'm sure there are places where one
approach should be preferred over the other.
--
Rance Hall
System Administrator
Nebraska Turkey Growers
1-308-468-5711, ext. 106
XXXX@XXXXX.COM
6. problem with variable scope
7. Variable scope
8. Help with Variable Scope, Regular Expressions