Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: passing array to a sub

by Zaxo (Archbishop)
on Aug 25, 2001 at 15:55 UTC ( [id://107814]=note: print w/replies, xml ) Need Help??


in reply to passing array to a sub

Setting values to feed example() and printing inside the function works. You are courting trouble by reusing $scalar1 and friends as names in the sub and not making them lexical with my.

Please say what the symptoms of 'not working' are, and show how the variables are declared and assigned.

$ cat pit.pl #!/usr/bin/perl -- use strict; use warnings; my $scalar1 = 'foo'; my $scalar2 = 'ferall'; my @info1 = ( 0..10 ); &example($scalar1, $scalar2, @info1); sub example { $scalar1 = shift;$scalar2 = shift;@info1 = @_; # bad # prefer lexical scope, ought not to reuse names # my ($scalar1, $scalar2, @info1) = @_; print $scalar1,$/; print $scalar2,$/; print @info1,$/; # ..... } $ ./pit.pl foo ferall 012345678910 $

After Compline,
Zaxo

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://107814]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others musing on the Monastery: (1)
As of 2024-04-19 18:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found