Loading ...
Sorry, an error occurred while loading the content.

RE: [PBML] use strict; within loop why " my auto-increment " dont increase.

Expand Messages
  • Oliver Manickum
    When you use the my operator, you define your variable, resetting any data that was in it. try changing your code logic to suit ur need. - Olly ... From:
    Message 1 of 6 , Jan 2, 2002
    • 0 Attachment
      When you use the 'my' operator, you define your variable,
      resetting any data that was in it.

      try changing your code logic to suit ur need.

      - Olly



      -----Original Message-----
      From: Leon [mailto:eventualdeath@...]
      Sent: Wednesday, January 02, 2002 9:51 AM
      To: perl-beginner@yahoogroups.com
      Subject: [PBML] use strict; within loop why " my auto-increment " dont
      increase.


      Hi Members,

      My question :- Could members please help me to understand why $b is not
      incremented within the while loop below.

      Just a comment :-
      Originally, I always thought that in using strict, it is mandatory that
      I
      declare all new variables with a 'my'. Correct me if I am wrong, however
      now
      I dont think so as this will prevent us from globalising a variable
      within a
      block, I have to thank Michael R. Wolf for this.

      Thank You.

      use strict;
      while ($a<10){
      $a++;
      my $b++; # why it did not increment?
      print "$a\n";
      print "$b\n";
      };
      print "\$a = $a\n"; # $a = 10 as expected.
      print "\$b = $b\n"; # $b returns undef, I understand this.



      _________________________________________________________

      Do You Yahoo!?

      Get your free @... address at http://mail.yahoo.com





      Unsubscribing info is here:
      http://help.yahoo.com/help/us/groups/groups-32.html

      Your use of Yahoo! Groups is subject to
      http://docs.yahoo.com/info/terms/
    • Charles K. Clarkson
      Leon ... $a and $b are special global variables in perl. They are used in sort . They can also be referred to as $main::a and
      Message 2 of 6 , Jan 2, 2002
      • 0 Attachment
        "Leon" <eventualdeath@...>

        : Hi Members,
        :
        : My question :- Could members please help me to understand
        : why $b is not incremented within the while loop below.

        $a and $b are special global variables in perl. They
        are used in 'sort'. They can also be referred to as
        $main::a and $main::b. Let's just do the replacement as
        perl sees it when using my:

        while ($main::a < 10){
        $main::a++;
        my $b++; # why it did not increment?
        print "\$main::a = $main::a\t";
        print "\$b = $b\n";
        };

        print "\n\$main::a = $main::a\n";
        print "\$main::b = $main::b\n";

        which prints:
        $main::a = 1 $b = 1
        $main::a = 2 $b = 1
        $main::a = 3 $b = 1
        $main::a = 4 $b = 1
        $main::a = 5 $b = 1
        $main::a = 6 $b = 1
        $main::a = 7 $b = 1
        $main::a = 8 $b = 1
        $main::a = 9 $b = 1
        $main::a = 10 $b = 1

        $main::a = 10
        $main::b =

        If you turn on warnings, you'll find that $main::b was
        used only once.

        : Just a comment :-
        : Originally, I always thought that in using strict, it is
        : mandatory that I declare all new variables with a 'my'.
        : Correct me if I am wrong, however now I dont think so as
        : this will prevent us from globalising a variable within
        : a block, I have to thank Michael R. Wolf for this.

        You're wrong. Consider yourself corrected. We'll waive
        the standing in the corner bit for now.

        It is mandatory. Except for buil-in perl variables,
        like $_ and $., and for special perl variables that are not
        built-in, like $a and $b.
        $a and $b are special in perl under strict. $a and $b
        are used to pass values to 'sort'. For instance:

        my @sorted = sort { $a <=> $b } qw/1 3 5 2 3 4/;

        doesn't raise any errors, even though $a and $b were
        not declared using 'my'.

        : use strict;
        : while ($a<10){
        : $a++;
        : my $b++; # why it did not increment?
        : print "$a\n";
        : print "$b\n";
        : };
        : print "\$a = $a\n"; # $a = 10 as expected.
        : print "\$b = $b\n"; # $b returns undef, I understand this.

        What we need to do in order to find out what happens
        under strict using "normal" variables is to substitute $c
        and $d for $a and $b in the example above:

        use strict;
        while ($c < 10){
        $c++;
        my $d++; # why it did not increment?
        print "\$c = $c\t";
        print "\$d = $d\n";
        };
        print "\$c = $c\n"; # $c = 10 as expected.
        print "\$d = $d\n"; # $d returns undef, I understand this.
        __END__

        Which gives us the more expected result:

        Global symbol "$c" requires explicit package name at aaa.pl line 14.
        Global symbol "$c" requires explicit package name at aaa.pl line 15.
        Global symbol "$c" requires explicit package name at aaa.pl line 17.
        Global symbol "$c" requires explicit package name at aaa.pl line 20.
        Global symbol "$d" requires explicit package name at aaa.pl line 21.
        Execution of aaa.pl aborted due to compilation errors.


        The moral of the story is:

        Use nice descriptive names for your variables, like $c and $d.

        :)


        HTH,
        Charles K. Clarkson
        Clarkson Energy Homes, Inc.
        254 968-8328


        I refuse to join any club that would have me as a member.
        - Groucho Marx
      • Leon
        ... _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com
        Message 3 of 6 , Jan 2, 2002
        • 0 Attachment
          ----- Original Message -----
          From: "Oliver Manickum" <oliver@...>
          To: <perl-beginner@yahoogroups.com>
          > When you use the 'my' operator, you define your variable,
          > resetting any data that was in it.

          It took me quite a while to figure out what you were trying to say. But my
          instinct tells me that what you were trying to say is that with each
          iteration, the variable my $b++ was resetted, hence no increment (remains as
          1), Members, please correct me if I've read Oliver wrongly.

          Normally if we declare a my $variable on a previous my $variable, the
          previous value is taken over by the latest value like this eg :-
          my $d = 100;
          my $d = 2;
          print $d; # value = 2

          But in the following script, it would be of interest to new Learners, like
          me, to note that " use strict " sees 2 different $b in the following script
          :-

          #!d:\perl\bin\perl.exe -w
          use strict;
          use CGI::Carp qw(fatalsToBrowser);
          print "content-type: text/plain\n\n";

          while ($b<3){
          $b++; # lets call this OUTER $b
          my $b++; # this $b does not take over the value of the OUTER $b
          # even though it is on the same block
          while ($b < 5 ) {
          $b++;
          print "2nd while \$b = $b\n";
          };
          };

          ---- end of my msg -----


          > -----Original Message-----
          > From: Leon [mailto:eventualdeath@...]
          > Sent: Wednesday, January 02, 2002 9:51 AM
          > To: perl-beginner@yahoogroups.com
          > Subject: [PBML] use strict; within loop why " my auto-increment " dont
          > increase.
          >
          >
          > Hi Members,
          >
          > My question :- Could members please help me to understand why $b is not
          > incremented within the while loop below.
          >
          > Just a comment :-
          > Originally, I always thought that in using strict, it is mandatory that
          > I
          > declare all new variables with a 'my'. Correct me if I am wrong, however
          > now
          > I dont think so as this will prevent us from globalising a variable
          > within a
          > block, I have to thank Michael R. Wolf for this.
          >
          > Thank You.
          >
          > use strict;
          > while ($a<10){
          > $a++;
          > my $b++; # why it did not increment?
          > print "$a\n";
          > print "$b\n";
          > };
          > print "\$a = $a\n"; # $a = 10 as expected.
          > print "\$b = $b\n"; # $b returns undef, I understand this.





          _________________________________________________________
          Do You Yahoo!?
          Get your free @... address at http://mail.yahoo.com
        • Leon
          ... _________________________________________________________ Do You Yahoo!? Get your free @yahoo.com address at http://mail.yahoo.com
          Message 4 of 6 , Jan 2, 2002
          • 0 Attachment
            ----- Original Message -----
            From: "Charles K. Clarkson" <cclarkson@...>
            > $a and $b are special global variables in perl. They
            > are used in 'sort'. They can also be referred to as
            > $main::a and $main::b.

            Previously, somebody pointed to me that perl have special reserved words
            such as print, chomp, exists and so on.... , now I realised that perl have
            special variables too.

            To avoid using barewords, warnings have taught me to use UPPERCASE or put
            quotes.
            As I do not know which variables are reserved, I just wonder how to avoid
            using them.

            > : Just a comment :-
            > : Originally, I always thought that in using strict, it is
            > : mandatory that I declare all new variables with a 'my'.
            > : Correct me if I am wrong, however now I dont think so as
            > : this will prevent us from globalising a variable within
            > : a block, I have to thank Michael R. Wolf for this.
            >
            > You're wrong. Consider yourself corrected. We'll waive
            > the standing in the corner bit for now.
            >
            > It is mandatory. Except for buil-in perl variables,
            > like $_ and $., and for special perl variables that are not
            > built-in, like $a and $b.
            > $a and $b are special in perl under strict. $a and $b
            > are used to pass values to 'sort'. For instance:

            Fortunately, I was corrected or my scripts would be in for a big mess.

            Now I could see clearly that Michael R. Wolf thought I was referring to the
            special variable $a, when I ask how to globalise a variable within a block,
            and Michael said to remove the my, which I originally put it as while (
            #.......) {my $a++}

            I wish to repost my question here again.
            When a script gets big, it is quite difficult to keep track of variable
            names, therefore the following question came to my mind.

            Apart from first declaring the lexical variable in the main script, how do
            I, OR , can I, globalise a variable within a block such as this :-

            use strict;
            # without declaring my $how_to_globalise_this_variable here;
            # Can I globalise $how_to_globalise_this_variable within the while loop

            while ( #stuff here ){
            $how_to_globalise_this_variable = 'Global';
            };
            print $how_to_globalise_this_variable;


            > The moral of the story is:
            > Use nice descriptive names for your variables, like $c and $d.

            Normally I do, but for studying sake, I am in the habit of using short names
            such as $a, but now I will use things like $ab_ instead.

            Thanks.



            _________________________________________________________
            Do You Yahoo!?
            Get your free @... address at http://mail.yahoo.com
          • Leon
            Hi Memebrs, To understand things better, could somebody explain to me why in line 6, $number = 5 did not override the outer scope lexical $number.use strict;
            Message 5 of 6 , Jan 2, 2002
            • 0 Attachment
              Hi Memebrs,
              To understand things better, could somebody explain to me why in line 6,
              $number = 5 did not override the outer scope lexical $number.

              use strict;
              my $number = 0;
              while ($number < 3){
              $number++; # lets call this OUTER scope $number
              my $number++;
              $number = 5; # line 6
              print '*'x 30,"\n";

              while ($number < 10 ) {
              $number++;
              print "2nd while \$number = $number\n";
              };
              };

              ---- results ----
              ******************************
              2nd while $number = 6
              2nd while $number = 7
              2nd while $number = 8
              2nd while $number = 9
              2nd while $number = 10
              ******************************
              2nd while $number = 6
              2nd while $number = 7
              2nd while $number = 8
              2nd while $number = 9
              2nd while $number = 10
              ******************************
              2nd while $number = 6
              2nd while $number = 7
              2nd while $number = 8
              2nd while $number = 9
              2nd while $number = 10

              ----- end of msg ---------

              ----- Original Message -----
              From: "Leon" <eventualdeath@...>
              > Normally if we declare a my $variable on a previous my $variable, the
              > previous value is taken over by the latest value like this eg :-
              > my $d = 100;
              > my $d = 2;
              > print $d; # value = 2
              >
              > But in the following script, it would be of interest to new Learners, like
              > me, to note that " use strict " sees 2 different $b in the following
              script
              > :-
              >
              > #!d:\perl\bin\perl.exe -w
              > use strict;
              > use CGI::Carp qw(fatalsToBrowser);
              > print "content-type: text/plain\n\n";
              >
              > while ($b<3){
              > $b++; # lets call this OUTER $b
              > my $b++; # this $b does not take over the value of the OUTER $b
              > # even though it is on the same block
              > while ($b < 5 ) {
              > $b++;
              > print "2nd while \$b = $b\n";
              > };
              > };



              _________________________________________________________
              Do You Yahoo!?
              Get your free @... address at http://mail.yahoo.com
            • Charles K. Clarkson
              Leon ... $a, $b and the ones in perlvar. ... I wouldn t reccomend this, but it is posble to create a global variable within a block
              Message 6 of 6 , Jan 3, 2002
              • 0 Attachment
                "Leon" <eventualdeath@...>

                : ----- Original Message -----
                : From: "Charles K. Clarkson" <cclarkson@...>
                : > $a and $b are special global variables in perl. They
                : > are used in 'sort'. They can also be referred to as
                : > $main::a and $main::b.
                :
                : Previously, somebody pointed to me that perl have special
                : reserved words such as print, chomp, exists and so on....,
                : now I realised that perl have special variables too.
                :
                : To avoid using barewords, warnings have taught me to use
                : UPPERCASE or put quotes.
                : As I do not know which variables are reserved, I just
                : wonder how to avoid using them.

                $a, $b and the ones in perlvar.

                : > : Just a comment :-
                : > : Originally, I always thought that in using strict, it is
                : > : mandatory that I declare all new variables with a 'my'.
                : > : Correct me if I am wrong, however now I dont think so as
                : > : this will prevent us from globalising a variable within
                : > : a block, I have to thank Michael R. Wolf for this.
                : >
                : > You're wrong. Consider yourself corrected. We'll waive
                : > the standing in the corner bit for now.
                : >
                : > It is mandatory. Except for buil-in perl variables,
                : > like $_ and $., and for special perl variables that are not
                : > built-in, like $a and $b.
                : > $a and $b are special in perl under strict. $a and $b
                : > are used to pass values to 'sort'. For instance:
                :
                : Fortunately, I was corrected or my scripts would be in for
                : a big mess.
                :
                : Now I could see clearly that Michael R. Wolf thought I was
                : referring to the special variable $a, when I ask how to
                : globalise a variable within a block, and Michael said to
                : remove the my, which I originally put it as
                : while (#.......) {my $a++}
                :
                : I wish to repost my question here again.
                : When a script gets big, it is quite difficult to keep track
                : of variable names, therefore the following question came to
                : my mind.
                :
                : Apart from first declaring the lexical variable in the main
                : script, how do I, OR, can I, globalise a variable within a
                : block such as this :-
                :
                : use strict;
                : # without declaring my $how_to_globalise_this_variable here;
                : # Can I globalise $how_to_globalise_this_variable within the
                : # while loop
                :
                : while ( #stuff here ){
                : $how_to_globalise_this_variable = 'Global';
                : };
                : print $how_to_globalise_this_variable;

                I wouldn't reccomend this, but it is posble to create
                a global variable within a block without having previously
                used it.

                while ( #stuff here ){
                $::globalised_variable = 'Global';
                }
                print $::globalised_variable;


                By uing $:: (which is the same as $main::) you
                are adding the veriable directly to the symbol table.
                You'll have to use the $:: or $main:: prefix for the
                whole script, but strict won't raise an error.

                It would be better to make the block return
                something.

                my $file_scoped_variable = do {
                while ( #stuff here ){
                my $block_scoped_variable = 'Global';
                }
                $block_scoped_variable; };


                HTH,
                Charles K. Clarkson
                Clarkson Energy Homes, Inc.
                254 968-8328


                How do they get the deer to cross at that yellow road sign?
              Your message has been successfully submitted and would be delivered to recipients shortly.