Beefy Boxes and Bandwidth Generously Provided by pair Networks BBQ
XP is just a number
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??
The forks module (drop-in replacement for Perl ithreads for unthreaded Perl 5.6 and higher) only missed one feature until this morning: the ": shared" attribute.

Some background: the forks::shared module already had a "_share" subroutine that only needs a reference to the variable (either a scalar, array or hash) to be shared. This subroutine basically does a tie on the variable.

sub _share { my $it = shift; my $ref = ref $it; if ($ref eq 'SCALAR') { tie ${$it},'threads::shared',{},${$it}; } elsif ($ref eq 'ARRAY') { tie @{$it},'threads::shared',{},@{$it}; } elsif ($ref eq 'HASH') { tie %{$it},'threads::shared',{},%{$it}; } elsif ($ref eq 'GLOB') { tie *{$it},'threads::shared',{},*{$it}; } else { _croak( "Don't know how to share '$it'" ); } } #_share

So the attribute handler only needed to get the reference and act on that. No further attribute data needs to be kept.

Yesterday, I finally grokked attributes.pm, particularly this bit:

MODIFY_type_ATTRIBUTES
This method is called with two fixed arguments, followed by the list of attributes from the relevant declaration. The two fixed arguments are the relevant package name and a reference to the declared subroutine or variable. The expected return value as a list of attributes which were not recognized by this handler. Note that this allows for a derived class to delegate a call to its base class, and then only examine the attributes which the base class didn't already handle for it.

and created the following code:

BEGIN { no strict 'refs'; # same handler for all types, so we loop through them foreach my $type (qw(SCALAR ARRAY HASH)) { my $name = "UNIVERSAL::MODIFY_${type}_ATTRIBUTES"; my $old = \&$name; # Install our new attribute handler *$name = sub { my ($package,$ref,@attribute) = @_; _share( $ref ) if grep m#^shared$#, @attribute; # handle other attributes, is this needed? if (@attribute = grep !m#^shared$#,@attribute) { @attribute = $old->( $package,$ref,@attribute ); } return @attribute; } #$name } } #BEGIN

Now, what I'm wondering about is whether I should capture the code reference of the "old" handler, and pass on the remaining attributes to it. Or whether that this is somehow handled automatically.

If it is not needed, I'd like to remove it of course. But if it is needed and people use other attribute handlers, than that may cause the other attribute handlers not to be called and thus break other modules.

If this is a piece of solid code, I was thinking of using this approach for other attributes and/or generalizing this into a module.

Finally, yes I know about Attribute::Handlers, but since I'm only interested in tieing the reference, it seems like overkill to me. Especially looking at the compactness of the code I've created now.

Liz


In reply to forks.pm finally supports : shared attribute, correctly? by liz

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others meditating upon the Monastery: (8)
    As of 2014-04-21 12:36 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      April first is:







      Results (495 votes), past polls