Re: Magic [Slightly Off-Topic... please point me to documentation]
On Tue, 6 Feb 2001 17:53:17 -0200, Branden wrote: >It appears you're blessing one reference and returning another... like > >sub new { >my $key; >my $a = \$key; >my $b = \$key; >bless $a; >return $b; >} > >I think the problem is not with the overloading magic, but with the code >snippet... A recent thread on comp.lang.perl.misc discussed how bless() works with the reference, but alledgedly, it's the underlying thing that gets blessed, not the reference itself. my $a = \$x; my $b = \$x; bless $a, 'FOO'; print $b; --> FOO=SCALAR(0x8a652e4) It sure looks that they're right. Oh, this is perl 5.6.0. -- Bart.
Re: Magic [Slightly Off-Topic... please point me to documentation]
Garrett Goebel wrote: > > > package bar; > > > @ISA = qw(foo); > > > sub new { bless \my $key; \$key } > > It appears you're blessing one reference and returning another... like sub new { my $key; my $a = \$key; my $b = \$key; bless $a; return $b; } I think the problem is not with the overloading magic, but with the code snippet... - Branden
Re: Magic [Slightly Off-Topic... please point me to documentation ]
On Tue, Feb 06, 2001 at 01:38:56PM -0600, Garrett Goebel wrote: > I'm sorry... I didn't mean to start an off-topic thread. This is currently being discussed on p5p, so you might want to take it over there. > Is there really no substantial documentation anywhere on magic? Not yet. This looks like something that we can fix. What I'll try and do, although I make no promises, is make sure that either Tim or I [1] finds out all about magic, writes up some documentation and pushes some of it back to perlguts.pod, since it looks like there is definitely a crying need for people to understand this. > I suppose that is why it is called magic, eh? Yup! [1] Tim Jenness has written a section about magic in a book we're writing on Perl and C interaction. However, what we've got at the moment isn't much more than what's in perlguts. -- "Darkly hinting of head hitting desk" -- Megahal (trained on asr), 1998-11-05
RE: Magic [Slightly Off-Topic... please point me to documentation]
From: Branden [mailto:[EMAIL PROTECTED]] > > try to define a method in package bar and try to call it > from $bar, like $bar->foo. Won't work either, you have > to ${$bar}->foo. Overloading should loose the magic > in the same sense that the method should not be called. No, $bar->asString and $baz->asString both work. They produce: Hello World > > package bar; > > @ISA = qw(foo); > > sub new { bless \my $key; \$key } > > You return a reference to the object... No, it returns an object reference... Try it. > Try printing $$bar, it will work... No, it doesn't. $$bar is an undefined scalar. sub new { my $ref = \ my $key; bless $ref; $ref } and sub new { bless \ my $key; \$key; } Both return an object reference. The former has magic, the latter does not. I'm sorry... I didn't mean to start an off-topic thread. Unless someone can find a way to shoe-horn in a discussion of language or internals magic handling for Perl6? Is there really no substantial documentation anywhere on magic? I suppose that is why it is called magic, eh? package foo; use overload '""' => 'asString'; sub asString { 'Hello World' } package bar; @ISA = qw(foo); sub new { bless \my $key; \$key } package baz; @ISA = qw(foo); sub new { my $ref = \ my $key; bless $ref; $ref } package main; my ($bar, $baz); $bar = bar::->new(); $baz = baz::->new(); print "\$bar$bar\n"; print "\$baz$baz\n"; print "\$bar->asString ", $bar->asString, "\n"; print "\$baz->asString ", $baz->asString, "\n"; print "\$\$bar ", $$bar, "\n"; Results In: $barbar=SCALAR(0x1bbfc9c) $bazHello World $bar->asString Hello World $baz->asString Hello World $$bar
Re: Magic [Slightly Off-Topic... please point me to documentation]
Magic [Slightly Off-Topic... please point me to documentation]Garrett Goebel wrote: > I was recently bit by overloading magic in the Class::Context > generic constructor which the following code illustrates. If > you return "another" reference to a scalar which was blessed, > instead of the blessed reference, then you loose your magic. > I'm trying to keep Class::Contract (which I'm maintaining for > Damian) overload friendly... But Perl magic on my map is > labelled "there be dragons". > Only blessed objects can be overloaded, not references. Only a class can have overloading behaviour, and a plain reference belongs to no class, so it should have no overloading behaviour. Contrast this use of \$key with having [1,2,3]. Do you think this array reference should have any overloading behaviour? Or else, try to define a method in package bar and try to call it from $bar, like $bar->foo. Won't work either, you have to ${$bar}->foo. Overloading should loose the magic in the same sense that the method should not be called. > package bar; > @ISA = qw(foo); > sub new { bless \my $key; \$key } You return a reference to the object... > package main; > my ($bar, $baz); > $bar = bar::->new(); > $baz = baz::->new(); > > print "bar $bar\n"; > print "baz $baz\n"; Try printing $$bar, it will work... - Branden
Re: Magic [Slightly Off-Topic... please point me to documentation]
At 11:46 AM 2/6/2001 -0600, Garrett Goebel wrote: >From: Dan Sugalski [mailto:[EMAIL PROTECTED]] > > > > No, you attach the magic to a value. Perl just doesn't copy > > magic when it copies data. Whether this is a good thing or > > not is up in the air. (Half the time I want it to, the other > > half I don't...) > >Is there a good discussion of magic, copying magic etc. in the core perl >documentation? Or elsewhere for that matter? Any documentation, pointers, >general tips, etc. would be greatly appreciated. Nope. >I was recently bit by overloading magic in the Class::Context generic >constructor which the following code illustrates. If you return "another" >reference to a scalar which was blessed, instead of the blessed reference, >then you loose your magic. I'm trying to keep Class::Contract (which I'm >maintaining for Damian) overload friendly... But Perl magic on my map is >labelled "there be dragons". Dragons is right. It's a dicey area, and past that most extensions handle magic values flat-out wrong. (Including most of mine, so I've no room to talk...) Dan --"it's like this"--- Dan Sugalski even samurai [EMAIL PROTECTED] have teddy bears and even teddy bears get drunk
Magic [Slightly Off-Topic... please point me to documentation]
From: Dan Sugalski [mailto:[EMAIL PROTECTED]] > > No, you attach the magic to a value. Perl just doesn't copy > magic when it copies data. Whether this is a good thing or > not is up in the air. (Half the time I want it to, the other > half I don't...) Is there a good discussion of magic, copying magic etc. in the core perl documentation? Or elsewhere for that matter? Any documentation, pointers, general tips, etc. would be greatly appreciated. I was recently bit by overloading magic in the Class::Context generic constructor which the following code illustrates. If you return "another" reference to a scalar which was blessed, instead of the blessed reference, then you loose your magic. I'm trying to keep Class::Contract (which I'm maintaining for Damian) overload friendly... But Perl magic on my map is labelled "there be dragons". package foo; use overload '""' => 'asString'; sub asString { 'Hello World' } package bar; @ISA = qw(foo); sub new { bless \my $key; \$key } package baz; @ISA = qw(foo); sub new { my $ref = \ my $key; bless $ref; $ref } package main; my ($bar, $baz); $bar = bar::->new(); $baz = baz::->new(); print "bar $bar\n"; print "baz $baz\n"; Results In: bar bar=SCALAR(0x1bbfc9c) baz Hello World