Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

my My $my;

by Thilosophy (Curate)
on Oct 22, 2007 at 07:11 UTC ( [id://646374]=perlquestion: print w/replies, xml ) Need Help??

Thilosophy has asked for the wisdom of the Perl Monks concerning the following question:

Monkey Fellows Fellow Monks,

Sixapart says:

my Perlbal::ClientHTTPBase $cb = shift; # base object
and I like how that looks.

But what does it do? Does it enforce types (namely PerlBal::ClientHTTPBase) ? Is it just a comment? How does it even parse?

Replies are listed 'Best First'.
Re: my My $my;
by Zaxo (Archbishop) on Oct 22, 2007 at 09:14 UTC
Re: my My $my;
by shmem (Chancellor) on Oct 22, 2007 at 09:10 UTC
    It doesn't seem to do anything useful but checking whether a package exists:
    qwurx [shmem] ~ > perl -MO=Deparse -le 'my My $my = "my"' No such class My at -e line 1, near "my My" -e had compilation errors. BEGIN { $/ = "\n"; $\ = "\n"; } $my = 'my';

    So it enforces the existence of that class; given that, it parses fine:

    qwurx [shmem] ~ > perl -MO=Deparse -le 'package My; package main; my M +y $my = "my"' BEGIN { $/ = "\n"; $\ = "\n"; } my $my = 'my'; -e syntax OK

    Dunno how that relates to being 'bound to the use of "fields" pragma' as the docs state, though.

    --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}
      Thanks for the replies, I figured it out now, I think.

      Saying

      my My $my
      indeed tells the compiler that $my should be a My object. This information is not used for type-checking, however (neither at run-time nor at compile time). It is currently only used to make use of the fields pragma, which lets you define your class' fields:
      # compile error $my->{no_such_field} ++;
      In addition to checking the spelling of field names, you get the improved performance of having the hash access turned into an array/pseudo-hash access (On the other hand, pseudo-hashes seem to be evil, the docs warn about their imminent removal from Perl).
      use strict; { package My; use fields qw[ one ]; sub new { return fields::new(shift); } } my My $my = new My; $my->{one}++;
      b-deparses to
      package My; sub BEGIN { use strict 'refs'; require fields; do { 'fields'->import('one') }; } sub new { use strict 'refs'; return fields::new(shift @_); } package main; use strict 'refs'; {;}; my $my = 'My'->new; ++$$my[1]; ## <--- array access
        On the other hand, pseudo-hashes seem to be evil, the docs warn about their imminent removal from Perl

        They have been removed from 5.10. On the other hand, 5.10 adds support for field hashes, which in turn are used for Inside Out objects, which is what all the cool kids are using these days.

        The main thing to remember about field hashes, if you haven't heard about them, is that they allow references to be used as hash keys in an efficient manner. (Yes, there are modules that let you do this already, but the performance stinks).

        • another intruder with the mooring in the heart of the Perl

        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}
Re: my My $my;
by Anonymous Monk on Oct 22, 2007 at 08:50 UTC
    perldoc -f my my EXPR my TYPE EXPR my EXPR : ATTRS my TYPE EXPR : ATTRS A "my" declares the listed variables to be local (lexically) t +o the enclosing block, file, or "eval". If more than one value i +s listed, the list must be placed in parentheses. The exact semantics and interface of TYPE and ATTRS are still evolving. TYPE is currently bound to the use of "fields" pragm +a, and attributes are handled using the "attributes" pragma, or starting from Perl 5.8.0 also via the "Attribute::Handlers" module. See "Private Variables via my()" in perlsub for detail +s, and fields, attributes, and Attribute::Handlers.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://646374]
Approved by bingos
Front-paged by Old_Gray_Bear
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2024-12-06 17:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Which IDE have you been most impressed by?













    Results (45 votes). Check out past polls.