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

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

Expand Messages
  • 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 1 of 6 , Jan 3, 2002
      "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

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

      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.