Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: %+ and cloning

by BrowserUk (Pope)
on Jan 11, 2010 at 12:03 UTC ( #816708=note: print w/ replies, xml ) Need Help??


in reply to %+ and cloning

Try this version of your code that uses Data::Dump and things become obvious:

#! perl -slw use strict; use warnings; use Storable qw(dclone); use Data::Dump qw[ pp ]; my $str = "Rico"; $str =~ m{(?<name>ico)}xms; my $match = dclone \%+; print \%+,; print " match: $match -", pp $match; $str =~ m{(?<buba>R)}xms; my $match2 = dclone \%+; print \%+; print " match: $match ", pp $match; print "match2: $match2 ", pp $match2; __END__ C:\test>816700 HASH(0x22ee20) match: HASH(0x6f218) -{ # tied Tie::Hash::NamedCapture } HASH(0x22ee20) match: HASH(0x6f218) { # tied Tie::Hash::NamedCapture } match2: HASH(0x6f128) { # tied Tie::Hash::NamedCapture }

%+ is not a real hash, but rather a tied hash. And cloning it appears to simply gives you another tied reference to the same global internal data structure.


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.


Comment on Re: %+ and cloning
Select or Download Code
Re^2: %+ and cloning
by PetaMem (Priest) on Jan 11, 2010 at 12:30 UTC

    Thanks for your insight.

    So I went back to twig technology and copied the hash via
    sub copy_hash { my $hr = shift // return {}; # get hash to copy or return empty ha +shref if not given my %copy = (); while (my ($k, $v) = each %{$hr}) { $copy{$k} = $v; } return \%copy; }

    Which works. So this is it? Best way to go?

    Bye
     PetaMem
        All Perl:   MT, NLP, NLU

      That depends on ones definition of 'best'. One could also write:
      sub copy_hash { my $hr = shift // return {}; my $copy; @$copy{keys %$hr} = value %$hr; $copy; }
      or just:
      sub copy_hash {{%{$_[0]//{}}}}
      Or without a sub:
      my %copy = %+;

      A simple my %copy = %+; is equivalent to your sub. However, that may not be enough!

      According to the docs for Tie::Hash::NamedCapture, the values of the tied hash can contain an array reference holding multiple captures due to the re-use of the same named capture. In which case you might need to use something more complex to perform a deep copy operation.

      That said, in my simple tests, this doesn't seem to apply to %+. It does apply to %- though.


      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.
        It happens if the tie happens with parameters all => 1, but %+ is tied without them. So even if your tests were less simple, you shouldn't have seen it with %+ (and if you ever do, there's a bug).

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (8)
As of 2014-08-27 11:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The best computer themed movie is:











    Results (237 votes), past polls