in reply to Wierd Behavior With Tie

In 5.6.1, even the first call doesn't work (Can't use an undefined value as a subroutine reference).

Using

my $sub = $tied; print $sub->(5), "\n";

fixed the problem in both places. It seems that $var->() and &{$var}() doesn't check if $var is tied.

 

Anyway, you don't have to use tie at all:

sub wrap { my ($r) = @_; return sub { print "Checking for pause...\n"; return $r->{subref}->(@_); }; } my $tied = wrap($r);

Replies are listed 'Best First'.
Re^2: Wierd Behavior With Tie
by rational_icthus (Sexton) on Jun 02, 2006 at 18:19 UTC
    TMTOWTDI, for sure. On the other hand, the pit bull in me has my teeth into tying variables, and I want to find some way to make it work. Closest I've been able to come so far is this wrapper, which fixes the issue:

    print call $tied, 5; sub call(@){ my $tied = shift; return $tied->(@_); }


    I hate the look of that, though. I really want to be able to tie the scalar, and call the routine via

    print $tied->(5), $/;

      On the other hand, the pit bull in me has my teeth into tying variables

      Tied variables are pretty slow, and they don't always work due to bugs. That's why I suggested the alternative.

      Closest I've been able to come so far is this wrapper

      The variable you called $tied isn't tied. That's why it works. If you fix the variable name, you get:

      sub call { my $sub = shift; return $sub->(@_); }

      You basically took the solution I already posted and put it in a function.

      I got rid of the misleading/broken/wrong prototype. Don't use prototypes unless you have a good reason and you understand the problems associated with using them.

        Credit where credit's due, I took a look at my implementation, plus Limbic~Region's fix, and by the time I had everything working I was implementing your sub inside a tied variable.

        Goodbye, tie.

        Thanks for the comments and suggestions.