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

Re: [PBML] Scope (long)

Expand Messages
  • Charles K. Clarkson
    ... From: Hanson, Mark ... Oh, sure!? Ask an easy question. :) Last time I checked there were about 580 members on this list; So
    Message 1 of 1 , Feb 1, 2001
    • 0 Attachment
      ----- Original Message -----
      From: "Hanson, Mark" <Mark.Hanson@...>
      > Hi! I'm a relative newcomer to Perl and I have a string
      > array in one sub which I need to keep in scope so I
      > can print it in a later sub. I do not know how to do this.
      > Any help is appreciated!

      Oh, sure!? Ask an easy question. :)

      Last time I checked there were about 580 members on
      this list; So there should be about 581 ways to answer
      your question. This is my anwer:

      For a quick program where you aren't using:
      *use strict;* or *use strict 'vars';* just define the array
      outside the subs:

      #!/usr/bin/perl
      @string_array = some_neat_re_routine;
      sub not_print {
      # I can play with @string_array here.
      }
      sub printing_stuff {
      # I can print with @string_array here.
      }

      While this works fine, it's only good for small
      programs and special cases. If you are
      programming in a corporate environment or if you
      want to be able to extend the program later, it's best
      to take a more disciplined route to programming.

      You should read perlsub and for your particular
      program perlref. You can find these in your perl
      documentation. It's a good idea to start all your
      programs with:

      #!/usr/bin/perl -w
      use strict;
      use diagnostics;

      If you do you will not be able to run the example
      above.In this case, you would need to pass the
      array or a reference to the array to each subroutine:

      #!/usr/bin/perl -w
      use strict;
      use diagnostics;

      @string_array = some_neat_re_routine;

      @string_array = not_print (@string_array);

      printing_stuff (@string_array);

      sub not_print {
      my @sub_string_array = @_;
      # I can play with @sub_string_array here.
      return @sub_string_array;
      }
      sub printing_stuff {
      my @sub_string_array = @_;
      # I can print with @sub_string_array here.
      return @sub_string_array;
      }

      Of course, if your string array is really large the
      above would be inefficient, which is why I
      recommend reading perlref.

      HTH,
      Charles K. Clarkson
    Your message has been successfully submitted and would be delivered to recipients shortly.