Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Re: Safely altering the behaviour of heavily used modules

by demerphq (Chancellor)
on Apr 08, 2006 at 20:15 UTC ( #542072=note: print w/replies, xml ) Need Help??


in reply to Safely altering the behaviour of heavily used modules

Seems to me that the problem with storable is that assignment to $_[0] doesn't work as it should work, or more specifically, as it would work in a normal perl subroutine.

For instance when I read this node my first though was "Hmm, you mean

sub STORABLE_thaw { $_[0]= $singleton; }

doesn't work?". And of course when I tried it, it didn't.

Anyway, it seems to me that making assignment to @_ possible in the STORABLE_freeze() hook would resolve your problem, and I think would be backwards compatible.

Ill just add the mandatory shameless plug: with Data::Dump::Streamer this is easy.

package Foo; use Data::Dump::Streamer; my $singleton=bless {},'Foo'; my $proxy=bless [],'Bar'; my $nope; sub DDS_freeze { return if $nope; return ($proxy,"->DDS_thaw") } sub Bar::DDS_thaw { $singleton; } my @array=($singleton,$singleton,$singleton); my $array=Dump(\@array)->Names('array')->Out(); print "$array\n---\n"; eval $array or die $@; $nope=1; Dump($singleton,\@array,$array)->Names(qw(singleton *array array))->Ou +t(); __END__ $array = [ bless( [], 'Bar' )->DDS_thaw(), 'V: $array->[0]', 'V: $array->[0]' ]; $array->[1] = $array->[0]; $array->[2] = $array->[0]; --- $singleton = bless( {}, 'Foo' ); @array = ( $singleton, $singleton, $singleton ); $array = [ $singleton, $singleton, $singleton ];
---
$world=~s/war/peace/g

Replies are listed 'Best First'.
Re^2: Safely altering the behaviour of heavily used modules
by adamk (Chaplain) on Apr 09, 2006 at 03:49 UTC
    After a huge amount of debate and discussions (see that link to RT) and some money slipped the way of an XS expert, this problem was solved, and solved both fully back-compatibly and in the best possible manner.

    See "STORABLE_attach" in the Storable POD for details.

      Regardless as to whether you added a new method. IMO Assignment to @_ should work as expected.

      ---
      $world=~s/war/peace/g

        It can't ever work, because of the problem of references.

        Storable supports circular references correctly, and the implementation of this means that what is in $_[0] may already have 4 or 5 other Perl references pointing to it by the time the hook gets called.

        So if you were to replace $_[0] with $singleton, you would break all the other references.

        If Perl references were purely heirachal, this wouldn't be a problem.

        And that is why we have a seperate method. Because it provides an alternative that certifies that that class won't end up creating circular references below it, and thus that a modify/return type result will work sanely.

        Read through bug #4901 and you might be able to understand it a bit more. It took me a long time.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others examining the Monastery: (8)
As of 2020-05-27 13:44 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    If programming languages were movie genres, Perl would be:















    Results (155 votes). Check out past polls.

    Notices?