[Israel.pm] Symbol table question

Shmuel Fomberg shmuelfomberg at gmail.com
Tue Aug 13 22:55:46 PDT 2013


Hi Ynon.


> I recently tried some symbol table manipulations and encountered the
> following:
>
> This code works:
> $main::{foo} = sub { warn 'M::Foo' };
> foo();
>
> But this one doesn't:
> BEGIN {
>   $main::{foo} = sub { warn 'M::Foo' };
> }
> foo();
>

Weird indeed. but why do you use '$' before main? I don't understand why
the first example works at all.
As I read it, you are assigning a code ref to the scalar $main::foo. why
calling to foo() afterwards works?

If I'd write this code, it will be:

BEGIN {
  no strict 'refs';
  *{"main::foo"} = sub { warn 'M::Foo' };
}
foo();

(and it works)

Shmuel.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20130814/2960d503/attachment.htm 


More information about the Perl mailing list