Using named parameters without names when calling functions in perl
Sunday, December 11, 2011 9:29:38 PM
So, I noticed he got a comment that he could use Devel::Declare and do something like the Perl6 implementation of prototypes, but I think it can be even easier than that (as it requires you to actually make new keywords and what not.)
Enter autoparams.pl:
#!/usr/bin/perl -wl
use strict;
use warnings;
use PadWalker qw/var_name/;
use Data::Dumper;
{
no strict 'refs';
*{'main:::'} = sub {
my $ret;
my @params;
foreach (@_) {
my $var_name = var_name(1,\$_);
do { $ret->{substr $var_name, 1} = $_; next } if $var_name;
push @params, $_;
}
return $ret, @params;
};
}
sub foo {
print Dumper(\@_);
}
my $a = 123;
my $b = 'abc';
my $c = { foo => 'bar' };
foo(&:($a, $b, $c, '456', '567'));
gives:
$ perl perl/autoparams.pl
$VAR1 = [
{
'c' => {
'foo' => 'bar'
},
'a' => 123,
'b' => 'abc'
},
'456',
'567'
];
Of course the implementation details could be slightly different, but I'm thinking something like this might make sense.














Unregistered user # Monday, December 12, 2011 9:23:37 AM
Unregistered user # Monday, December 12, 2011 3:33:52 PM
Unregistered user # Monday, December 12, 2011 9:13:04 PM