in reply to Function calls and list creation

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" } ];

Replies are listed 'Best First'.
Re: Re: Function calls and list creation
by chromatic (Archbishop) on May 31, 2003 at 00:28 UTC

    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
Re: Re: Function calls and list creation
by brewer (Sexton) on May 31, 2003 at 00:45 UTC

    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