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

Hi all,

When you construct a call to a function, e.g. solve_problem($problem, $parameter) is it in anyway correct or useful to consider that you are constructing the @_ list array that will be available in the function with the part in braces, just as you would write @array = ($problem, $parameter) ?

I hope that makes sense - as a bit of background, I thought of it when asked why the following two uses of sort didn't work the same way:

my $sort_func = sub { $a <=> $b }; sort $sort_func @to_be_sorted; # Sort 1 sort ($sort_func, @to_be_sorted); # Sort 2
The first use clearly matches 'sort SUBNAME LIST', but the second one looks like plain old 'sort LIST'.

Is this analysis correct? Could thinking like this ever lead to confusion?

Thanks,
brewer

Replies are listed 'Best First'.
Re: Function calls and list creation
by tcf22 (Priest) on May 31, 2003 at 00:19 UTC
    Not really sure what your asking here. The question is a little confusing. With the sort routine, when you put something in () it will combine them into 1 list, so you fell into the 'sort LIST' usage.

    So it you had
    #! /usr/bin/perl use Data::Dumper; my $sort_func = sub { $a <=> $b }; my @to_be_sorted = (1,2,3); my @new = sort ($sort_func, @to_be_sorted); print Dumper \@new;
    It would actually sort the list ($sort_func, 1,2,3) and you would end up with
    $VAR1 = [ 1, 2, 3, sub { "DUMMY" } ];

      It's not the parenthesis that create the list. They just group things. It's the comma. See:

      #!/usr/bin/perl -w use Data::Dumper; my $sort_func = sub { $b <=> $a }; my @to_be_sorted = (1,2,3); # sort LIST my @new = sort ($sort_func, @to_be_sorted); print Dumper \@new; # sort LIST @new = sort $sort_func, @to_be_sorted; print Dumper \@new; # sort SUB LIST @new = sort ($sort_func @to_be_sorted); print Dumper \@new;
        That is good to know.
        Thanks.

        Thanks! The penny drops a little further. I definately need to think on that, probably while sleeping....

        Regards,
        brewer

      Sorry, I wondered how coherent that was.

      First of all, thanks for confirming that the list is sorted like that.

      Basically, I just had a sort of internal re-arrangement of how I thought about calling functions, and wondered if this new view might get me in to trouble one day.

      function($param1, $param2) has stopped feeling like:
      function( $param1, $param2 )
      ^^^^^^^^^                  ^
      
      where there's magic in the way the braces are attached to to the function name and declare that I'm calling a function, and become:
      function ($param1, $param2)
      ^^^^^^^^
      
      where the braces are now associated with creating the list of parameters.

      I wondered if this was closer to the way that function calls are implemented in Perl.

      This is probably no better than my first attempt, but I'm enjoying thinking about it anyway!

      Regards,
      brewer