scope of the variable?



  • 1. how do I create a image
    I want to create a image containg a string I am trying to get GD installed on my machine ( linux redhat 7.2 ) but it gives loads of errors Is there a simple way I Can create images ( png jpg or gif ) Thanks Ram
  • 2. Oracle DBI
    Hi! I'm a starter with this subject. my script is: use strict; use DBI; my $dbh = DBI->connect( 'dbi:Oracle:orcl', 'blah', 'blah',)|| die "Database connection not made: $DBI::errst r"; $dbh->disconnect(); when trying to run this i get an error: DBI->connect(APPS1) failed: ORA-01017: invalid username/password; logon denied (DBD ERROR: OCISessionBegin) at line 4 But my user name/password are correct, i've checked that. Is it something to do with shell variables or stuff? Ant.

scope of the variable?

Postby bogus » Sat, 04 Feb 2006 01:50:14 GMT

Hello All,
I have following question regarding accessing variable from other module:

In I have following:

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);



In I have following:

package test2;

use strict;
use warnings;

#  How to test::dbaccess ??


My question is how to access $dbaccess variable (object) defined and initialized in within module?


Re: scope of the variable?

Postby pangj » Sat, 04 Feb 2006 19:11:29 GMT

I think the '$dbaccess' in your should be declared as perl's global var as 'our $dbaccess;'  or  'use vars qw($dbaccess);'. Then in your,you can access it as:

use vars qw($dbaccess);
print $test::dbaccess;

use vars qw($dbaccess);
*dbaccess = \$test::dbaccess;
print $dbaccess;  #the same as 'print $test::dbaccess;'

-----Original Message-----

Jeff Pang
NetEase AntiSpam Team

Re: scope of the variable?

Postby Paul Lalli » Sat, 04 Feb 2006 22:08:53 GMT

Three options:
1) Make $dbaccess a package variable instead of a lexical variable.
Change the
my $dbaccess;
our $dbaccess;
and then in, access the variable as: $test::dbaccess

2) Make $dbaccess a package variable instead of a lexical variable, as
above.  Then export the variable as described in
perldoc Exporter
and import the variable in

3) In, write a subroutine that can be called to test the
defined'ness of the variable:
sub get_dbaccess {
   return $dbaccess;
Then in, call that subroutine:
my $db = test::get_dbaccess();
die "\$dbaccess is not yet defined\n" unless defined $dbaccess

3b) You could also export this subroutine to avoid calling it with the
test:: qualifier.  Again, see 
perldoc Exporter

Paul Lalli

Re: scope of the variable?

Postby showaltb » Sat, 04 Feb 2006 23:03:18 GMT


If $dbaccess is delared with 'my' in, you cannot directly access 
it from another file. You have two basic options:

1. Provide an accessor function in that returns the object:

   sub dbaccess { $dbaccess }

2. Change the variable to a global (symbol table) variable. You can 
optionally use the Exporter module to allow the symbol to be exported to 
other namespaces:
   package Test;
   require Exporter;
   our @ISA = qw(Exporter);
   our @EXPORT_OK = qw($dbaccess);
   our $dbaccess;


   use Test qw($dbaccess);

   print $dbaccess->some_method;

Similar Threads:

1.[newbie] scope of the variables

Hi all,

I am trying to create a module. and I have a question about the scope
of the variables.

lets say,

use strict;

sub one {

    my ($rvalue) = @_;   # here I define a variable

    for my $i (0..$#$rvalue){   # here I define a local 
                                # variable $i and I use it

        # do something


sub second {

    my ($rvalue) = @_;   # on this second sub $rvalue 
                         # is a new one or I carry it
                         # as the first one

    for my $i (0..$#$rvalue){ # now the same case, $i was
                              # already defined and if the
                              # scope is just its
                              # own subsroutine, now this
                              # $i is something copletely
                              # new. Am I right?

      # do something else

 # Thanks for your help
 # John

2.tracking scope of a variable?

I'm presently having a "bit of an issue" with a variable,
which is declared, and exported in various interesting ways.

While I have no doubt I will eventually find the problem
and fix it, and even understand the fix, I would like to ask:

Is there a way to print out all the declarations
and references to a variable?

In particular I would like a routine to
dump all declarations of a named variable e.g. "flooble".

I would ideally like to see a list, including
all block scoped variable called "flooble", and
all module scoped variable (A::flooble, B::flooble).

Further, when I use a variable
($flooble = "test", or print "$flooble") I would
like to see a list of all the declarations
that this variable has (often multiple, due to exports).

My apologies for the vaguness and errors in this request,
but if I knew enough to write this up really
well, I probably wouldn't need to :-)


3.newbie question about scope, variables, declarations of variables and option strict (as in perl)


I am trying to understand the syntax error I receive in a code similar to

1 require 'logger'
3 log =  #some other logger settings are ignored.

4 def func
5   log.debug "a statement" # error is reported here when func is called
6  # some code
7 end
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?


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

6. problem with variable scope

7. Variable scope

8. Local Scope Variables

Return to PERL


Who is online

Users browsing this forum: No registered users and 58 guest