Discussion:
sending scalar and hash to function
b***@alejandro.ceballos.info
2011-06-27 18:33:33 UTC
Permalink
I am trying to send an scalar and hash to a function, but is not
receiving the value of the hash

My code is:

dosomething('option',{'extraparam1'=>'hello'});

function dosomething
{
($myopt,%myparams) = @_;
print "opt = $myopt\n";
while( my ($k, $v) = each %myparams )
{ print "$k = $v \n"; }
}

And result is:

opt = option
HASH(0xcb4f490) =
HASH(0xcb4f490) =


I have tryied different solutions (look below) but no one
loads/display the values of %myparams:

1) function dosomething
{
($myopt,%myparams) = @_;
print "opt = $myopt\n";
while( my ($k, $v) = each %{$myparams} )
{ print "$k = $v \n"; }
}

2) function dosomething
{
$myopt = shift;
%myparams = shift;
print "opt = $myopt\n";
while( my ($k, $v) = each %$myparams )
{ print "$k = $v \n"; }
}

3) function dosomething
{
use Tie::RefHash;
($myopt,%myparams) = @_;
tie %hash_postparams, "Tie::RefHash";
print "opt = $myopt\n";
while( my ($k, $v) = each %myparams )
{ print "$k = $v \n"; }
}

Any idea what I am doing wrong?



Really thank you in advance,

Alejandro
--
To unsubscribe, e-mail: beginners-cgi-***@perl.org
For additional commands, e-mail: beginners-cgi-***@perl.org
http://learn.perl.org/
Shaun Fryer
2011-06-27 23:24:23 UTC
Permalink
In the first code snippit, your function will receive a hash reference,
which is a scalar. If you get rid of the curly braces, it will work. Better
still would be....

my ($myopt, $myparams) =@_;

...and...

each %$myparams

--
Shaun Fryer
1-647-723-2729
Post by b***@alejandro.ceballos.info
I am trying to send an scalar and hash to a function, but is not
receiving the value of the hash
dosomething('option',{'extraparam1'=>'hello'});
function dosomething
{
print "opt = $myopt\n";
while( my ($k, $v) = each %myparams )
{ print "$k = $v \n"; }
}
opt = option
HASH(0xcb4f490) =
HASH(0xcb4f490) =
I have tryied different solutions (look below) but no one
1) function dosomething
{
print "opt = $myopt\n";
while( my ($k, $v) = each %{$myparams} )
{ print "$k = $v \n"; }
}
2) function dosomething
{
$myopt = shift;
%myparams = shift;
print "opt = $myopt\n";
while( my ($k, $v) = each %$myparams )
{ print "$k = $v \n"; }
}
3) function dosomething
{
use Tie::RefHash;
tie %hash_postparams, "Tie::RefHash";
print "opt = $myopt\n";
while( my ($k, $v) = each %myparams )
{ print "$k = $v \n"; }
}
Any idea what I am doing wrong?
Really thank you in advance,
Alejandro
--
http://learn.perl.org/
Loading...