Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Comment on

( #3333=superdoc: print w/ replies, xml ) Need Help??

Here it is slightly clearer description of the problem, you cannot find out what the type is -- attributes calls the appropriate MODIFY handler and that is it, but you can't do FETCH-ing outside of the class, attributes::get will call the wrong FETCH based on caller

#!/usr/bin/perl -- use strict; use warnings; use attributes(); use Data::Dump; BEGIN { package Junk; $INC{'Junk.pm'}=__FILE__; use Scalar::Util(); sub Junk::MODIFY_SCALAR_ATTRIBUTES { print "Junk::MODIFY_SCALAR_ATTRIBUTES! @_\n"; my($package, $scalarref, @attrs) = @_; $Junk::attrs{ Scalar::Util::refaddr $scalarref } = \@attrs; return; } sub Junk::FETCH_SCALAR_ATTRIBUTES { print "Junk::FETCH_SCALAR_ATTRIBUTES! @_\n"; my($package, $scalarref) = @_; my $attrs = $Junk::attrs{ Scalar::Util::refaddr $scalarref }; return $attrs ? @$attrs : (); } } BEGIN { package Foo; use Scalar::Util(); sub MODIFY_SCALAR_ATTRIBUTES { print "Foo::MODIFY_SCALAR_ATTRIBUTES! @_\n"; my($package, $scalarref, @attrs) = @_; $Foo::attrs{ Scalar::Util::refaddr $scalarref } = \@attrs; return; } sub FETCH_SCALAR_ATTRIBUTES { print "Foo::FETCH_SCALAR_ATTRIBUTES! @_\n"; my($package, $scalarref) = @_; my $attrs = $Foo::attrs{ Scalar::Util::refaddr $scalarref }; return $attrs ? @$attrs : (); } sub MODIFY_REF_ATTRIBUTES { print "Foo::MODIFY_REF_ATTRIBUTES! @_\n"; my($package, $scalarref, @attrs) = @_; $Foo::attrs{ attributes::refaddr $scalarref } = \@attrs; return; } sub FETCH_REF_ATTRIBUTES { print "Foo::FETCH_REF_ATTRIBUTES! @_\n"; my($package, $scalarref) = @_; my $attrs = $Foo::attrs{ Scalar::Util::refaddr $scalarref }; return $attrs ? @$attrs : (); } sub new { print "Foo::new! @_\n"; my $class = shift; return bless { yo => [@_] }, $class ; } } sub FETCH_SCALAR_ATTRIBUTES { print "\nCalling me is a mistake? bug?\n@_\n@{[caller]}\nSince the +re is no object, you don't know type(package)\n\n"; return; } BEGIN { * FETCH_REF_ATTRIBUTES = \&FETCH_SCALAR_ATTRIBUTES ; } my Junk $bar :STOOL :CHAIR = 42; my Foo $new :AND :CHEWY = Foo->new( 42 ); for my $ref( \$bar, \$new ){ dd [ $ref, attributes::reftype( $ref ) , attributes::_guess_stash +( $ref ) ], [ attributes::get($ref) ], [ attributes::_fetch_attrs($ref) ];;; print "\n\n"; } dd \%Junk::attrs , \%Foo::attrs; __END__ Junk::MODIFY_SCALAR_ATTRIBUTES! Junk SCALAR(0xa38c14) STOOL CHAIR Foo::new! Foo 42 Foo::MODIFY_SCALAR_ATTRIBUTES! Foo SCALAR(0xa38ca4) AND CHEWY Calling me is a mistake? bug? main SCALAR(0xa38c14) attributes C:/perl/5.14.1/lib/MSWin32-x86-multi-thread/attributes.pm 9 +2 Since there is no object, you don't know type(package) ([\42, "SCALAR", undef], [], []) Calling me is a mistake? bug? main REF(0xa38ca4) attributes C:/perl/5.14.1/lib/MSWin32-x86-multi-thread/attributes.pm 9 +2 Since there is no object, you don't know type(package) ([\bless({ yo => [42] }, "Foo"), "REF", undef], [], []) ( { 10718228 => ["STOOL", "CHAIR"] }, { 10718372 => ["AND", "CHEWY"] }, )

In reply to Re^3: Detecting declaration type? by Anonymous Monk
in thread Detecting declaration type? by LanX

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 exploiting the Monastery: (8)
    As of 2014-09-03 05:01 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite cookbook is:










      Results (35 votes), past polls