CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
lexical scoping when passing a variable from one subroutine to another

 



kencl
User

Jan 19, 2001, 10:37 PM

Post #1 of 3 (156 views)
lexical scoping when passing a variable from one subroutine to another Can't Post

I want to pass a variable from subroutine A to subroutine B by reference. Should I scope it as my, local or not at all?


Code
sub A { 
local $scalar = "A long string, trust me :)";
# I'm thinking that local variables are accessable within this subroutine
# and subroutines called by this subroutine, but not outside of here.
&B(\$scalar);
# this is what I want to do, treat sub B like a function,
# in which case $scalar within this subroutine was modified by sub B,
# so I don't need to receive anything from sub B right?
# or would it be better to receive something back with $scalar = &B(\$scalar);
}

sub B {
my $scalar = shift; chomp($scalar); # should be fine since I'm using my
modify $$scalar with some complex procedure;
# since I modified the value directly, I shouldn't have to return it yes/no?
# or I could return a reference to is with return $$scalar
# but that just creates a second pointer to the same variable
# or I could return it by value with return $scalar which is what I'm trying to
# avoid doing in the interest of conserving system resourses
}

I'm sure this question would be simpler if I understood scoping better.

Thanks in advance, and happy Perling :)



Jasmine
Administrator / Moderator

Jan 20, 2001, 2:39 AM

Post #2 of 3 (154 views)
Re: lexical scoping when passing a variable from one subroutine to another [In reply to] Can't Post

Okay, I'm going to try my hand at this, but please note that I'm really tired :)

The local "pronoun" means that the variable will be "in scope" from the point of its declaration to the end of the subroutine that declared it. This means that if you declare $scalar in subroutine a, and invoke subroutine b, that $scalar will be available in subroutine b, whether or not you choose to pass $scalar explicitly into subroutine b.

my means that the variable is only known to its encapsulating block { }.

local means that the variable is known from the point of its declaration to the end of the current block and all subroutines invoked in the current block.

If you think that at some point you may want to use strict, don't use the symbolic reference $$variable method -- use the hard reference \$variable method. use strict freaks out (aka dies) with symbolic references.





japhy
Enthusiast

Jan 20, 2001, 7:31 AM

Post #3 of 3 (152 views)
Re: lexical scoping when passing a variable from one subroutine to another [In reply to] Can't Post

Scoping is not an issue when you're dealing with passing variables to functions. Scoping is an issue when you aren't passing variables to functions. No matter what the scope of a variable, when you pass it to a function, you're safe.


Code
sub foo { 
my $x = "jeff";
local $y = "japhy";
$z = "other";
bar(\$x);
bar(\$y);
bar(\$z);
print "$x; $y; $z\n"; # prints "Jeff; Japhy; Other"
}

sub bar {
my $var = shift;
$$var = ucfirst lc $$var;
}

Jeff "japhy" Pinyan -- accomplished hacker, teacher, lecturer, and author

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives