Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^2: Dynamically constructed function calls

by dpuu (Chaplain)
on Nov 04, 2004 at 01:07 UTC ( #405066=note: print w/replies, xml ) Need Help??

in reply to Re: Dynamically constructed function calls
in thread Dynamically constructed function calls

It has to be a scalar, but it doesn't need to be a variable. You can say
However, using a variable is probably clearer.

Update: added the missing '$' pointed out by ikegami

Opinions my own; statements of fact may be in error.

Replies are listed 'Best First'.
Re^3: Dynamically constructed function calls
by ysth (Canon) on Nov 04, 2004 at 02:07 UTC
    I've toyed with the idea of making $object->do {expression-yielding-method-name} work. Right now it gives a syntax error.

    Of course, you can do something pretty close by just having a do method:

    sub foo { print "in foo" } sub do { my $self = shift; my $meth = shift; $self->$meth(@_) } $object->do("foo", @args);
      It would have to be something that has scalar context applied to it. That would yield some neat obfu.
      package Foo; sub new { bless {}, shift } *$_ = sub { my ($self, $arg) = @_; print "I am $_ $arg.\n" } for 1 .. +5; package main; my $foo = Foo->new; $foo->do { @ARGV }( 'world' );

      Blammo! :-)

      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.

        *$_ = sub ... doesn't work if $_ is an integer.

        $_ isn't a lexical, so it doesn't get captured, producing "I am world" for output.

        What's after the -> has to start with a $, so do { @ARGV } wouldn't work even if it did return a scalar.

        The following works

        package Foo; sub new { bless {}, shift } do { my $n=$_; *${\"n$n"} = sub { my ($self, $arg) = @_; print "I am $n $arg.\n" } } for (-1..5); package main; my $foo = Foo->new; $foo->${\(n.@ARGV)}( 'world' );
Re^3: Dynamically constructed function calls
by ikegami (Pope) on Nov 04, 2004 at 04:37 UTC

    You're missing a $. It should be $f->${\("prefix_$key")}($atr);

Re^3: Dynamically constructed function calls
by itub (Priest) on Nov 04, 2004 at 01:54 UTC
    Nice trick, I had never thought about that! :-)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://405066]
MidLifeXis waves.
[stevieb]: man, I absolutely LOVE driving in the worst winter conditions. In the mountainous roads, there are extremely few (if any) other drivers. I love the challenge. I've been doing extreme off-road my whole life, and as I said, I'm always well...
[stevieb]: ...prepared for even the very worst
Discipulus seems a part of the globe very unfrindly with Kawasaki riders..
[thezip]: My doggies love it when it snows
[perldigious]: As long as you are a good "boy scout" stevieb, more power to you... but I like my Jeep, and don't like the idea of rolling it or crashing it in to anything. :-)
[thezip]: Me, not as much.
[perldigious]: Mine too thezip, especially my German Shepherd. He looks like he is about to die of heat exhaustion all summer, so he goes nuts when winter comes and he can play in the snow.
[stevieb]: my main off-road vehicle nowadays is a strengthened mercedes ML320. I'm very hard on it, but I've never had any real issues at all (I love how small and light it is, and it has a very, very good centre of gravity for how I roll)

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (11)
As of 2016-12-06 16:48 GMT
Find Nodes?
    Voting Booth?
    On a regular basis, I'm most likely to spy upon:

    Results (112 votes). Check out past polls.