You could use closures, as has already been mentioned, or you could use tie. Something like this might work for you... I don't know.
package Product; use Tie::Array; @ISA = qw/Tie::StdArray/; use strict; sub TIEARRAY { my $class = shift; bless [ @_ ], $class; } sub FETCH { my $them = shift; my $index = shift; return $index >= @$them ? eval join '*', @$them : $them->[$index]; } package main; ## Initialize your array with 2 elements, 8 and 2 tie my @p, 'Product', 8, 2; print $p[2], "\n"; ## Change the second element to 5 $p[1] = 5; print $p[2], "\n"; ## You can even add new elements to the array; now ## you need to use $p[3] to get the product push @p, 10; print $p[3], "\n";
This is, admittedly, pretty hackish. But it works. :)


16 40 400
If you try to access any element beyond the end of the array, it returns the product of the elements in the array. Admittedly, I don't really like those semantics much--anyone have any better ideas?

In reply to Re: how do i put an equasion in a variable by btrott
in thread how do i put an equasion in a variable, one which is evaluated at the time of use? by Buckaroo Buddha

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, 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, 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.