There are two ways I usually handle this problem of scoping within subs:
1) You can use the return() function to pass the value of the variable back
to a variable within the main scope.
Example:
my $mainscope = SubScope();
sub SubScope{
my $localscope = 15;
return $localscope;
}
2) You can pass a reference to the variable to your subroutine. This tends
to look hairier than it really is once you get used to it. Just remember
that each argument to a sub is put into the @_ array. This way you are
actually only passing the reference, so that if you have a very large
variable, array, hash, etc, you don't end up having to create two copies of
it.
Example:
my @bigArray = ();
PopulateArray(\@bigArray); #pass the reference
sub PopulateArray{
open(INFILE,"myfile.txt");
@{$_[0]} = <INFILE>; #dereference the first argument
}
Either way the way I usually "solve" this is usually to tie (not tie()) the
variable to the main scope somehow. If I don't want to use the variable for
very long, I can declare it within a block in the main scope so that it
passes out of scope after the sub or subs have been called.
-----Original Message-----
From: Bob Showalter
To: 'Johnson, Shaunn'; '[EMAIL PROTECTED]'
Sent: 5/2/02 8:06 AM
Subject: RE: passing local value between sub functions
> -----Original Message-----
> From: Johnson, Shaunn [mailto:[EMAIL PROTECTED]]
> Sent: Thursday, May 02, 2002 10:37 AM
> To: '[EMAIL PROTECTED]'
> Subject: passing local value between sub functions
>
>
> Howdy:
>
> Have a question about trying to pass the result
> of a sub function to another sub function.
>
> Let's say I have some code like this:
>
> [snip]
>
> sub firstPart {
> while (<>) {
> print "Stuff here is: $_\n";
> }
>
> [/snip]
>
> I'd like to know how can I make $_ available
> for OTHER sub functions if not the entire program.
> I'm trying to get snips of code so I can show
> you what I'm working with (apologies since it
> isn't my code); I was under the impression that
> I could use my() and pass that info OUTSIDE
> the sub function.
$_ is one of Perl's "special" variables and is always global.
So, if you have:
sub foo
{
$_ = "Hello, world\n";
bar();
}
sub bar
{
print $_;
}
foo(); # call sub foo
Then when foo() is called, sub bar will print "Hello, world\n",
since $_ is a global variable.
Consider this:
sub foo
{
$hello = "Hello, world\n";
bar();
}
sub bar
{
print $hello;
}
foo(); # call sub foo
The same situation holds. $hello is a global variable, because by
default, all variables in Perl are global. Using "my" makes a
local (lexical) variable:
sub foo
{
my $hello = "Hello, world\n"; # $hello now lexical var
bar();
}
sub bar
{
print $hello; # prints nothing, why?
}
foo(); # call sub foo
The addition of "my" turns $hello into a "private" or lexcial
variable. It now is limited to a "scope" consisting of the point
at which it is defined until the end of the innermost enclosing
block { } or file. In this case, $hello can only be accessed by
lines of code that lie between the "my" line and the closing brace
(}) two lines later. When we try to print $hello in sub bar,
nothing prints, because we are actually using the "global" $hello.
When we are outside of the scope of the "private" $hello, we
default to using the global $hello.
This is usually a problem that should be brought to our attention,
so perl has a "use strict" pragma that will raise an error if we
try to access a global variable (such as $hello inside sub bar)
without previously delaring our intention to do so. ($_ and the
other "special" globals are exempted from this, however.)
There's *lots* more to be said about this in:
perldoc perlsub
perldoc perlvar (about the "special" variables)
perldoc strict
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]
--
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]