Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

sending scalar and hash to function

4 views
Skip to first unread message

bu...@alejandro.ceballos.info

unread,
Jun 27, 2011, 2:33:33 PM6/27/11
to beginn...@perl.org, jo...@joal.net
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

Shaun Fryer

unread,
Jun 27, 2011, 7:24:23 PM6/27/11
to bu...@alejandro.ceballos.info, jo...@joal.net, beginn...@perl.org
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

> --
> To unsubscribe, e-mail: beginners-cg...@perl.org
> For additional commands, e-mail: beginners...@perl.org
> http://learn.perl.org/
>
>

0 new messages