develooper Front page | perl.beginners | Postings from February 2009

question on 2 sub modules on the same scope

Thread Next
From:
itshardtogetone
Date:
February 9, 2009 10:55
Subject:
question on 2 sub modules on the same scope
Message ID:
COL0-DAV57485243C1904270DF365B7BC0@phx.gbl
Hi,
Looking at the script below, can someone explain why the final output is "Final = 1" instead of "Final = 5".
I thought at the end of the while loop, $a_ctr is 5, this value is then read by the sub module &data() and this value of 5 is then passed on to sub publish before its being printed out. Thanks



#!/usr/bin/perl
use strict;
use warnings;

{
my $a_ctr = 0;

 sub data {
       $a_ctr = @_;
    }

  sub publish{
     print "Final = $a_ctr\n";
    }
}
#########################
my $a = 5;
my $a_ctr = 0;

while ($a_ctr < $a){
 $a_ctr ++;
    print "a_ctr = $a_ctr\n";
    &data($a_ctr);
}
&publish();
Thread Next


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