7stud has asked for the wisdom of the Perl Monks concerning the following question:
Dear Monks,
I want to know why the symbolic reference in the following code does not generate an error when using strict:
use strict; use warnings; use 5.010; sub greet { say 'hello'; } sub test { goto &{"greet"}; #SYMBOLIC REFERENCE } test(); --output:-- hello
In the following line of code, "greet" is a string:
goto &{"greet"};
and it looks like the string is being dereferenced into a sub--yet strict 'refs' is in effect.
The above is a simplified version of the following code:
{package Dog; sub new { my $class = shift; bless {Name=>'Spot', Color=>"black"}, $class; } #For auto generating the getters: sub AUTOLOAD { my $self = shift; our $AUTOLOAD; my %methods = ( color => undef, name => undef, ); if ($AUTOLOAD =~ /::(\w+)\z/ and exists $methods{$1} ) { my $data_name = ucfirst $1; { no strict 'refs'; *{$AUTOLOAD} = sub { $self->{$data_name} }; } goto &{$AUTOLOAD}; } else { use Carp; croak "No method named $AUTOLOAD"; } } } my $dog = Dog->new; say $dog->name; say $dog->color;
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: symbolic references
by james2vegas (Chaplain) on Jan 01, 2011 at 09:08 UTC | |
|
Re: symbolic references
by Anonymous Monk on Jan 01, 2011 at 07:27 UTC | |
by Anonymous Monk on Jan 01, 2011 at 07:31 UTC | |
by 7stud (Deacon) on Jan 01, 2011 at 08:33 UTC | |
by Anonymous Monk on Jan 01, 2011 at 09:12 UTC | |
by ikegami (Patriarch) on Jan 02, 2011 at 21:15 UTC | |
|
Re: symbolic references
by 7stud (Deacon) on Jan 01, 2011 at 09:32 UTC | |
by Anonymous Monk on Jan 01, 2011 at 09:56 UTC | |
by chromatic (Archbishop) on Jan 01, 2011 at 18:25 UTC |