Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change

Re: Passing an anonymous block to a method.

by TGI (Parson)
on Mar 11, 2011 at 16:25 UTC ( #892689=note: print w/replies, xml ) Need Help??

in reply to Passing an anonymous block to a method.

Since prototypes are compile time effects and method resolution happens at run time there is no (easy) way to make them happy together. You could do something with a sub based wrapper:

sub do_this (&$$;@) { my $code = shift; my $method = shift; my $obj = shift; $obj->$method( $code, @_ ); } do_this {$_ += 1} do_it => $o, 'fred';

Yep, it's all backwards order-wise. By using multiple prototyped subs you might be able to make a saner syntax. Something like this: run_code { 'foo' } on_object $o  with_method 'bar' and_args 'fred', but less stupid.

TGI says moo

Replies are listed 'Best First'.
Re^2: Passing an anonymous block to a method.
by BrowserUk (Pope) on Mar 11, 2011 at 16:49 UTC

    Thank you for looking at this from another perspective.

    The more I investigate this, the more I'm convinced that the source of the problem is my having bought into the monoculture of OO syntax, whilst not wanting to give up the convenience and flexibility of being able to tailor the syntax to the task.

    Once that realisation dawned, it looked more closely at whether I was actually benefiting elsewhere from OO for this particular application, and I've pretty much concluded no.

    Since then, I've made some progress. My ideal situation would be to use an lvalue accessor to the properties and that seemed ideal until I discovered that the actual assignment to the underlying value accessed via an lvalue sub is done outside of the scope of that sub.

    That is, in the following:

    my $x; sub x :lvalue { use somepragma; $x; } x() += 1;

    The assignment of 1 to $x is not subject to somepragma. And that was the purpose of using an accessor in the first place.

    Dropping the OO and using the anonymous block syntax seems to be my best bet at this time. Now I'm investigating appying the :shared attribute to an entire namespace.

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (7)
As of 2021-05-18 07:00 GMT
Find Nodes?
    Voting Booth?
    Perl 7 will be out ...

    Results (174 votes). Check out past polls.