in reply to Localizing hash keys in an AoH
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
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Localizing hash keys in an AoH
by kennethk (Abbot) on Mar 15, 2013 at 22:49 UTC | |
by tobyink (Canon) on Mar 15, 2013 at 22:58 UTC | |
by tobyink (Canon) on Mar 17, 2013 at 00:32 UTC |
In Section
Seekers of Perl Wisdom