Local Scope Variables



  • 1. Crypt Function
    Hi everyone i'm using crypt function to verify an input password with password stored in /etc/shadow. I'm on Red Hat 9 but crypt doesn't function, in other words it return a different crypted password. Why?
  • 2. I'm stuck!
    I'm trying to use soupermail.pl and am having a problem. The script consistently fails on this line: if ($filename =~ /[^\w\.\-\/:]/){ failSecurity("Filename $filename contains a .. " . " or other illegal characters"); cleanScratch(); exit; } The filename is: C:/Program Files/Apache Group/Apache2/public/test1 What in the heck am I doing wrong? Any help would be greatly appreciated. Also, can someone point me to a useful beginners guide to perl? Thanks! James
  • 3. Package require question again
    I want to thank everyone who has helped me with this. and I'm trying to remember to bottom post ok I have the program working again. but how do I get a var from the package? At the time you are reading this I am still experimenting wih the following: the package has this line in it. $Options="Buy Sell Talk Sleep Work Move"; and the cgi calls the var in the package like this my $V = $main::{$Player{Location} . "::"}{Options}; I am also experiementing with the $glob command Help? Lou

Local Scope Variables

Postby josh » Sat, 14 Feb 2004 08:19:50 GMT


I'm having a bit of trouble with using some local scope variables. I 
have a function where I want to define a variable, then use that 
variable in a sub function. That sounds like the text book definition 
for when to use a local scope variable. Below is the test script that 
I'm using.

I'm using the 'use strict' declaration as all the books I've read 
(O'Reilly's) say that's a Good Thing to do.

Can someone offer some insight as to why when I declare $a it's ok, but 
declaring $aa give me an error?

I'm using Active State's Perl v5.8.3 build 809 for Windows.

Thanks for any help
- Josh

use strict;


sub test1
    local $a = 1;
    local $aa = 2;

sub test2
    print "a = $a\n";
    print "aa = $aa\n";

# Global symbol "$aa" requires explicit package name at 
C:\projects\Tracing\test.pl line 8.
# Global symbol "$aa" requires explicit package name at 
C:\projects\Tracing\test.pl line 15.
# Execution of C:\projects\Tracing\test.pl aborted due to compilation 

Re: Local Scope Variables

Postby eternius » Sat, 14 Feb 2004 19:10:24 GMT


use my
extract from perldoc -f my :
  A "my" declares the listed variables to be local
                (lexically) to the enclosing block, file, or


Re: Local Scope Variables

Postby RandyS » Sat, 14 Feb 2004 20:08:11 GMT

'local' does not create a variable; the variable must already exist. In 
order to create a package variable you must use 'our' or 'use vars':

our $a;
our $aa;


our ($a, $aa);


use vars qw($a $aa);

The above are all equivelant, and they all create to package variables 
named $a and $aa;

The reason you get no error for $a is because there is already a package 
variable with that name: the variables $a and $b exist automatically in 
every package for use in the 'sort' function.


Re: Local Scope Variables

Postby lists » Mon, 16 Feb 2004 02:08:06 GMT

Elegance in programming should be defined in terms of readability, at least partly. Very dense ways of coding a certain goal are smart (and I admit that I like to compress my code as much as possible), but not necessarily very elegant. ;)

- Jan
There's no place like ~/

Similar Threads:

1.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 = logger.new  #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?


2.local variables and global variables

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.


3.scope of the variable?

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

In test.pm 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 test2.pm 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 test.pm within test2.pm module?


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. Help with Variable Scope, Regular Expressions

Return to PERL


Who is online

Users browsing this forum: No registered users and 23 guest