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

David Hummel ddhummel at pacbell.net
Thu Jun 24 15:08:05 PDT 2004


On Thu, Jun 24, 2004 at 02:46:39PM -0700, Peter Jay Salzman wrote:
> 
> i have a function which takes a hash and scalar arg.  how is this done
> correctly?

By passing the hash as a reference.

> why is $scalar undefined at the print statement in function()?

Because you've shifted all the arguments into function()'s %hash.

> #!/usr/bin/perl
> use strict;
> use diagnostics;
> 
> sub function(%$)

  sub function(\%$)

> {
>    my (%hash, $scalar) = (shift, shift);

     my ($hashref, $scalar) = @_;

>    print "hash: $hash{foo}\n";

     print "hash: $hashref->{foo}\n";

>    print "scalar: $scalar\n";
> }
> 
> my (%hash, $scalar);
> 
> $hash{foo} = "hello";
> $scalar    = "goodbye";
> 
> function(%hash, $scalar);

  function(\%hash,$scalar);


More information about the vox-tech mailing list