Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > What does `my' do?!

Reply
Thread Tools

What does `my' do?!

 
 
Ilya Zakharevich
Guest
Posts: n/a
 
      04-16-2009
I managed to confuse myself in a teaspoon... Consider

perl -wle "my $x; BEGIN{ $x=12 } print $x"
12

I would expect it to warn that $x is uninitialized, and print
nothing. The reasoning goes like that:

AT COMPILE TIME:
`my' is seen; after this $x is considered as lexical
BEGIN is compiled and executed; $x becomes 12
`print' is compiled

AT RUN TIME
`my' is executed.[*] Variable $x becomes undef
`print' is executed

Apparently,[*]-action is not executed in this context. The question
is: why? Is it documented?

Puzzled,
Ilya
 
Reply With Quote
 
 
 
 
Frank Seitz
Guest
Posts: n/a
 
      04-16-2009
Ilya Zakharevich wrote:
> I managed to confuse myself in a teaspoon... Consider
>
> perl -wle "my $x; BEGIN{ $x=12 } print $x"
> 12
>
> I would expect it to warn that $x is uninitialized, and print
> nothing. The reasoning goes like that:
>
> AT COMPILE TIME:
> `my' is seen; after this $x is considered as lexical
> BEGIN is compiled and executed; $x becomes 12
> `print' is compiled
>
> AT RUN TIME
> `my' is executed.[*] Variable $x becomes undef
> `print' is executed
>
> Apparently,[*]-action is not executed in this context. The question
> is: why? Is it documented?


I think, my() creates a variable at compile time but it doesn't
change its value at run time. To achieve the desired effect,
you must initialze $x yourself:

perl -wle 'my $x = 13; BEGIN{ $x=12 } print $x'
13

Frank
--
Dipl.-Inform. Frank Seitz; http://www.fseitz.de/
Anwendungen für Ihr Internet und Intranet
Tel: 04103/180301; Fax: -02; Industriestr. 31, 22880 Wedel
 
Reply With Quote
 
 
 
 
Peter Makholm
Guest
Posts: n/a
 
      04-16-2009
Ilya Zakharevich <nospam-> writes:

> Apparently,[*]-action is not executed in this context. The question
> is: why? Is it documented?


perlsub(1) got the following under the heading 'Private Variables via
my()':

A "my" has both a compile-time and a run-time effect. At
compile time, the compiler takes notice of it. The principal
usefulness of this is to quiet "use strict 'vars'", but it is
also essential for generation of closures as detailed in
perlref. Actual initialization is delayed until run time,
though, so it gets executed at the appropriate time, such as
each time through a loop, for example.

Not that perlsub(1) is the most intuitive place to look for the
documentation for 'my', but at least 'perldoc -f my' referes to it, so
it should be findable.

//Makholm
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      04-16-2009
On Thu, 16 Apr 2009 14:15:40 GMT, Ilya Zakharevich <nospam-> wrote:

>I managed to confuse myself in a teaspoon... Consider
>
> perl -wle "my $x; BEGIN{ $x=12 } print $x"
> 12
>
>I would expect it to warn that $x is uninitialized, and print
>nothing. The reasoning goes like that:
>
> AT COMPILE TIME:
> `my' is seen; after this $x is considered as lexical
> BEGIN is compiled and executed; $x becomes 12
> `print' is compiled
>
> AT RUN TIME
> `my' is executed.[*] Variable $x becomes undef
> `print' is executed
>
>Apparently,[*]-action is not executed in this context. The question
>is: why? Is it documented?
>
>Puzzled,
>Ilya


perl -wle " BEGIN{ $x=12; print $x; } print 'here'; my $x=99; print $x"
12
here
99
------------
perl -wle " BEGIN{ $x=12; print $x; } print 'here'; my $x; print $x"
12
here
Use of uninitialized value in print at -e line 1.
------------

perl -wle "my $x; undef $x; print $x; BEGIN{ $x=12; } print 'here'; print $x"
Use of uninitialized value in print at -e line 1.

here
Use of uninitialized value in print at -e line 1.
------------

It appears declarations used by and past the BEGIN block is re-initialized
after block execution ('my' is re-examined) while at run-time, assignments
are processed from the beginning.

This makes sense since 'use Module' initializes variables and runs code
at compile time.

But, unusual behavior.

-sln
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      04-16-2009
On Thu, 16 Apr 2009 14:15:40 GMT, Ilya Zakharevich <nospam-> wrote:

>I managed to confuse myself in a teaspoon... Consider
>
> perl -wle "my $x; BEGIN{ $x=12 } print $x"
> 12
>
>I would expect it to warn that $x is uninitialized, and print
>nothing. The reasoning goes like that:
>
> AT COMPILE TIME:
> `my' is seen; after this $x is considered as lexical
> BEGIN is compiled and executed; $x becomes 12
> `print' is compiled
>
> AT RUN TIME
> `my' is executed.[*] Variable $x becomes undef
> `print' is executed
>
>Apparently,[*]-action is not executed in this context. The question
>is: why? Is it documented?
>
>Puzzled,
>Ilya



perl -wle "BEGIN{ $x=12; } print 'here'; print $x; my $x;"
here
12
---------------
perl -wle "BEGIN{ $x=12; } print 'here'; print $x;"
here
12

Looks like a strange issue. Kind of auto-creates the lexical in the
case of the last one.

 
Reply With Quote
 
Tim McDaniel
Guest
Posts: n/a
 
      04-16-2009
In article <slrnguefcc.fpg.nospam->,
Ilya Zakharevich <nospam-> wrote:
>I managed to confuse myself in a teaspoon... Consider
>
> perl -wle "my $x; BEGIN{ $x=12 } print $x"
> 12
>
>I would expect it to warn that $x is uninitialized, and print
>nothing. The reasoning goes like that:
>
> AT COMPILE TIME:
> `my' is seen; after this $x is considered as lexical
> BEGIN is compiled and executed; $x becomes 12
> `print' is compiled
>
> AT RUN TIME
> `my' is executed.[*] Variable $x becomes undef
> `print' is executed
>
>Apparently,[*]-action is not executed in this context. The question
>is: why? Is it documented?


In "man perlsub", under "Private Variables via my()", I see both

The parameter list to my() may be assigned to if desired, which
allows you to initialize your variables. (If no initializer is
given for a particular variable, it is created with the
undefined value.)

and

A "my" has both a compile-time and a run-time effect. At
compile time, the compiler takes notice of it. The principal
usefulness of this is to quiet "use strict 'vars'", but it is
also essential for generation of closures as detailed in
perlref. Actual initialization is delayed until run time,
though, so it gets executed at the appropriate time, such as
each time through a loop, for example.

I think "Actual initialization" refers to explicit assignment via "=",
so if there's no initialization, there's no run-time assignment.
So I think
(If no initializer is given for a particular variable, it is
created with the undefined value.)
is inaccurate: it's always created undef.

So I'm thinking the the sequence is:
- compile time:
= variable is created
= since it has to have SOME value in Perl, it's set to undef
- run time:
= if an explicit initializer is provided, the assignment is done
= so if no explicit initializer is provided, it is unchanged

That is, that
my $y = 123;
is equivalent to
my $y; BEGIN { undef $y; }
$y = 123; # assigned at run time
$y;
and
my $z;
is
my $z; BEGIN { undef $z; }
$z;

That seems consistent with the examples and with
perl -wle 'my $x = 13; BEGIN{ print "in begin ", $x; $x=12 } print $x'
Use of uninitialized value $x in print at -e line 1.
in begin
^ both showing that it's created undef
13

--
Tim McDaniel,
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      04-16-2009
Ilya Zakharevich <nospam-> wrote:
> I managed to confuse myself in a teaspoon... Consider
>
> perl -wle "my $x; BEGIN{ $x=12 } print $x"
> 12



You develop on Windows?


> I would expect it to warn that $x is uninitialized, and print
> nothing. The reasoning goes like that:
>
> AT COMPILE TIME:
> `my' is seen; after this $x is considered as lexical
> BEGIN is compiled and executed; $x becomes 12
> `print' is compiled
>
> AT RUN TIME
> `my' is executed.[*] Variable $x becomes undef
> `print' is executed
>
> Apparently,[*]-action is not executed in this context. The question
> is: why?



Because $x is not being assigned to in the my(), I guess.

i.e. "no initializer is given" (see below).


> Is it documented?



I think so, if you can divine that "created" means "at compile time"...

From perlsub.pod (with my emphasis):

The parameter list to my() may be assigned to if desired, which allows you
to initialize your variables. (If no initializer is given for a
particular variable, it is created with the undefined value.)
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

and since "creation" is in my()'s compile-time component, that is when
it is given the undef value.

If we _do_ give an initializer:

perl -wle 'my $x=undef; BEGIN{ $x=12 } print $x'

then things match up with your expectations.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      04-16-2009
<> wrote:

[ snip all but "the last one" ]

> ---------------
> perl -wle "BEGIN{ $x=12; } print 'here'; print $x;"
> here
> 12
>
> Looks like a strange issue. Kind of auto-creates the lexical in the
> case of the last one.



There is no lexical in that one. There are only package variables there.


--
Tad McClellan
email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
 
Reply With Quote
 
sln@netherlands.com
Guest
Posts: n/a
 
      04-16-2009
On Thu, 16 Apr 2009 12:55:20 -0500, Tad J McClellan <> wrote:

>Ilya Zakharevich <nospam-> wrote:
>> I managed to confuse myself in a teaspoon... Consider
>>
>> perl -wle "my $x; BEGIN{ $x=12 } print $x"
>> 12

>
>
>You develop on Windows?
>
>
>> I would expect it to warn that $x is uninitialized, and print
>> nothing. The reasoning goes like that:
>>
>> AT COMPILE TIME:
>> `my' is seen; after this $x is considered as lexical
>> BEGIN is compiled and executed; $x becomes 12
>> `print' is compiled
>>
>> AT RUN TIME
>> `my' is executed.[*] Variable $x becomes undef
>> `print' is executed
>>
>> Apparently,[*]-action is not executed in this context. The question
>> is: why?

>
>
>Because $x is not being assigned to in the my(), I guess.
>
>i.e. "no initializer is given" (see below).
>
>
>> Is it documented?

>
>
>I think so, if you can divine that "created" means "at compile time"...
>
>From perlsub.pod (with my emphasis):
>
> The parameter list to my() may be assigned to if desired, which allows you
> to initialize your variables. (If no initializer is given for a
> particular variable, it is created with the undefined value.)
> ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
>
>and since "creation" is in my()'s compile-time component, that is when
>it is given the undef value.
>
>If we _do_ give an initializer:
>
> perl -wle 'my $x=undef; BEGIN{ $x=12 } print $x'
>
>then things match up with your expectations.

^^^^^
Not really...

perl -wle "my $x=undef; BEGIN{ $x=12 } print $x"
12
------------------
perl -wle "my $x; undef($x); BEGIN{ $x=12 } print $x"
Use of uninitialized value in print at -e line 1.

-sln

 
Reply With Quote
 
Ilya Zakharevich
Guest
Posts: n/a
 
      04-16-2009
On 2009-04-16, <> wrote:
> On Thu, 16 Apr 2009 14:15:40 GMT, Ilya Zakharevich <nospam-> wrote:
> perl -wle " BEGIN{ $x=12; print $x; } print 'here'; my $x=99; print $x"
> 12
> here
> 99
> ------------
> perl -wle " BEGIN{ $x=12; print $x; } print 'here'; my $x; print $x"
> 12
> here
> Use of uninitialized value in print at -e line 1.


Keep in mind that in these examples you have DIFFERENT variables, all
named $x.

Yours,
Ilya
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
.NET 2.0 ASPx Page does not load, but HTM does prabhupr@hotmail.com ASP .Net 1 02-08-2006 12:57 PM
Button OnClick does not fire on first postback, but does on second Janet Collins ASP .Net 0 01-13-2006 10:08 PM
Does the 2.0 Framework come out when Visual Studio .NET 2005 does? needin4mation@gmail.com ASP .Net 3 10-07-2005 12:55 AM
CS0234 Global does not exist ... but it genuinely does Bill Johnson ASP .Net 0 07-08-2005 06:34 PM
Does no one else think microsoft does a poor job? =?Utf-8?B?SmVyZW15IEx1bmRncmVu?= Wireless Networking 2 11-20-2004 12:17 AM



Advertisments