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 | |
by ikegami (Patriarch) on Nov 04, 2004 at 15:56 UTC |