Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Re: Question re $obj->$action(@args) syntax

by kyle (Abbot)
on Mar 21, 2007 at 13:26 UTC ( [id://605841]=note: print w/replies, xml ) Need Help??


in reply to Question re $obj->$action(@args) syntax

I don't think I understand your question. I tried this:

use strict; use warnings; use Data::Dumper; my $obj = bless {}, 'Foo'; my $action = sub { print Dumper( \@_ ) }; $obj->$action( 'hello' ); __END__ $VAR1 = [ bless( {}, 'Foo' ), 'hello' ];

...and that works. Are you saying you want to do something like "$obj->sub { do_stuff(); etc(); }( @args );" so that you don't need an explicit $action? It seems that you're basically doing something like:

{ @_ = ( $obj, @args ); # rest of 'sub' goes here }

"Inline" code doesn't need to be made into a sub. Am I misunderstanding your question?

Replies are listed 'Best First'.
Re^2: Question re $obj->$action(@args) syntax
by blazar (Canon) on Mar 21, 2007 at 14:47 UTC
    I don't think I understand your question. I tried this:
    # snip code
    ...and that works.

    Of course!

    Are you saying you want to do something like "$obj->sub { do_stuff(); etc(); }( @args );" so that you don't need an explicit $action?

    Exactly, out of orthogonality considerations.

    It seems that you're basically doing something like:
    { @_ = ( $obj, @args ); # rest of 'sub' goes here }
    "Inline" code doesn't need to be made into a sub. Am I misunderstanding your question?

    Possibly so: I certainly don't understand your reply. Are you saying that you would expect the following to work as (I) intended?

    use strict; use warnings; use Data::Dumper; my $obj = bless {}, 'Foo'; $obj->{ print Dumper( \@_ ) }( 'hello' ); __END__

    If so, then it doesn't because ->{ is interpreted as hash dereferencing:

    $VAR1 = []; Use of uninitialized value in subroutine entry at 605841.pl line 7. Can't use string ("") as a subroutine ref while "strict refs" in use a +t 605841.p l line 7.

    OTOH Anno and grinder completely addressed my question.

      Are you saying that you would expect the following to work as (I) intended?

      No, not at all. What I'm saying is that the code I wrote achieves the goal of removing the extra variable (i.e., there's no explicit $action code ref) while not achieving the goal of a "$obj->blah(@args)" style syntax. To be longer and more explicit:

      use strict; use warnings; use Data::Dumper; my $obj = bless {}, 'Foo'; my @args = ( 'hello' ); { @_ = ( $obj, @args ); print Dumper( \@_ ) } __END__ $VAR1 = [ bless( {}, 'Foo' ), 'hello' ];

      The functional difference between this and what you're shooting for is (in addition to the gross syntax difference) that this one is taking copies in @_ instead of aliases.

      So, perhaps a better short version of what I'm saying is, "if all you want to do is eliminate a variable, do this..." Sorry for the confusion.

        No, not at all. What I'm saying is that the code I wrote achieves the goal of removing the extra variable (i.e., there's no explicit $action code ref) while not achieving the goal of a "$obj->blah(@args)" style syntax.

        Oh, but that I knew. My question was clearly focusing on syntax, not semantics. It was a question "out of curiosity" after all...

        So, perhaps a better short version of what I'm saying is, "if all you want to do is eliminate a variable, do this..." Sorry for the confusion.

        Not at all, it's an instructive discussion anyway, for someone who could stumble upon it, I guess...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2024-04-25 15:40 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found