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

Re: [PBML] local

Expand Messages
  • Andrew Johnson
    ... [snip] ... Not exactly -- they will retain those values for the duration of that sub, meaning that any other subs called from within that one will also
    Message 1 of 1 , Jan 30 10:13 AM
      > Hi all,
      >
      > If you reference a bunch of variables with local inside a sub,,,
      > Like so,
      > sub display_small_cart_table
      > {
      > local($reason_to_display_cart) = @_;
      > local(@cart_fields);

      [snip]

      > Does that mean that they will only retain those values in that sub?

      Not exactly -- they will retain those values for the 'duration' of
      that sub, meaning that any other subs called from within that one
      will also see the localized values.

      Using my() would create new lexical variables only visible within the
      particular sub (or block) that declared them, and not any other called
      routines.

      Here is an example that may help illustrate the difference:

      $global = 11;
      my $lexical = 11;
      my_local();
      print "In main scope: $lexical, $global\n";

      sub my_local {
      my $lexical = 42;
      local $global = 42;
      print "Inside my_local: $lexical, $global\n";
      foo();
      }

      sub foo {
      print "Inside foo: $lexical, $global\n";
      }

      I suggest using my() unless you need to use local() because you are
      localizing a special global such as $/.

      regards,
      andrew

      --
      Andrew L. Johnson http://members.home.net/andrew-johnson/
    Your message has been successfully submitted and would be delivered to recipients shortly.