The worlds ugliest idiom?

richardc on 2002-02-11T10:41:05

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>