develooper Front page | perl.perl6.internals | Postings from February 2001

Magic [Slightly Off-Topic... please point me to documentation]

From:
Garrett Goebel
Date:
February 6, 2001 09:46
Subject:
Magic [Slightly Off-Topic... please point me to documentation]
Message ID:
71BEC0D4E1DED3118F7A009027B120287031C6@EXCH_MISSION
From: Dan Sugalski [mailto:dan@sidhe.org]
> 
> 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



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About