Beefy Boxes and Bandwidth Generously Provided by pair Networks vroom
laziness, impatience, and hubris
 
PerlMonks  

Re: Parameters, subs and the shift function

by Abigail-II (Bishop)
on Aug 19, 2003 at 07:08 UTC ( [id://284850]=note: print w/replies, xml ) Need Help??

This is an archived low-energy page for bots and other anonmyous visitors. Please sign up if you are a human and want to interact.


in reply to Parameters, subs and the shift function

Parameters to a function get flattened to a single list. If you call a sub as test (3, @a, @b), then inside test you can't distinguish what came from @a and what from @b. If you want to pass entire arrays, you will have to pass references, and deal with references in the function.
sub test { my $number = shift; my @array1 = @{+shift}; # Shift off the reference, and derefe +rence it my @array2 = @{+shift}; # Do your stuff } test (2, \@a, \@b);

Abigail

Replies are listed 'Best First'.
Re: Re: Parameters, subs and the shift function
by monktim (Friar) on Aug 19, 2003 at 10:42 UTC
    Abagail-II, what is the signifigance of the + in the shift statements? Why is this a good thing to do?
    my @array1 = @{+shift}; my @array2 = @{+shift};
    Thanks,
    Tim
      Run it under strict, and try it without the +. The error message you get will enlighten you.

      Abigail

        Ahh, thank you. I'm a little embarrassed. I never dereferenced a builtin like shift only user defined functions.
        Global symbol "@shift" requires explicit package name at noname5.pl li +ne 5. Global symbol "@shift" requires explicit package name at noname5.pl li +ne 6. Execution of noname5.pl aborted due to compilation errors.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://284850]
help
Sections?
Information?
Find Nodes?
Leftovers?
    Notices?
    hippoepoptai's answer Re: how do I set a cookie and redirect was blessed by hippo!
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.