[Israel.pm] use constant and our

Adriano Ferreira a.r.ferreira at gmail.com
Wed Mar 4 16:16:10 PST 2009


On Wed, Mar 4, 2009 at 8:59 PM, Gaal Yahas <gaal at forum2.org> wrote:
> Minor correction: "our" is lexically scoped, so you need:

No, it is not.

$ perl -le ' our $foo = 3; { our $foo = 4; } print $foo '
4

"our" is package scoped. If you refer to the same package/our variable
name, it is the same variable.

> our @list;
>
> BEGIN {
>  @list = qw(one two three);
> }
>
> use constant .......;
>
> On Thu, Mar 5, 2009 at 12:07 AM, Shmuel Fomberg <semuelf at 012.net.il> wrote:
>> Hi All.
>>
>> Today I've seen a weird behavior in a program. I had something like:
>>
>> our @list = qw{one two three};
>>
>> use constant VAR => {
>>    key => "value",
>>    map( { ( $_ => 16 ) } @list ),
>>    key2 => "value2",
>> };
>>
>> To my surprise, VAR contained only key and key2.
>> I theorized that it is because "use constant" is set in compile time,
>> and @list is empty in that time. @list is populated in run-time, but
>> that is too late for VAR.
>>
>> Am I right or is there other reason for this?
>>
>> Tested on Perl 5.6.
>>
>> Shmuel.
>> _______________________________________________
>> Perl mailing list
>> Perl at perl.org.il
>> http://perl.org.il/mailman/listinfo/perl
>>
>
>
>
> --
> Gaal Yahas <gaal at forum2.org>
> http://gaal.livejournal.com/
> _______________________________________________
> Perl mailing list
> Perl at perl.org.il
> http://perl.org.il/mailman/listinfo/perl
>



More information about the Perl mailing list