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

Re: Global variables - when to use

Expand Messages
  • johnbr123zzz
    Thanks for all your replies. Your advice (and examples) has helped to clarify what I need to do now and what would be nice to do in the future. Regards John
    Message 1 of 5 , Aug 3, 2005
    • 0 Attachment
      Thanks for all your replies. Your advice (and examples) has helped to
      clarify what I need to do now and what would be nice to do in the
      future.

      Regards
      John

      --- In perl-beginner@yahoogroups.com, Jeff 'japhy' Pinyan
      <japhy@p...> wrote:
      > On Aug 2, johnbr123zzz said:
      >
      > > I have a fairly long script and want to use a hash to collect
      data.
      > > This hash will need to be accessed by a number of subroutines,
      each
      > > of which will add or change data and return the modified hash.
      > >
      > > Is the only way to do this by declaring the hash globally. I've
      read
      > > a lot about trying to minimise the use of global variables but am
      > > still grappling with the concept of passing by reference. Thanks
      for
      > > any input.
      >
      > Well, the preferred way would be, as you've said, to use
      references.
      > There's plenty of standard documentation to help you learn about
      them,
      > and a good starting place is 'perldoc perlreftut'.
      >
      > Here's a brief example:
      >
      > my %hash = ();
      >
      > add_key(\%hash); # \%hash creates a reference to %hash
      >
      > print $hash{foo};
      >
      > sub add_key {
      > my $href = $_[0];
      > $href->{foo} = 10; # $href->{foo} <==> $hash{foo}
      > }
      >
      > --
      > Jeff "japhy" Pinyan % How can we ever be the sold short or
      > RPI Acacia Brother #734 % the cheated, we who for every service
      > http://japhy.perlmonk.org/ % have long ago been overpaid?
      > http://www.perlmonks.org/ % -- Meister Eckhart
    Your message has been successfully submitted and would be delivered to recipients shortly.