On Oct 6, Kevin Pfeiffer said:

>I have several types of user input values I am storing in %history, similar
>to the way the shell maintains a history of previous command line commands.
>These are only used by a sub-routine, but they need to remain valid for the
>life of the program. I see no reason for them to be global, but if they
>live in a subroutine, they will be initialized each time.
>
>My current solution is to place the %history variable and its sub-routine
>inside a block.

There's nothing wrong with that.

  {
    my $count;
    sub add { ++$count }
    sub count { $count }
  }

Now add() and count() have access to the lexical $count, but nothing else
does.

>A second (related?) problem. I have a variable $READ_ONLY that is needed by
>some routines. Right now this is also a global value which has a default
>value, but then is modified by one sub-routine. I see no reason for this to
>be global, either.
>
>I'm thinking that it would be better to set up a sub-routine called
>"file_mode" and use it like an OO-method getter/setter. You call it with a
>value (file_mode("r") and it sets this value. You call it without any
>parameters and it returns the current value.

Have it return the old value regardless, like select() does.

  {
    my $mode = 'r';
    sub file_mode {
      my $old = $mode;
      $mode = shift if @_;
      return $old;
    }
    sub thingy {
      if ($mode eq 'r') { ... }
      else { ... }
    }
  }

-- 
Jeff "japhy" Pinyan      [EMAIL PROTECTED]      http://www.pobox.com/~japhy/
RPI Acacia brother #734   http://www.perlmonks.org/   http://www.cpan.org/
<stu> what does y/// stand for?  <tenderpuss> why, yansliterate of course.
[  I'm looking for programming work.  If you like my work, let me know.  ]


-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to