[Israel.pm] Symbol table question

ynon perek ynonperek at gmail.com
Wed Aug 14 10:18:22 PDT 2013


Hi Shmuel and Thanks,

It's an existing code. I suspect $main:: is some kind of alias to
*{"main::"}.
I also found the same trick does not work for other namespaces.

So now I'm just trying to understand what is $main:: and where did it come
from ...


On 14 August 2013 08:55, Shmuel Fomberg <shmuelfomberg at gmail.com> wrote:

> 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.
>
>
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://mail.perl.org.il/mailman/listinfo/perl
>



-- 

כותב הרצאות ? מדבר מול קהל ? הבלוג שלי לומד
לדבר<http://publicspeakr.blogspot.com/>כתוב במיוחד בשבילך.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mail.perl.org.il/pipermail/perl/attachments/20130814/0b1c7ac7/attachment.htm 


More information about the Perl mailing list