Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

Re^4: Dynamically constructed function calls

by dragonchild (Archbishop)
on Nov 04, 2004 at 04:03 UTC ( #405081=note: print w/replies, xml ) Need Help??


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

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.

Replies are listed 'Best First'.
Re^5: Dynamically constructed function calls
by ikegami (Patriarch) on Nov 04, 2004 at 04:48 UTC

    *$_ = 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' );
      *$_ = 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://405081]
help
Chatterbox?
and the web crawler heard nothing...

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











    Results (13 votes). Check out past polls.

    Notices?