I used to use only positional arguments in all of my Perl functions simply because I didn't know any better. There are problems with that approach as this article points out. With any new development, I am solely using named arguments but what about legacy code? I wondered if I could change a method to accept named arguments but still use the same positional arguments so that I wouldn't have to update my legacy code base. Here's what I came up with:
if (ref(@_[0]) eq "HASH") { my %args = %{@_[0]}; foreach my $arg (keys %args) { eval("\$$arg = \$args{\$arg}"); } }What this code snippet does is look at the first argument in the list to determine its type. If it's a HASH, then we know the arguments are being passed as named arguments. So we convert them using the eval function to look just like the old positional arguments.
So let's say we have this legacy Perl function:
sub test { my ($arg1, $arg2, $arg3) = @_; print "\$arg1 = $arg1\n"; print "\$arg2 = $arg2\n"; print "\$arg3 = $arg3\n"; }To convert it, we just add the new code snippet in. Here's a test script to see it in action.
#!/usr/bin/perl use strict; &test("abc", "123", "xyz"); &test({ arg1 => 'abc', arg2 => '123', arg3 => 'xyz' }); sub test { my ($arg1, $arg2, $arg3) = @_; if (ref(@_[0]) eq "HASH") { my %args = %{@_[0]}; foreach my $arg (keys %args) { eval("\$$arg = \$args{\$arg}"); } } print "\$arg1 = $arg1\n"; print "\$arg2 = $arg2\n"; print "\$arg3 = $arg3\n"; }
0 comments:
Post a Comment