Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

returning hash from sub problems

by gibsonca (Beadle)
on May 21, 2012 at 19:38 UTC ( #971684=perlquestion: print w/ replies, xml ) Need Help??
gibsonca has asked for the wisdom of the Perl Monks concerning the following question:

I don't want to make this hash 'palette' global, but rather pass it to a subroutine, have the hash updated, and then the result passed back, ideally 'palette' is now updated. This code is just my (last bad) guess at doing it.

Not a speed issue.

The problem seems to be with the hash, after it has been passed back.

use strict; use warnings; my $i; my %palette; $palette{'palette_info'}[0] = 128; $palette{'mask_info'}[0] = 0; $palette{'shade_stencil_mask'} = 99 ; my $data; ($data) = &writeCmd(111, \%palette, 222, 333); map { print "$_ => $$data{$_} \n" } keys %$data; # Problem starts after this point, how to update %palette %palette = %$data; # just my last guesss is here now # printf("main : data info = %d, \n", $palette->{'palette_info'}[0]); # printf("main : data shade_stencil_mask = %d, \n", $palette->{'shade +_stencil_mask'}); printf("main : data info = %d, \n", $data->{'palette_info'}[0]); printf("main : data shade_stencil_mask = %d, \n", $data->{'shade_ste +ncil_mask'}); exit(0); #****************** sub writeCmd() { #****************** # # my $command = shift; my $pal = shift; my $address = shift; my $length = shift; map { print "$_ => $$pal{$_} \n" } keys %$pal; # palette_info[0] mask_info'}[0] 'mask_info'}[0] 'shade_stencil_ma +sk' printf("WriteCommand : cmd, addr, len = %d, %d, %d\n", $command, $ad +dress, $length); printf("WriteCommand : pal info = %d, \n", $pal->{'palette_info'}[0] +); printf("WriteCommand : pal shade_stencil_mask = %d, \n", $pal->{'sha +de_stencil_mask'}); $pal->{'palette_info'}[0] = 444; $pal->{'shade_stencil_mask'} = 234; return($pal); } # end of writeCmd()

Comment on returning hash from sub problems
Download Code
Re: returning hash from sub problems
by toolic (Chancellor) on May 21, 2012 at 20:00 UTC
    Since you are already passing by reference, any changes you make in the sub are reflected out of the sub:
    use warnings; use strict; use Data::Dumper; $Data::Dumper::Sortkeys = 1; my %palette; $palette{'palette_info'}[0] = 128; $palette{'mask_info'}[0] = 0; $palette{'shade_stencil_mask'} = 99 ; print Dumper(\%palette); writeCmd(111, \%palette, 222, 333); print Dumper(\%palette); sub writeCmd { my $command = shift; my $pal = shift; my $address = shift; my $length = shift; $pal->{'palette_info'}[0] = 444; $pal->{'shade_stencil_mask'} = 234; } __END__ $VAR1 = { 'mask_info' => [ 0 ], 'palette_info' => [ 128 ], 'shade_stencil_mask' => 99 }; $VAR1 = { 'mask_info' => [ 0 ], 'palette_info' => [ 444 ], 'shade_stencil_mask' => 234 };

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://971684]
Approved by toolic
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (6)
As of 2014-12-20 01:17 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls