in reply to Re^11: Preferred technique for named subroutine parameters?
in thread Preferred technique for named subroutine parameters?

For instance in functional programming anonymous functions are passed around, even in arrays or hashes and then you might even want to assign further data to them. ... it's certainly not useless to do so.
I completely agree it is far from useless. But there should be no problem passing a reference to an arbitrarily complex data structure as the parameter of a named parameter and then accessing the data in any legal way whatsoever. (And of course, this works equally well with either of the techniques for passing named parameters described in the OP, so this whole, rather verbose sub-thread is actually a little bit OT.)

To verify my understanding of what you have written and to illustrate what I mean, an example:

>perl -wMstrict -le "my $funcs = [ sub { print q{i'm at index zero} }, sub { print q{and i'm at index 1} }, ]; add_func({ fns => $funcs, fn => sub { print q{i'm new here} }, at => 3 }); $funcs->[0](); $funcs->[3](); sub add_func { my %args = %{ shift() }; $args{fns}->[$args{at}] = $args{fn} } " i'm at index zero i'm new here
It wasn't clear that your problem is "what is it good for", you sounded more like "theres no difference".
There is, indeed, a vast difference between passing a reference as a named parameter name (i.e., a hash key) and passing a reference as the parameter (i.e., hash value). My problem with passing a reference as a name was squarely in the "what's it good for" camp.

Replies are listed 'Best First'.
Re^13: Preferred technique for named subroutine parameters?
by LanX (Saint) on May 24, 2009 at 12:43 UTC
    But there should be no problem passing a reference to an arbitrarily complex data structure as the parameter of a named parameter and then accessing the data in any legal way

    Well it's insofar a problem that it's more complicated and unintuitive.

    Would you preferre to use HTML this way?  <tag="a" name="anchor" ... >

    "There should be no problem" to use a workaround is good argument to abandon Perl for the sake of a more primitive language.

    Me, I can perfectly imagine a case where I'd like to pass anonymous functions with parameters (e.g to use as defaults):

    doit ( \&func1 => [para1, para2], \&func2 => [para7, para4] )

    Cheers Rolf

      But there should be no problem passing a reference to an arbitrarily complex data structure ...
      Well it's insofar a problem that it's more complicated and unintuitive. Would you preferre to use HTML this way?
          <tag="a" name="anchor" ... >
      "There should be no problem" to use a workaround is good argument to abandon Perl for the sake of a more primitive language.
      But if you consider the Perl "named parameters" alternative to lengthy positional argument lists in function calls (especially where defaults may be involved) to be a complicated and unintuitive workaround, then just don't use it! It's a perfectly valid choice. (However, you may not have much luck in seeking for a solution among "more primitive" languages: I think few, if any, such languages offer anything other than positional function argument lists.)
      Me, I can perfectly imagine a case where I'd like to pass anonymous functions with parameters (e.g to use as defaults):
          doit ( \&func1 => [para1, para2], \&func2 => [para7, para4] )
      True, if you go anywhere near a hash with that argument list, you will stringize and therefore completely destroy the function references. So, don't do that!

      However, consider this example, and consider what it would take to implement it as a function with a pure positional and possibly defaulted argument list:

      use warnings FATAL => 'all'; use strict; doit({ func3 => [ \&up => [ qw(x y z) ] ] }); sub doit { my %defaults = ( func1 => [ sub { print map $_ . '1', @_ }, [ qw(a b) ] ], func2 => [ sub { print map $_ . '2', @_ }, [ qw(c d e) ] ], func3 => [ sub { print map $_ . '3', @_ }, [ qw(f g) ] ], ); my %args = (%defaults, %{ shift() }); for my $func (qw(func1 func2 func3)) { die "unknown func $func" unless exists $args{$func}; $args{$func}->[0]->(@{ $args{$func}->[1] }); print "\n"; } } sub up { print map uc, @_; }
      Output:
      a1b1 c2d2e2 XYZ
      Again, it's a matter of personal preference: I know what looks better to me.