I have a lookup table of values and formulae, that I reuse many times, like so:

my (%frame_lookup_by) = ( 21 => { desc => "S/W Status", value => sub { $minor_frame_data[0]*256 + $minor_frame_dat +a[1]} , }, 33 => { desc => "HK Subcom", value => sub { $minor_frame_data[1] % 16 } , #modu +lo 16 of w12 }, 117 => { desc => "General Status", value => sub {$minor_frame_data[0]*256 + $minor_frame_data +[1] }, }, 129 => { desc => "Sensor Status", value => sub {$minor_frame_data[0]*256 + $minor_frame_data +[1] }, }, );
However, this hash is used by various subroutines, and @minor_frame_data varies with each time subroutine call. Is there a way to define this once and pass it into each subroutine, where it's updated as necessary? I'd like to avoid globals, obviously, but realize that if I declare
my @minor_frame_data
in each subroutine, the @minor_frame_data in the has of subroutines and the current subroutine are in different lexical scopes, and thus contain different values. I want the values calculated in each subroutine to be derived from the current values in @minor_frame_data. In other words, the following

$ref_frame_lookup_by->{21}->{value}->()
should yield 257 if @minor_data = (1,1). Similarly,

$ref_frame_lookup_by->{33}->{value}->()
should yield 1 if @minor_data = (1,1).

How can I write it so the @minor_frame_data values from the current subroutine are used instead of the ones in the original lexical scope?


In reply to Updating hash of subroutines by Anonymous Monk

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.