You want a positional parameter to a subroutine to be either an array reference or a scalar. If it's a scalar it should be treated as a one element array. OK, so far.

What if you want to preserve the @_ aliasing?
Um...Just use @_?
Say you can't because you're using the rest of @_ for other parameters
(like another optional list).

Well if you find yourself in that improbable situation, you can use shift_list.

sub routine { my $self = shift; my $list = &shift_list; # expose our @_ # then @_ = ($other, $args) s/a/A/g for (@$list); # change the original ... }
If there's other ways I'd like to know, this is a fishing expedition.
sub shift_list { # shift one element of @_ and return it as an array ref # PRESERVING the @_ aliasing # (if called like &shift_list we get the caller's actual @_) my $list; if(ref $_[0]) { $list = shift; unless (ref $list eq 'ARRAY') { die "Not an array ref ($list)"; } } else { # aliasing funkiness # pass aliased $_[0] to sub ref which returns a ref to it's @_ $list = ( sub {\@_} )->($_[0]); shift; # for parent } return $list; }
(That weird ( sub {\@_} )->($_[0]) stuff is stolen from Damian's slice aliasing code posted to perl6-lang)

Replies are listed 'Best First'.
Re: Preserve @_ aliasing a (scalar||array)
by BrowserUk (Patriarch) on Jan 06, 2004 at 09:55 UTC

    This is similar to/a extention of an idea juerd meditated upon (and which I completely missed the point of originally) this time last year at Non-destructive array processing.

    Seems to me that some variation of this could be added to List::Util therebye hiding the obscure nature of the technique behind a standard interface and lifting it out of the realms of "clever code" to a useful, usable idiom?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    Hooray!