Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

Re: Two argument bless sometimes ignores the class name?

by !1 (Hermit)
on Jan 15, 2005 at 06:42 UTC ( #422468=note: print w/replies, xml ) Need Help??


in reply to Two argument bless sometimes ignores the class name?

Hmm, this seems to be a trick with bless. At least that's all I can deduce from this snippet:

#!/usr/bin/perl -l *Bar:: = \*Foo::; print ref bless{}, "Bar"; __END__ Foo

Haven't seen anything about this in any of the docs but the behavior is consistent since at least 5.6.1.

Update: Even more curious...

#!/usr/bin/perl -l sub Foo::j { print "F ",ref shift } sub Bar::j { print "B ",ref shift } $a = bless {}, "Bar"; *Bar:: = \*Foo::; $b = bless {}, "Bar"; print "\$a is type ",ref $a; print "\$b is type ",ref $b; $a->j; Bar::j($a); *{"Bar::j"}{CODE}->($a); $b->j; __END__ $a is type Bar $b is type Foo B Bar B Bar F Bar F Foo

I think there are some compile-time vs runtime considerations that must be made.

Update part 2: As far as I understand it, when you bless a reference, the blessed reference will get a pointer to the stash designated by the string you pass as the second parameter. The blessed reference never actually gets the string passed as the second parameter. What ref actually returns is the name on the stash to which the reference points. Thus since Bar points to Foo's stash, you will always get Foo when you bless with Bar.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://422468]
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: (4)
As of 2021-07-30 15:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?