Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Localizing hash keys in an AoH

by kennethk (Monsignor)
on Mar 15, 2013 at 18:54 UTC ( #1023759=perlquestion: print w/ replies, xml ) Need Help??
kennethk has asked for the wisdom of the Perl Monks concerning the following question:

This seems like it should be possible, but I can't seem to wrap my head around how to get the scope right. I have a complex data structure where I need to temporarily modify some key-values - by doing this I get to reuse some rather complex parsing logic. As code is worth 1000 words, I mean something like:

#!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @data = ( {unit => 'S', value => 50, }, {unit => 'T', value => 60, }, {unit => 'Q', value => 70, }, ); # Works LOCAL_BLOCK_1: { local $data[0]{unit} = 'S'; local $data[1]{unit} = 'S'; local $data[2]{unit} = 'S'; print Dumper \@data; } print Dumper \@data;

This works well if you know your array length; however I can't figure out how to make this work for an arbitrary-length array. I've tried the following variants:

# Doesn't work LOCAL_BLOCK_2: { local $data[$_]{unit} = 'S' for 0 .. $#data; print Dumper \@data; } # Doesn't work LOCAL_BLOCK_3: { local $_->{unit} = 'S' for @data; print Dumper \@data; } ## Doesn't work, fatal #LOCAL_BLOCK_4: { # local map {$_->{unit}} @data; # $_->{unit} = 'S' for @data; # # print Dumper \@data; #}
I understand why none of these work; but it seems like what I want to do should be possible. Right now the actual code looks something like:
LOCAL_BLOCK: { my @units = map $_->{unit}, @data; $_->{unit} = 'S' for @data; parser(@data); $data[$_]{unit} = $units[$_] for 0 .. $#data; }
The parser can die, which results in some misdirection with error messages and potentially corrupt records. Local is so much cleaner here, if only it worked. What am I missing? Is my explicit cache and restore the only solution, with the addition of an catch-and-throw around the value restore?

#11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Comment on Localizing hash keys in an AoH
Select or Download Code
Re: Localizing hash keys in an AoH
by Anonymous Monk on Mar 15, 2013 at 19:38 UTC
      So string eval works so long as the entire trailing code block is in the string, which gets pretty clunky for one scenario I'm dealing with. But it works, which is better than I've gotten on my own.
      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; my @data = ( {unit => 'S', value => 50, }, {unit => 'T', value => 60, }, {unit => 'Q', value => 70, }, ); LOCAL_BLOCK: { my $cmd; $cmd .= "local \$data[$_]{unit} = 'S';\n" for 0 .. $#data; $cmd .= 'print Dumper \@data'; eval $cmd; } print Dumper \@data;

      I'll do some digging on the cited modules, to see if they work in my context.


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Localizing hash keys in an AoH
by BrowserUk (Pope) on Mar 15, 2013 at 19:48 UTC

    Can you not just localise the entire AoH?:

    { local @data; $data[ $_ ]{unit} = 'something' for 0 .. $#data; ... } # all changes undone here

    With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.
      That would probably require:
      local @data = @data;
      else local @data would initialize empty. (At least it does, in my tests).

      Suffers from "Can't localize lexical variable @data at ..."

      Update: this works:

      LOCAL_BLOCK_5: { local @data[0..$#data]; $_->{unit} = 'S' for @data; print Dumper \@data; }
      Update 2: Just noticed kennethks solution below, that changes @data to a package (our) variable, and allows it to be localized.

                   "I'm fairly sure if they took porn off the Internet, there'd only be one website left, and it'd be called 'Bring Back the Porn!'"
              -- Dr. Cox, Scrubs

        Localizing the array slice seems to work, which is weird because localizing the array did not -- see Re^2: Localizing hash keys in an AoH. Huh. Regardless, thank you! Just what the doctor ordered.


        #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

      When you localize the array, it's only the array elements that get localized; you still have the original hash refs.
      #!/usr/bin/perl use strict; use warnings; use Data::Dumper; our @data = ( {unit => 'S', value => 50, }, {unit => 'T', value => 60, }, {unit => 'Q', value => 70, }, ); LOCAL_BLOCK: { local(@data) = @data; $_->{unit} = 'S' for @data; } print Dumper \@data;
      outputs
      $VAR1 = [ { 'unit' => 'S', 'value' => 50 }, { 'unit' => 'S', 'value' => 60 }, { 'unit' => 'S', 'value' => 70 } ];

      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

Re: Localizing hash keys in an AoH
by tobyink (Abbot) on Mar 15, 2013 at 22:26 UTC

    Guards.

    use strict; use warnings; use Data::Dumper; sub localised { package localised; my ($ref, $new) = @_; my $old = $$ref; $$ref = $new; return bless [$ref, $old]; sub DESTROY { my ($ref, $old) = @{+shift}; $$ref = $old; } } my @data = ( { unit => 'S', value => 50 }, { unit => 'T', value => 60 }, { unit => 'Q', value => 70 }, ); LOCAL_BLOCK_1: { my @guards = map localised(\$data[$_]{unit}, "S"), 0 .. $#data; print Dumper \@data; } # Note that @guards has gone out of scope. # Hey, presto... DESTROY gets called! print Dumper \@data;
    package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

      Very nice. I think this one would give my coworkers tremendous pause, but this will work in the general case, whereas NetWallah's solution fails if we have some more grotesque AoHoAoAoH thing going on.

      Update: And it actually gives you tremendous control over localization scope, since you can stash the guard at whatever level you want. Nice.


      #11929 First ask yourself `How would I do this without a computer?' Then have the computer do it the same way.

        It could be made more readable. Defining entire packages within a sub is a trick I'm fond of, but I'm aware it's not everybody's cup of tea.

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

        Indeed, but you don't really need to wait for the guard to go out of scope. It's just a blessed object, so you can call $guard->DESTROY manually any time you like.

        package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name

Log In?
Username:
Password:

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

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

    When choosing user names for websites, I prefer to use:








    Results (245 votes), past polls