Order of evaluation of arguments in a subroutine

Discussion in 'Perl Misc' started by Krishna Chaitanya, Dec 9, 2010.

  1. In Perl, is the order of evaluating subroutine args L-to-R?

    I saw in perlop that a comma operator in a list context evaluates from
    L-to-R, and also in perlsub that all args to a subroutine are squashed
    into 1 single list......can I deduce from these 2 statements that
    order of evaluation of subroutine args is L-to-R as well?

    I wrote some sample code like this, and would like to predict its
    outcome (if possible):

    ====================

    #!/usr/bin/perl

    sub func { }

    my $a = sub { print "Hello"; };
    my $b = sub { print " world "; };

    func($a->(),$b->());

    ====================

    TIA,
    Chaitanya
     
    Krishna Chaitanya, Dec 9, 2010
    #1
    1. Advertising

  2. Krishna Chaitanya <> writes:
    > In Perl, is the order of evaluating subroutine args L-to-R?
    >
    > I saw in perlop that a comma operator in a list context evaluates from
    > L-to-R, and also in perlsub that all args to a subroutine are squashed
    > into 1 single list......can I deduce from these 2 statements that
    > order of evaluation of subroutine args is L-to-R as well?


    I believe so. "perldoc perlop" says:

    Comma Operator
    Binary "," is the comma operator. In scalar context
    it evaluates its left argument, throws that value away,
    then evaluates its right argument and returns that value.
    This is just like C’s comma operator.

    In list context, it’s just the list argument separator, and
    inserts both its arguments into the list. These arguments
    are also evaluated from left to right.

    I'm fairly sure that subroutine arguments are evaluated as an ordinary
    list, so the second paragraph would apply.

    > I wrote some sample code like this, and would like to predict its
    > outcome (if possible):
    >
    > ====================
    >
    > #!/usr/bin/perl
    >
    > sub func { }
    >
    > my $a = sub { print "Hello"; };
    > my $b = sub { print " world "; };
    >
    > func($a->(),$b->());
    >
    > ====================


    Consider whether you really need to do this? I'm not 100% certain
    that my conclusion above is correct (though I'll be more confident
    if nobody shoots it down in flames in the next couple of days).
    And though it's not entirely relevant, there are plenty of other
    languages in which the order of evaluation of subroutine arguments
    is unspecified.

    Even if the language guarantees left-to-right evaluation, I suggest
    that your code would likely be clearer if it didn't depend on
    this guarantee.

    my $first = $a->();
    my $second = $b->();
    func($first, $second);

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Dec 9, 2010
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Ilias Lazaridis
    Replies:
    2
    Views:
    393
    Ilias Lazaridis
    Apr 24, 2005
  2. dragoncoder

    Order of evaluation of function arguments

    dragoncoder, Dec 21, 2005, in forum: C Programming
    Replies:
    21
    Views:
    852
    Keith Thompson
    Dec 23, 2005
  3. Peter
    Replies:
    7
    Views:
    318
    Mark P
    Mar 22, 2007
  4. Michael Garriss

    order of evaluation for method arguments

    Michael Garriss, Sep 13, 2003, in forum: Ruby
    Replies:
    6
    Views:
    137
  5. king
    Replies:
    5
    Views:
    188
Loading...

Share This Page