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

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

Replies are listed 'Best First'.
Re^3: Perl OO: Need a concise way of representing operations on an object
by Athanasius (Archbishop) on Nov 04, 2012 at 14:45 UTC
    ...($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