argv has asked for the wisdom of the Perl Monks concerning the following question:
So, now what I have is a nice elegant API that looks like:
foo ( { param => "value", ... } );
Until I actually convert all the CALLS to the function in the new format, I've written the functions in such a way that it can accept either form by using ref() on the first param and seeing if it's a hash. If so, the function's vars are set to the has key/value pairs. otherwise, the rest of the args are read from @_.
All's well and good, and things work properly. However, for one function, there were so many params that I thought I would simplify the above process using a loop as illustrated in the code below. The surprise isn't so much to do with the parameters to the function, but the setting of the values to the local my variables within the function.
Can someone explain the scoping going on here that isn't preserving the variables' values outside of the "using args..." block?
#!/usr/bin/perl use strict; sub foo { my ( $flags, $img, $name, $big, $gallery, $page, $caption, $num ); my $opts = shift; if (ref $opts eq "HASH") { print "using hash...\n"; no strict; for (qw( flags img name big gallery page caption num ) ) { $$_ = $opts->{$_}; print "\tsetting $_ to $$_ ($opts->{$_})\n"; } use strict; } else { print "using args...\n"; $flags = $opts; # these vars are local for some reason... ??? ( $img, $name, $big, $gallery, $page, $caption, $num ) = @_; print "flags = $flags, img = $img\n"; # they're getting set... # but their values are now going to vanish... } print "reviewing what's been set...\n"; no strict; for (qw( flags img name big gallery page caption num ) ) { print "\t$_ = $$_\n"; } use strict; } # it doesn't matter which order you call these functions... foo( "hi", "apoinv", "slkjhfp", "876", "poi", "asdf", "sdf", "there" ) +; foo( { flags => "hi", img => "apoinv", name => "slkjhfp", big => "876", gallery => "poi", page => "asdf", caption => "sdf", num => "there" });
|
|---|