Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

what is this ? 2

Status
Not open for further replies.

theEclipse

Programmer
Dec 27, 1999
1,190
US
hi, I am learning perl for cgi, and was wondering what this line means:

local($name, $value) = split(/=/, $pair);

thanks for your input Robert Carpenter
questions? comments? thanks? email me!
eclipse_web@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
actually nevermind. instead, what does the local() function do? Robert Carpenter
questions? comments? thanks? email me!
eclipse_web@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
local is the same as my, which "defines" the scope of the variable. that means that the variables are only useable in that block of code.

//Daniel
 
ok, I think I understand:


#this would work
sub my_sub{
local($foo)='foo';
$bar='bar';
print "\$foo = $foo";
}
print "\$bar = $bar";
#but this would not
print "\$foo = $foo";


correct? (written as if it were one page of code) Robert Carpenter
questions? comments? thanks? email me!
eclipse_web@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
thx
Robert Carpenter
questions? comments? thanks? email me!
eclipse_web@hotmail.com
Icq: 124408594
online.dll

AIM & MSN: robacarp
 
Actually, local is NOT the same as my. They do have some similarities, but they are not the same. I don't recall the differences, and don't have the time to look them up now. Generally you should be using my instead of local. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
From the [tt]perlsub[/tt] manual pages:
Code:
     Private Variables via my()

     Synopsis:

         my $foo;            # declare $foo lexically local
         my (@wid, %get);    # declare list of variables local
         my $foo = "flurp";  # declare $foo lexical, and init it
         my @oof = @bar;     # declare @oof lexical, and init it

     A "my" declares the listed variables to be confined
     (lexically) to the enclosing block, conditional
     (if/unless/elsif/else), loop
     (for/foreach/while/until/continue), subroutine, eval, or
     do/require/use'd file.  If more than one value is listed,
     the list must be placed in parentheses.  All listed elements
     must be legal lvalues.  Only alphanumeric identifiers may be
     lexically scoped--magical builtins like $/ must currently be
     localized with "local" instead.

     Unlike dynamic variables created by the "local" statement,
     lexical variables declared with "my" are totally hidden from
     the outside world, including any called subroutines (even if
     it's the same subroutine called from itself or elsewhere--
     every call gets its own copy).

====================

     Temporary Values via local()

     NOTE: In general, you should be using "my" instead of
     "local", because it's faster and safer.  Exceptions to this
     include the global punctuation variables, filehandles and
     formats, and direct manipulation of the Perl symbol table
     itself.  Format variables often use "local" though, as do
     other variables whose current value must be visible to
     called subroutines.

     Synopsis:

         local $foo;                 # declare $foo dynamically local
         local (@wid, %get);         # declare list of variables local
         local $foo = "flurp";       # declare $foo dynamic, and init it
         local @oof = @bar;          # declare @oof dynamic, and init it

         local *FH;                  # localize $FH, @FH, %FH, &FH  ...
         local *merlyn = *randal;    # now $merlyn is really $randal, plus
                                     #     @merlyn is really @randal, etc
         local *merlyn = 'randal';   # SAME THING: promote 'randal' to *randal
         local *merlyn = \$randal;   # just alias $merlyn, not @merlyn etc


     A local() modifies its listed variables to be local to the
     enclosing block, (or subroutine, eval{}, or do) and any
     called from within that block.  A local() just gives
     temporary values to global (meaning package) variables.
     This is known as dynamic scoping.  Lexical scoping is done
     with "my", which works more like C's auto declarations.
In short, use [tt]my[/tt], unless you are dealing with special variables, then use [tt]local[/tt]

Cheers, Neil
 
Thanks Neil! That's exactly the documentation I remembered reading about my and local, but I didn't have the time to go back and find where I'd read it. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top