in reply to Dynamically constructed function calls

Just check if you can call it
$f->can("prefix_$key")->( $atr );

anon-broquaint

Replies are listed 'Best First'.
Re^2: Dynamically constructed function calls
by tall_man (Parson) on Nov 04, 2004 at 15:45 UTC
    That doesn't work. $f->can returns a false value, and then you get the message:
    Can't use string ("") as a subroutine ref while "strict refs" in use a +t autol.pl line 21.

    Update:In the pod for UNIVERSAL.pm, it says:

    "can" cannot know whether an object will be able to provide a method through AUTOLOAD, so a return value of undef does not necessarily mean the object will not be able to handle the method call.

      That doesn't work.

      It doesn't work with UNIVERSAL::can, but it could work with an overloaded can:

      { package Foo; my %meth = ( foo => sub { print "Foo?\n" }, bar => sub { print "Bar!\n" }, ); sub baz { print "Baz.\n"; } sub can { $meth{$_[1]} || __PACKAGE__->UNIVERSAL::can($_[1]) } sub AUTOLOAD { use vars '$AUTOLOAD'; (my $methname = $AUTOLOAD) =~ s/.*:://; $meth{$methname}->(@_); } } for (qw(foo bar baz qux)) { if (my $cr = Foo->can($_)) { $cr->(); } else { print "Cannot $_\n"; } }
Re^2: Dynamically constructed function calls
by dragonchild (Archbishop) on Nov 04, 2004 at 16:44 UTC
    Surely, you mean
    $f->can("prefix_$key")->( $f, $atr );

    Being right, does not endow the right to be rude; politeness costs nothing.
    Being unknowing, is not the same as being stupid.
    Expressing a contrary opinion, whether to the individual or the group, is more often a sign of deeper thought than of cantankerous belligerence.
    Do not mistake your goals as the only goals; your opinion as the only opinion; your confidence as correctness. Saying you know better is not the same as explaining you know better.

Re^2: Dynamically constructed function calls
by BrowserUk (Patriarch) on Nov 04, 2004 at 16:00 UTC
    anon-broquaint

    Why?


    Examine what is said, not who speaks.
    "Efficiency is intelligent laziness." -David Dunham
    "Think for yourself!" - Abigail
    "Memory, processor, disk in that order on the hardware side. Algorithm, algorithm, algorithm on the code side." - tachyon