At 05:09 PM 2/23/09 -0800, Jim Gibson wrote:
>On 2/23/09 Mon  Feb 23, 2009  4:30 PM, "Karyn Stump" <ka...@calarts.edu>
>scribbled:
>
>> I am trying to learn subroutines. I have seen refernces to using my and
>> local for passing variables so I am playing with them to try to understand
>> better how this all works.
>
>Neither 'local' nor 'my' is required for passing arguments to subroutines.
>'my' is often used inside a subroutine to "localize" the scope of variables
>used within the subroutine, but 'local' is rarely used for that purpose or
>any other purpose.
>
>Aliases to values (variables, constants, or expressions) in the calling
>routine are passed in the built-in @_ array.
>
>Your sample program contains two characteristics that indicate it was
>written a long time ago and should be re-written: the use of 'local' to
>declare local variables, and calling subroutines with an ampersand '&'. For
>modern Perls, it is usually better to localize the scope of variables with
>'my' and to call subroutines without using ampersands.
>
>Check 'perldoc perlsub' for how to call subroutines and pass arguments to
>them, and how the ampersand can affect the behavior of your program.


Thanks. Big problem with the net is a lack of time reference. 




>> I have a subroutine in the script below, printsub that errors when it is
>> run with 
>> 
>> Global symbol "$name" requires explicit package name at ./user-sub.pl line
>> 143.
>> Global symbol "$name" requires explicit package name at ./user-sub.pl line
>> 144.
>> Execution of ./user-sub.pl aborted due to compilation error
>> 
>> This is the code pulled out of easy viewing:
>> 
>> sub printsub
>> {
>>     local($name) = $_[0];
>>     print "This is the value of $name in the subroutine.\n";
>> }
>
>This syntax is very old and dates from Perl 4. The error message is because
>you have the very modern 'use strict' in your script, but are using the very
>old 'local'. Replace with:
>
>    my $name = $_[0];
>
>and all will be fine. You have also found the equivalent below:
>
>> 
>> The second sub in this script works using:
>> 
>>     my $name = shift(@_);
>> 
>> another example I found online.
>
>Either of those two forms is how subroutines arguments are passed and
>accessed in subroutines. The first leaves @_ untouched, while the second
>removes the first member from @_. You can also use members of @_ directly,
>but you should be aware that the members are aliases, and if you change a
>value of a member aliased to a variable in the calling routine, the value in
>the calling routine will also be changed.
>
>> I would appreciate some help understanding if or how local can be used in
>> this context. Also if there is a better/preferred way to pass this var to
>> the sub.
>
>'local' declares a new value for a package (global) variable. When the
>declaration goes out of scope, the old value is restored. 'local' is not
>needed to pass arguments to subroutines, and it is usually better to limit
>the scope using 'my', rather than 'local'. 'my' declares a lexically-scoped
>variable. The difference is that the new value declared by 'local' will be
>available to subroutines called by the local-declaring subroutine, while
>variables declared with 'my' will not be accessible by other subroutines at
>all.
>
>
>
-- 

Karyn Stump
Network Services Manager
California Institute of the Arts
ka...@calarts.edu
http://noc.calarts.edu

-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to