Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Need cleaner way to read @_ when dereferencing

by Sandy (Curate)
on Jan 14, 2004 at 16:52 UTC ( [id://321305]=perlquestion: print w/replies, xml ) Need Help??

Sandy has asked for the wisdom of the Perl Monks concerning the following question:

Any ideas how this could be 'cleaner?'
sub myfunc($\@\@$) { my $var1 = $_[0]; my @arr1 = @{$_[1]}; my @arr2 = @{$_[2]}; my $var2 = $_[3]; }
or
sub myfunc($\@\@$) { my ($var1,$tmp1,$tmp2,$var2) = @_; my @arr1 = @$tmp1; my @arr2 = @$tmp2; }
Update: Fixed code snippet as shown by dragonchild. It was just a typo.

Replies are listed 'Best First'.
Re: Need cleaner way to read @_ when dereferencing
by dragonchild (Archbishop) on Jan 14, 2004 at 16:58 UTC
    First, you've got $ and @ mixed up. If you used warnings, you'd get a statement about 1-element array slices. That looks like it really should be:
    sub myfunc($\@\@$) { my $var1 = $_[0]; my @arr1 = @{$_[1]}; my @arr2 = @{$_[2]}; my $var2 = $_[3]; }

    Secondly, your second version is what should happen, except you don't have to dereference $tmp1 and $tmp2. You can replace as so:

    @arr1[3] --- $tmp1->[3]

    Hope that helps!

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

      you don't have to dereference $tmp1 and $tmp2
      Unless you want to work on a local copy.

      The PerlMonk tr/// Advocate
        If you want to edit and not have those changes reflected, yes. However, when using the \@ prototype, my experience is that you usually want to have your changes reflected in the array when you're done. In fact, most examples in the docs regarding prototypes and arrays involve things like my_push() and my_pop(), which do change their parameters. YMMV

        ------
        We are the carpenters and bricklayers of the Information Age.

        Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others about the Monastery: (2)
As of 2024-04-26 00:27 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found