M
Mintcake
This is *not* a trivial problem. If you know Perl well, please take a
bit of time to look at this.
I have the following code in a file Foo.pm
package Foo;
my @xyzzy = (1,2,3);
sub new {
my $self = bless {}, shift;
$self->ini('xyzzy');
print \@xyzzy, ' ', scalar @xyzzy;
print $self->{xyzzy}, ' ', scalar @{$self->{xyzzy}};
}
sub ini {
my ($self, $field) = @_;
eval "\$self->{$field} = \\\@$field";
}
1;
__END__
My main program is simply this:
#!/usr/local/bin/perl -l
use Foo;
new Foo;
__END__
The two lines of output are:
ARRAY(0x90edda4) 3
ARRAY(0x90edfcc) 0
It seems that there are two separate arrays, one of which is empty. I
was expecting the blessed hash to simply contain a reference to the
@xyzzy lexical declared with module scope.
If I include the package Foo code in the main program instead of a
separate module I get the expected result.
If I lose the ini() subroutime and put the eval directly in the
constructor I get the expected result.
If I don't declare @xyxxy with my or use our instead I get the
expected result.
If I add a use strict in Foo.pm and change $self->ini('xyzzy') to
$self->ini('plugh') I get the expeted error:
Can't use an undefined value as an ARRAY reference at /home/tony/lib/
Foo.pm line 11.
I'm using perl v5.8.8 and I get the some on i686-linux and Activstate
on Windoze.
bit of time to look at this.
I have the following code in a file Foo.pm
package Foo;
my @xyzzy = (1,2,3);
sub new {
my $self = bless {}, shift;
$self->ini('xyzzy');
print \@xyzzy, ' ', scalar @xyzzy;
print $self->{xyzzy}, ' ', scalar @{$self->{xyzzy}};
}
sub ini {
my ($self, $field) = @_;
eval "\$self->{$field} = \\\@$field";
}
1;
__END__
My main program is simply this:
#!/usr/local/bin/perl -l
use Foo;
new Foo;
__END__
The two lines of output are:
ARRAY(0x90edda4) 3
ARRAY(0x90edfcc) 0
It seems that there are two separate arrays, one of which is empty. I
was expecting the blessed hash to simply contain a reference to the
@xyzzy lexical declared with module scope.
If I include the package Foo code in the main program instead of a
separate module I get the expected result.
If I lose the ini() subroutime and put the eval directly in the
constructor I get the expected result.
If I don't declare @xyxxy with my or use our instead I get the
expected result.
If I add a use strict in Foo.pm and change $self->ini('xyzzy') to
$self->ini('plugh') I get the expeted error:
Can't use an undefined value as an ARRAY reference at /home/tony/lib/
Foo.pm line 11.
I'm using perl v5.8.8 and I get the some on i686-linux and Activstate
on Windoze.