Thanks for sharing that.
Toying around a bit, I found out that the RHS of my My $my = $foo need not be an object
of class My, in fact, it can be any object, or even just a plain array reference:
package My;
use fields qw(bar);
sub new {
return fields::new(shift)
}
1;
package Foo;
use fields qw(foo);
sub new {
return fields::new(shift)
}
1;
#!/usr/bin/perl
use My;
use Foo;
use strict;
my $bar = [];
print "\$bar = $bar\n";
my My $my = $bar;
$my->{bar} = 'quux';
print "\$my = $my\n";
print "my bar field: $my->{bar}\n";
print "bar: (",@$bar,")\n";
my $foo = Foo->new();
$foo->{foo} = 'is foo';
print "(1)foo: $foo, foo field = ".$foo->{foo},"\n";
my My $bar = $foo;
$bar->{bar} = 'is bar';
print "(2) bar: $bar, bar field = ".$bar->{bar},"\n";
print "(3) foo: $foo, foo field = ".$foo->{foo},"\n";
print "(4) foo: $foo, foo field = ".$foo->{bar},"\n";
__END__
$bar = ARRAY(0x827ac28)
$my = ARRAY(0x827ac28)
my bar field: quux
bar: (quux)
(1) foo: Foo=ARRAY(0x827ab44), foo field = is foo
(2) bar: Foo=ARRAY(0x827ab44), bar field = is bar
(3) foo: Foo=ARRAY(0x827ab44), foo field = is bar
No such pseudo-hash field "bar" at my.pl line 25.
Assigning the Foo object $foo to a lexical my My $bar makes it into a My object. Or it seems to do so;
the object's body still is an array reference blessed into package Foo, while its fields seem to be of the My package.
But then, the 'My' package didnn' import field 'foo'...? Scary stuff, that, great for obfus and to annoy your cow-orkers and maintainers, and for writing hard to spot bugs...
--shmem
_($_=" "x(1<<5)."?\n".q·/)Oo. G°\ /
/\_¯/(q /
---------------------------- \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}
|