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

Re: [PBML] my, our, local?

Expand Messages
  • Paul Archer
    ... Yuppers. One critical difference between local and my (and a good reason to (usually) avoid local ) is that a local variable in a subroutine will be
    Message 1 of 8 , Sep 1, 2003
      >
      > Paul Archer wrote:
      > > (Short answer on 'local', BTW, is it predates 'my', and isn't used much
      > > anymore.)
      >
      >
      > That's right. The "my" function wasn't around in Perl 4,
      > "local" was.
      >

      Yuppers.

      One critical difference between 'local' and 'my' (and a good reason to
      (usually) avoid 'local') is that a 'local' variable in a subroutine will be
      passed to another subroutine, where a variable scoped with 'my' won't.


      $var1 = 1;
      $var2 = 2;

      &sub1;


      sub sub1 {
      my $var1 = one;
      local $var2 = two;
      &sub2;
      }


      sub sub2 {
      print "\$var1 is $var1\n";
      print "\$var2 is $var2\n";
      }

      print "Afterwards \$var1 is $var1\n";
      print "Afterwards \$var2 is $var2\n";


      __END__


      Paul
    • Jonathan Hipkiss
      Thanks to all who replied to this, much appreciated. Jonathan ... these
      Message 2 of 8 , Sep 2, 2003
        Thanks to all who replied to this, much appreciated.

        Jonathan

        --- In perl-beginner@yahoogroups.com, "Jonathan Hipkiss"
        <jonathan@h...> wrote:
        > Can anyone explain in "English" what the difference is between
        these
        > three?
        >
        > Cheers
        >
        > Jonathan
      • lang@ms.chinmin.edu.tw
        ... I think another reason is you find yourself using the same name for different variables in different parts of the program, without realizing it. -- Greg
        Message 3 of 8 , Sep 4, 2003
          On Mon, 01 Sep 2003, Paul Archer wrote:

          > >From the beginners@... list:

          > >From Bob_Showalter@... Mon Sep 1 10:27:55 2003
          > Date: Wed, 13 Aug 2003 09:44:06 -0400
          > From: Bob Showalter <Bob_Showalter@...>
          > To: 'Paul Archer' <tigger@...>
          > Cc: beginners@...
          > Subject: RE: our vs my

          > Since global variables are created automatically,
          > it's easy to introduce bugs into your program by making typos in variable
          > names.

          I think another reason is you find yourself using the same name
          for different variables in different parts of
          the program, without realizing it.

          --
          Greg Matheson, Taiwan
        Your message has been successfully submitted and would be delivered to recipients shortly.