Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: (my?) problem with re-blessed references(?)

by djantzen (Priest)
on Dec 13, 2002 at 06:10 UTC ( #219508=note: print w/replies, xml ) Need Help??


in reply to (my?) problem with re-blessed references(?)

The problem doesn't have anything to do with bless, but rather with the fact that you are storing your instance variables in the package scoped lexicals %phrase, %author, %approved and %date. This is equivalent to private static variables in C++/Java, and means that only one instance of each exists per package, all shared between instances of the class. Thus, the last created object overwrites those values. Variables declared using use vars or our are basically equivalent to public static in that you can refer to them from outside the package, i.e., %Quote::phrase, but are likewise shared among all instances.

If you want unique data per instance, you'll need to store that data inside the object itself, which in this case means using a hash or array, not a scalar, as the blessed reference.

Replies are listed 'Best First'.
Re: Re: (my?) problem with re-blessed references(?)
by chromatic (Archbishop) on Dec 13, 2002 at 07:50 UTC

    He's using the Flyweight pattern. Check the hash keys carefully.

      I think that BrowserUK's version isn't using the memory address as the hash key (which is what Abigail-II does). So rather than each object looking up parameters in the superclass like so:

      $phrase{'Quote=HASH(0x804b514)'}

      It's just calling the same key each time.

      This is why Abigail's objects need to clean up after themselves much more actively.

      If he's using Flyweight intentionally, then the overwriting of instance data is to be expected. This is not what he wants however. The point of the Flyweight is to enable the quick flow of data through a standard framework. But he appears simply to want standard inheritance, for which package lexicals will not do for more than one instance.

      Update: D'oh! so sub phrase : lvalue { $phrase{shift}; } means that the shifted value is the memory address of the thingy and since it's an lvalue that is assigned the unique instance data. Ergo, my $opinion = ('cool!' && 'eewww!').

        See Adrianh's post below for the ({sheepish grin}stupid) reason the data was being overwritten. The inheritance is now working correctly.


        Examine what is said, not who speaks.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (7)
As of 2020-02-21 16:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What numbers are you going to focus on primarily in 2020?










    Results (96 votes). Check out past polls.

    Notices?