Professional Web Applications Themes

storing values non-globally - PERL Beginners

Hi all, Sorry about subject line, it's the best I could do. Two (somewhat realted) questions... 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. A ...

  1. #1

    Default storing values non-globally

    Hi all,

    Sorry about subject line, it's the best I could do. Two (somewhat realted)
    questions...

    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.

    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.

    Is this a valid alternative to global variables?

    -K

    --
    Kevin Pfeiffer
    International University Bremen

    Kevin Pfeiffer Guest

  2. #2

    Default Re: storing values non-globally

    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]japhypobox.com[/email] [url]http://www.pobox.com/~japhy/[/url]
    RPI Acacia brother #734 [url]http://www.perlmonks.org/[/url] [url]http://www.cpan.org/[/url]
    <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. ]

    Jeff 'Japhy' Pinyan Guest

Similar Threads

  1. Links - globally applying to a multi-page doent?
    By hungry_ghost@adobeforums.com in forum Adobe Acrobat Macintosh
    Replies: 2
    Last Post: November 11th, 03:13 PM
  2. Globally change fontsize in website
    By A.M in forum Macromedia Dynamic HTML
    Replies: 9
    Last Post: January 11th, 03:45 PM
  3. Webservices: Timeout and storing values Q
    By Ivan Demkovitch in forum ASP.NET Web Services
    Replies: 1
    Last Post: November 14th, 06:49 AM
  4. Replies: 4
    Last Post: July 9th, 12:59 PM
  5. newbie needs to globally edit his site
    By leegold in forum PERL Miscellaneous
    Replies: 4
    Last Post: July 2nd, 08:38 AM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139