<?xml version="1.0" encoding="windows-1252"?>
<node id="294615" title="forks.pm finally supports : shared attribute, correctly?" created="2003-09-27 03:18:10" updated="2005-07-08 16:14:01">
<type id="115">
perlquestion</type>
<author id="272239">
liz</author>
<data>
<field name="doctext">
The [http://search.cpan.org/author/ELIZABETH/forks/|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.
&lt;P&gt;
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 &lt;code&gt;tie&lt;/code&gt; on the variable.
&lt;code&gt;
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
&lt;/code&gt;
&lt;P&gt;
So the attribute handler only needed to get the reference and act on that.  No further attribute data needs to be kept.
&lt;P&gt;
Yesterday, I finally grokked [http://www.perldoc.com/perl5.8.0/lib/attributes.html|attributes.pm], particularly this bit:
&lt;I&gt;&lt;DL&gt;
&lt;DT&gt;MODIFY_type_ATTRIBUTES  
&lt;DD&gt;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.
&lt;/DL&gt;&lt;/I&gt;
&lt;P&gt;
and created the following code:
&lt;code&gt;
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 = \&amp;$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-&gt;( $package,$ref,@attribute );
            }
            return @attribute;
        } #$name
    }
} #BEGIN
&lt;/code&gt;
&lt;P&gt;
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.
&lt;P&gt;
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.
&lt;P&gt;
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.
&lt;P&gt;
Finally, yes I know about [perldoc://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.
&lt;P&gt;
Liz</field>
</data>
</node>
