Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

Re^5: Dynamically constructed function calls

by ikegami (Patriarch)
on Nov 04, 2004 at 04:48 UTC ( #405087=note: print w/replies, xml ) Need Help??


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

*$_ = 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' );

Replies are listed 'Best First'.
Re^6: Dynamically constructed function calls
by ysth (Canon) on Nov 04, 2004 at 05:36 UTC
    *$_ = sub ... doesn't work if $_ is an integer.
    Not true. Did you try it?
    $ perl -we'*$_ = sub { print "I am 1 $_[1].\n" } for 1; $x = bless {}; + $meth = "1"; $x->$meth("world")' I am 1 world.
    $_ isn't a lexical, so it doesn't get captured, producing "I am world" for output.
    Correct.
    What's after the -> has to start with a $, so do { @ARGV } wouldn't work even if it did return a scalar.
    dragonchild was responding to my suggestion that ->do{} be made to work.

      "Not true. Did you try it?"

      yes, I did. Perl does many odd things, so I always try things before commenting on them. I even tried a few variations. None of them worked. It works, now, though, so I must have done somethig wrong, but I can't imagine what. X_X

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://405087]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (3)
As of 2023-12-11 06:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    What's your preferred 'use VERSION' for new CPAN modules in 2023?











    Results (41 votes). Check out past polls.

    Notices?