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.Question about Modules and Variable Scope
3.Question about variable scope
In the code snippet below, why can't I access the $nntp variable, unless I
instantiate it within the ListGroups() subroutine?
-Thanks
#!/usr/bin/perl -w
#This nntp instance cannot be used in ListGroups()
#sub below:
# $nntp = Net::NNTP->new($newshost);
# $nntp->authinfo($username,$password);
.
.
.
sub ListGroups()
{
#Need to instantiate $nntp here for this to work:
$nntp = Net::NNTP->new($newshost);
$nntp->authinfo($username,$password);
my $groups = $nntp->list() or die "Cannot get group list";
print join("\n", keys %$groups), "\n";
$nntp->quit();
return(0);
}
4.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
5.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
6. comparing and contrasing two approaches to variable scope
7. problem with variable scope
8. Variable scope