Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re^2: Assign (key, value) to a hash w/o clobbering hash

by reasonablekeith (Deacon)
on Sep 28, 2006 at 10:25 UTC ( [id://575322]=note: print w/replies, xml ) Need Help??


in reply to Re: Assign (key, value) to a hash w/o clobbering hash
in thread Assign (key, value) to a hash w/o clobbering hash

To avoid this inefficiency, you could always tie your own hash which didn't clobber the original values, by not implementing CLEAR. So your above line becomes.
%hash = split /X/, 'fooXbar' # added the missing comma ;)
depends how far you want to go. Full example as follows...

(BTW I did try to do this with Tie::StdHash, but couldn't figure it out)

#!/usr/bin/perl use strict; use Data::Dumper; tie my %hash, 'ExpandingHash'; $hash{'still'} = 'here'; %hash = split /X/, 'fooXbar'; print Dumper(\%hash); # I also added a new clear method, which you could use like this. # tied(%hash)->FORCE_CLEAR; __OUTPUT__ $VAR1 = { 'foo' => 'bar', 'still' => 'here' }; #================================================================== package ExpandingHash; use strict; sub TIEHASH { my %self; bless \%self, shift } sub FETCH { $_[0]->{$_[1]} } sub STORE { $_[0]->{$_[1]} = $_[2] } sub FIRSTKEY { each %{$_[0]} } sub NEXTKEY { each %{$_[0]} } sub CLEAR { } # NOT IMPLEMENTED sub DELETE { delete $_[0]->{$_[1]} } sub EXISTS { exists $_[0]->{$_[1]} } sub FORCE_CLEAR { %{$_[0]} = () }
---
my name's not Keith, and I'm not reasonable.

Replies are listed 'Best First'.
Re^3: Assign (key, value) to a hash w/o clobbering hash
by davorg (Chancellor) on Sep 28, 2006 at 11:21 UTC

    That's nasty :)

    It's much simpler using Tie::StdHash as you only need to implement the methods that you want to override.

    package Tie::Hash::ExpandingHash; use strict; use warnings; use Tie::Hash; our @ISA = 'Tie::StdHash'; sub CLEAR { } # NOT IMPLEMENTED sub FORCE_CLEAR { %{$_[0]} = () }
    --
    <http://dave.org.uk>

    "The first rule of Perl club is you do not talk about Perl club."
    -- Chip Salzenberg

      davorg, I did mention I tried to do it with Tie::StdHash :). It's obviously the way to go if one was to choose this method. However, I still can't get a working example going, even using your example above. For me it gives an error when I try to tie the hash...
      Can't locate object method "TIEHASH" via package ...
      the line being
      tie my %hash, 'ExpandingHash';
      and I also tried
      tie my %hash, 'Tie::Hash::ExpandingHash';

      ---
      my name's not Keith, and I'm not reasonable.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://575322]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2024-04-19 20:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found