rational_icthus,
The following accomplishes what you want plus has the added benefit of allowing you to cache the values of the function.
package TieTest; sub TIESCALAR { my $class = shift @_; die "Incorrect # of arguments" if @_ % 2; my $self = bless {}, $class; $self->_init(@_); return $self; } sub FETCH { my $self = shift @_; my $work_around = sub { my $tgt = shift @_; if (exists $self->{$tgt}) { print "Fetching value from cache\n"; return $self->{$tgt}; } print "Calculating and caching value for $tgt\n"; return $self->{$tgt} = $self->{subref}->($tgt); }; return $work_around; } sub _init { my $self = shift @_; my %arg = @_; for (qw/subref/) { # All valid args $self->{$_} = delete $arg{$_}; } if (keys %arg) { my $bad = join ' ', keys %arg; die "The following args are invalid: $bad"; } return; } package main; tie my $tied_func, 'TieTest', subref => sub {$_[0] * 2}; # Calculate the function values for 1 .. 100 print $tied_func->($_), "\n" for 1 .. 10; # Retrieve cached values print $tied_func->($_), "\n" for 1 .. 10;
If you don't want the function caching - FETCH becomes:
sub FETCH { my $self = shift @_; my $work_around = sub { my $tgt = shift @_; print "Checking for pause\n"; return $self->{subref}->($tgt); }; return $work_around; }
I hope that helps. You should still file a bug report!

Cheers - L~R


In reply to Re: Wierd Behavior With Tie by Limbic~Region
in thread Wierd Behavior With Tie by rational_icthus

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.