Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

richardc (1662)

richardc
  (email not shown publicly)
http://unixbeard.net/~richardc/

Journal of richardc (1662)

Monday February 11, 2002
05:41 AM

The worlds ugliest idiom?

[ #2764 ]
In working on some slides I came up with this. It gives you named parameters with pass by reference semantics.

 sub event {
    my %o; # offsets
    for ( my $i = 0; $i < @_; $i += 2 ) {
       $o{$_[$i]} = $i + 1;
    }

    print $_[ $o{left} ], $_[ $o{right} ], "\n"; # prints foobar
    $_[ $o{right} ] = 2 + 2;
    print $_[ $o{left} ], $_[ $o{right} ], "\n"; # prints foo4
 }

 my ($foo, $bar) = qw( foo bar );
 event( left => $foo, right => $bar );
 print $foo, $bar, "\n"; # prints foo4

I'm very sorry to have worked this out, but it does go to show that Sub::Parameters is a much prettier way to go if you wanted these semantics </plug>