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

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.

Replies are listed 'Best First'.
Re^3: Question re $obj->$action(@args) syntax
by kyle (Abbot) on Mar 21, 2007 at 15:07 UTC

    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...