[vox-tech] perl: function with hash and scalar args

Peter Jay Salzman p at dirac.org
Thu Jun 24 15:28:52 PDT 2004


thanks david, matt and jeff,

On Thu 24 Jun 04,  2:57 PM, Jeff Newmiller <jdnewmil at dcn.davis.ca.us> said:
> On Thu, 24 Jun 2004, Peter Jay Salzman wrote:
> 
> > i have a function which takes a hash and scalar arg.  how is this done
> > correctly?  why is $scalar undefined at the print statement in
> > function()?
> 
> Because argument lists are always lists... and hashes decompose into lists
> of alternating "key,value" pairs... and when you add $scalar into the
> list, you can't differentiate where the hash portion of the list ends and
> other arguments end.
 
this clears up another mystery for me.  perl was complaining about an
"odd number of elements in the hash" (in my real code).  now i understand
why (although i'm at a loss why i didn't get the same "odd number of
hash element" message in the test code i posted).

after changing my (real) code, everything works.  i had momentary
confusion when i was able to "dereference" the hash with $hash{foo}
instead of $$hashref{foo}, but then i realized that my hash was declared
outside of any functions, and so was global (which is exactly what i'm
changing right now and got me in trouble with argument lists).

thanks for you help!

pete


More information about the vox-tech mailing list