Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Re: Perl OO: Need a concise way of representing operations on an object

by Athanasius (Monsignor)
on Nov 04, 2012 at 03:41 UTC ( #1002162=note: print w/ replies, xml ) Need Help??


in reply to Perl OO: Need a concise way of representing operations on an object

I think you can achieve what you’re looking for by using closures as follows:

{ my ($foo, $bar, $container); my %transformations = ( xform1 => sub { ... }, xform2 => sub { ... }, ); sub transform { (my $self, $container) = @_; for my $xform (@{$self->{xforms}}) { $foo = get_foo(); $bar = get_bar(); $transformations{$xform}->(); } } }

Using this approach, the anonymous subroutines (closures) in %transformations have access to $foo, $bar, and $container without the need for the boilerplate code.

Hope that helps,

Athanasius <°(((><contra mundum


Comment on Re: Perl OO: Need a concise way of representing operations on an object
Download Code
Re^2: Perl OO: Need a concise way of representing operations on an object
by wanna_code_perl (Pilgrim) on Nov 04, 2012 at 09:05 UTC

    Thank you for your reply. I see what you are trying to do, but I think your use of my in lexical scope around the package means ($foo, $bar, $container) are shared between all Transform objects. This will break thread safety.

    Those variables are associated with a specific call to the transform() method, which is what I tried to demonstrate with my sample code. They need to remain private to that call and its delegates, but, ideally, without all the repetitive code. (Keep in mind this example is simplified... there will be more going on like alternative traversals, building transforms on top of other transforms, and so forth.)

      ...($foo, $bar, $container) are shared between all Transform objects. This will break thread safety.

      No, it won’t.

      The variables are shared between Foo::Transform objects within a thread (which doesn’t matter in this case, as they’re explicitly re-initialised in sub transform before being used). But in Perl, whenever a new process or thread is created, the memory is cloned at the point of creation, and thereafter is (by default) not shared (unless this is done explicitly).

      For processes, see fork:

      File descriptors (and sometimes locks on those descriptors) are shared, while everything else is copied.

      For threads, see threads and threads::shared:

      By default, variables are private to each thread, and each newly created thread gets a private copy of each existing variable.

      Hope that helps,

      Athanasius <°(((><contra mundum

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others meditating upon the Monastery: (4)
As of 2014-09-02 00:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    My favorite cookbook is:










    Results (18 votes), past polls