[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: [Omaha.pm] CGI.pm is your friend



Daniel Linder wrote:
Gotcha.  The actual popup_menu() function expands the hash itself.

I think that's almost right. Subroutines always receive and return "a single, flat list of scalars." So the sub doesn't receive a hash. The hash I pass in is flattened into a list of scalars. Inside your sub you often shove that list into a different, local hash.

In our example the sub receives 4 scalars. The fourth (or second, randomly) happens to be an array reference.


$ cat j.pl
#!/usr/bin/perl

use Data::Dumper;
do_something(-arg1 => 'val1', -arg2 => [ 5,6,7 ]);

sub do_something {
  my (%args) = @_;
  print Dumper(%args);
}


$ perl j.pl
$VAR1 = '-arg2';
$VAR2 = [
         5,
         6,
         7
       ];
$VAR3 = '-arg1';
$VAR4 = 'val1';


Or, we can look at it in the debugger (which I strongly prefer):

$ perl -d j.pl
...
 DB<2> x %args
0  '-arg2'
1  ARRAY(0x99faa00)
  0  5
  1  6
  2  7
2  '-arg1'
3  'val1'


Hopefully that helps. Perhaps the idea of throwing a hash into a list and then putting that list into a new hash is confusing... The key/value pairs always stay together, which is the only important thing in hashes.

I was thinking this was some sort of "OO-magic" being performed by the Perl
interpreter auto-magically calling the function multiple times with the
new values.

Nope. Definitely not that.  :)

j