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 14: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.