develooper Front page | perl.beginners.cgi | Postings from June 2011

Re: sending scalar and hash to function

Thread Previous
From:
Shaun Fryer
Date:
June 27, 2011 16:24
Subject:
Re: sending scalar and hash to function
Message ID:
BANLkTimuWLWLhGMnsNsxvurBnd5NRFZ6_g@mail.gmail.com
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
On Jun 27, 2011 2:33 PM, <buzon@alejandro.ceballos.info> wrote:
> 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-unsubscribe@perl.org
> For additional commands, e-mail: beginners-cgi-help@perl.org
> http://learn.perl.org/
>
>

Thread Previous


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About