develooper Front page | perl.fwp | Postings from July 2003

my if?

From:
Vladi Belperchinov-Shabanski
Date:
July 1, 2003 07:18
Subject:
my if?
Message ID:
20030701171509.01eabf89.cade@biscom.net

hi,

if we ignore the possibility that the following code could
ruin your day, it could be considered dark fun:

this one:

use strict;

for(1..3)
  {
  my $id = 1 if $_ == 3;
  print "[$id]\n";
  $id = 999;
  }

prints:

[]
[999]
[1]

why?!

`my $id' defines new var every time in the loop, so
expected output is:

[]
[]
[1]

the only explanation I see is that `my' is executed only once,
and $id most of the time is global (i.e. packaged) so `strict' is
fooled...

any ideas?

P! Vladi.
--
Vladi Belperchinov-Shabanski <cade@biscom.net> <cade@datamax.bg>
Personal home page at http://soul.datamax.bg/~cade
DataMax SA   http://www.datamax.bg
Welcome to this crazy world, welcome to the show that never ends...



nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About