Karyn Stump wrote:
I am trying to learn subroutines. I have seen refernces to using my and
local for passing variables
You seem to be dealing with named variables within subroutines, not
passing them.
<snip>
Global symbol "$name" requires explicit package name at ./user-sub.pl line
143.
<snip>
sub printsub
{
local($name) = $_[0];
print "This is the value of $name in the subroutine.\n";
}
local() creates a local copy of a global variable. Hence, under
strictures, a package global needs to be declared using our() or use
vars (if you don't refer to it using the fully qualified name).
For an attempt to explain what local() is about, please consider this code:
C:\home>type test.pl
use strict;
use warnings;
our $motto = 'SMSOWSDI';
print change_motto($motto), "\n";
print "$motto\n";
sub change_motto {
local $motto = shift;
$motto =~ tr/S/T/;
return $motto;
}
C:\home>test.pl
TMTOWTDI
SMSOWSDI
C:\home>
As you can see, the sub returns an altered version of $motto, while
$motto retains the old value outside the sub. This is one situation when
local() may be motivated.
I would appreciate some help understanding if or how local can be used in
this context.
Normally you should stick with my(), especially since you seem to be a
beginner.
Suggested reading: http://perl.plover.com/FAQs/Namespaces.html
--
Gunnar Hjalmarsson
Email: http://www.gunnar.cc/cgi-bin/contact.pl
--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/