Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Thanks for sharing that.

Toying around a bit, I found out that the RHS of my My $my = $foo need not be an object of class My, in fact, it can be any object, or even just a plain array reference:

package My; use fields qw(bar); sub new { return fields::new(shift) } 1;
package Foo; use fields qw(foo); sub new { return fields::new(shift) } 1;
#!/usr/bin/perl use My; use Foo; use strict; my $bar = []; print "\$bar = $bar\n"; my My $my = $bar; $my->{bar} = 'quux'; print "\$my = $my\n"; print "my bar field: $my->{bar}\n"; print "bar: (",@$bar,")\n"; my $foo = Foo->new(); $foo->{foo} = 'is foo'; print "(1)foo: $foo, foo field = ".$foo->{foo},"\n"; my My $bar = $foo; $bar->{bar} = 'is bar'; print "(2) bar: $bar, bar field = ".$bar->{bar},"\n"; print "(3) foo: $foo, foo field = ".$foo->{foo},"\n"; print "(4) foo: $foo, foo field = ".$foo->{bar},"\n"; __END__ $bar = ARRAY(0x827ac28) $my = ARRAY(0x827ac28) my bar field: quux bar: (quux) (1) foo: Foo=ARRAY(0x827ab44), foo field = is foo (2) bar: Foo=ARRAY(0x827ab44), bar field = is bar (3) foo: Foo=ARRAY(0x827ab44), foo field = is bar No such pseudo-hash field "bar" at my.pl line 25.

Assigning the Foo object $foo to a lexical my My $bar makes it into a My object. Or it seems to do so; the object's body still is an array reference blessed into package Foo, while its fields seem to be of the My package. But then, the 'My' package didnn' import field 'foo'...? Scary stuff, that, great for obfus and to annoy your cow-orkers and maintainers, and for writing hard to spot bugs...

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re^3: my My $my; by shmem
in thread my My $my; by Thilosophy

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!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • 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
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            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 all is quiet...

    How do I use this? | Other CB clients
    Other Users?
    Others drinking their drinks and smoking their pipes about the Monastery: (3)
    As of 2018-05-27 04:18 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?
      Notices?