Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Re: order of arguments evaluated

by JamesNC (Chaplain)
on May 17, 2005 at 16:47 UTC ( [id://457892]=note: print w/replies, xml ) Need Help??


in reply to order of arguments evaluated

You could use reverse I would think it is better to fix your code. Perhaps named args? They help document, avoid confusion and set defaults.
use strict; &foo( x=> 10, ); sub foo { my %args = @_; my $x = $args{'x'} || 0; my $y = $args{'y'} || 0; print "Args: x=> $x , y=> $y\n"; }

Replies are listed 'Best First'.
Re^2: order of arguments evaluated
by ivancho (Hermit) on May 17, 2005 at 18:34 UTC
    let me charge in with my new found understanding, limited as it may be.

    reverse is unlikely to solve the original problem: printit($var,$var++);
    will produce exactly the same as printit(reverse($var++,$var)), namely '21'.

    Also, the named args would suffer from exactly the same issue - have you tried foo(x => $var, y => $var++) with your construction? After all, it is just another way of writing foo("x",$var,"y",$var++)

    From what I gather, Perl always sends things effectively by reference - and that reference is decided in a left-to-right order over the arguments. The thing is, when it sees $var++, it copies the value of $var to a temporary variable, increases $var, but sends a reference to the temp.
    From there onwards, no changes in $var would have influence on what is sent from the spot of $var++ - nothing else is referencing that temp var. Where $var is, on the other hand, we have already decided to send the reference to $var - but the other terms may also have access to that piece of memory, in which case only what is there at the end of the entire argument list matters.
    You could try and fix the value in each slot, by sending $var+0, or whatever, but that looks evil

    Additionally, if we think about '+' in a functional sort of way, ie $x + $y being actually +($x,$y)... or (+ $x $y), whatever your poison is, then we can solve any sort of conundrums, like what the code below would produce

    my $m = 20; print ++$m + $m++;
    (ie, 43), or why, for example, the following are completely different:
    $var++ + ($var + $var)
    and
    $var++ + $var + $var
    perlop seems to be misleading in that sense:
    Auto-increment and Auto-decrement:
    ``++'' and ``--'' work as in C. That is, if placed before a variable, they increment or decrement the variable by one before returning the value, and if placed after, increment or decrement after returning the value.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others admiring the Monastery: (5)
As of 2024-04-25 21:15 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found