[oak perl] Re: my operator question

Joshua Wait joshnjillwait at yahoo.com
Sat Mar 12 10:46:40 PST 2005


Great discussion of the my operator!

The example given 'my $total += $_ for @numbers;"
reminds me of why I write what I call "idiotic perl"
instead of idiomatic perl.

I don't mean that perl is idiotic, instead, I mean me,
the programmer, is idiotic! I write perl in the most
obvious straightforward manner so that the problem is
clear and I won't forget what I was trying to do
later. 

So I would have written it like so in the first place:

use strict;
my @numbers = qw(1 4 6);
my $total = 0;
foreach (@numbers) {
	$total += $_;
}
print $total;

This approach isn't necessarily faster or better, I
just like my code to be painfully obvious.

--JOSHUA

----------------
Message: 7
Date: Tue, 08 Mar 2005 07:38:47 -0500
From: Sandy Santra <santranyc at yahoo.com>
Subject: [oak perl] my operator question
To: Oakland Perl <oakland at mail.pm.org>
Message-ID:
<5.1.0.14.2.20050308073513.025be230 at pop.mail.yahoo.com>
Content-Type: text/plain; charset=us-ascii;
format=flowed

Following up on Zed's answer to George's "simple
problem,"

why does this work:

my @numbers = qw(1 4 6);
$total += $_ for @numbers;
print $total;

but not this:

use strict;
my @numbers = qw(1 4 6);
my $total = 0;
$total += $_ for @numbers;
print $total;

[Global symbol "$total" requires explicit package name
at test line 3.
Global symbol "$total" requires explicit package name
at test line 4.]

and adding "my" this way doesn't work either:

use strict;
my @numbers = qw(1 4 6);
my $total += $_ for @numbers;
print $total;

[it prints nothing]

--Sandy Santra



------------------------------

Message: 8
Date: Tue, 8 Mar 2005 08:08:34 -0800 (PST)
From: "Jon Seidel, CMC" <jseidel at edpci.com>
Subject: Re: [oak perl] my operator question
To: "Oakland Perl Mongers" <oakland at pm.org>
Message-ID:
<52189.63.192.200.250.1110298114.squirrel at 63.192.200.250>
Content-Type: text/plain;charset=iso-8859-1

I'm guessing it has something to do with the "for
numbers" construct
actually expecting an array context...?

Good question...jon

> Following up on Zed's answer to George's "simple
problem,"
>
> why does this work:
>
> my @numbers = qw(1 4 6);
> $total += $_ for @numbers;
> print $total;
>
> but not this:
>
> use strict;
> my @numbers = qw(1 4 6);
> my $total = 0;
> $total += $_ for @numbers;
> print $total;
>
> [Global symbol "$total" requires explicit package
name at test line 
3.
> Global symbol "$total" requires explicit package
name at test line 
4.]
>
> and adding "my" this way doesn't work either:
>
> use strict;
> my @numbers = qw(1 4 6);
> my $total += $_ for @numbers;
> print $total;
>
> [it prints nothing]
>
> --Sandy Santra
>
> _______________________________________________
> Oakland mailing list
> Oakland at pm.org
> http://mail.pm.org/mailman/listinfo/oakland
>



-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-

      Connecting Business and Technology

Jon Seidel, CMC                   +1-510-530-6314
EDP Consulting, Inc.                www.edpci.com

CMC (Certified Management Consultant) is a
certification mark awarded 
by
the Institute of Management Consultants USA and
represents evidence of 
the
highest standards of consulting and adherence to the
ethical canons of 
the
profession. Less than 1% of all consultants have
achieved this level of
performance. See www.imcusa.org/hireacmc.acgi.



------------------------------

Message: 9
Date: Tue, 8 Mar 2005 09:52:29 -0800
From: Zed Lopez <zed.lopez at gmail.com>
Subject: Re: [oak perl] my operator question
To: Oakland Perl Mongers <oakland at pm.org>
Message-ID:
<83a996de05030809525038c081 at mail.gmail.com>
Content-Type: text/plain; charset=US-ASCII

On Tue, 08 Mar 2005 07:38:47 -0500, Sandy Santra
<santranyc at yahoo.com> 
wrote: 
> why does this work:
> but not this:
> 
> use strict;
> my @numbers = qw(1 4 6);
> my $total = 0;
> $total += $_ for @numbers;
> print $total;
> 
> [Global symbol "$total" requires explicit package
name at test line 
3.
> Global symbol "$total" requires explicit package
name at test line 
4.]

It works for me on Perl 5.8.5 on Fedora Core 3.

> and adding "my" this way doesn't work either:
> 
> use strict;
> my @numbers = qw(1 4 6);
> my $total += $_ for @numbers;
> print $total;

You shouldn't do end-of-line modifiers (if, unless,
while, for) with
my statement. You're using a new, undef'd $total each
time through the
for loop. And it's undef after the loop, too. It seems
to be the
equivalent of:

my @numbers = qw(1 4 6);
my $total;
for (@numbers) {
  my $total;
  $total += $_;
}
print $total;

That is, while you do succeed in declaring a lexically
scoped varable
named $total in the main program, it's as if there
were a different
lexically scoped variable named $total for the
purposes of the for
loop.

Zed


------------------------------

Message: 10
Date: Tue, 8 Mar 2005 10:00:54 -0800
From: Zed Lopez <zed.lopez at gmail.com>
Subject: Re: [oak perl] my operator question
To: Oakland Perl Mongers <oakland at pm.org>
Message-ID:
<83a996de0503081000732e3fca at mail.gmail.com>
Content-Type: text/plain; charset=US-ASCII

On Tue, 8 Mar 2005 09:52:29 -0800, Zed Lopez
<zed.lopez at gmail.com> 
wrote:
> You shouldn't do end-of-line modifiers (if, unless,
while, for) with
> my statement. You're using a new, undef'd $total
each time through 
the
> for loop. And it's undef after the loop, too. It
seems to be the
> equivalent of:
> 
> my @numbers = qw(1 4 6);
> my $total;
> for (@numbers) {
>   my $total;
>   $total += $_;
> }
> print $total;

Oops. I contradicted myself there -- if it's
equivalent to the latter,
it's not the case that you're using a new, undef'd
$total each time
through the for loop. I really don't know what Perl
might be doing
inside that for loop; I only know the external
behavior.

Anyone have a clue what's _really_ happening?


------------------------------

Message: 11
Date: Tue, 08 Mar 2005 11:07:41 -0800
From: Steve Fink <sfink at reactrix.com>
Subject: Re: [oak perl] my operator question
To: Zed Lopez <zed.lopez at gmail.com>, Oakland Perl
Mongers
        <oakland at pm.org>
Message-ID: <422DF7FD.5040609 at reactrix.com>
Content-Type: text/plain; charset=us-ascii;
format=flowed

Zed Lopez wrote:
> On Tue, 8 Mar 2005 09:52:29 -0800, Zed Lopez
<zed.lopez at gmail.com> 
wrote:
> 
>>You shouldn't do end-of-line modifiers (if, unless,
while, for) with
>>my statement. You're using a new, undef'd $total
each time through 
the
>>for loop. And it's undef after the loop, too. It
seems to be the
>>equivalent of:
>>
>>my @numbers = qw(1 4 6);
>>my $total;
>>for (@numbers) {
>>  my $total;
>>  $total += $_;
>>}
>>print $total;
> 
> 
> Oops. I contradicted myself there -- if it's
equivalent to the 
latter,
> it's not the case that you're using a new, undef'd
$total each time
> through the for loop. I really don't know what Perl
might be doing
> inside that for loop; I only know the external
behavior.

Why not? It seems like your description was correct.
The inner $total 
is 
lexically scoped to the for block, so it starts out
undef, gets 
assigned 
to undef plus each of the numbers (equiv to the
number), then goes out 
of scope and is forgotten before the next iteration.
It's confusing 
that 
it's the same $total during each iteration, but the
value gets reset. 
Still, that's what I would expect from:

   for (1..3) {
     print "Iteration $_\n";
     if (42 > 0) {
       my $x;
       $x += $_;
       print "Yow!\n" if $x > 3;
     }
   }

I wouldn't expect that to print "Yow!", since $x
really shouldn't 
survive outside of that if statement.

The exact translation of the original would actually
be

   for (@numbers) {
     my $total += $_;
   }

as can be seen with

   perl -MO=Deparse -e 'my $total += $_ for (1..3)'

but I think it's the same.

The confusing bit is that the variable really is the
same:

   perl -le 'for (1..3) { my $x; print \$x; }'

Hmm... so what does this do?

   perl -le 'for (1..3) { if (1) { my $x; my $y = \$x;
$$y += $_; print 
"$y=$$y" } }'

I guess that means that the value really is reset at
runtime when the 
'my' is encountered? Yes, it sounds like it, from
reading through 
perlsub, when it talks about 'my' having both
compile-time and runtime 
effects.

---------------------
ArcSource Consulting
"Providing Computer Support in the San Francisco Bay Area"
http://www.arcsource.net


		
__________________________________ 
Do you Yahoo!? 
Yahoo! Small Business - Try our new resources site!
http://smallbusiness.yahoo.com/resources/ 


More information about the Oakland mailing list