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

Monks-

I just ran into this syntax for the first time, and I'm not finding info on it in the docs:

package Foo; my Foo $bar = 42;

(specifically, the "my Foo $bar" bit of it.)

(Update: snipped the irrelevant bit about Invalid SCALAR attribute and made it its own question.)

Explanations are appreciated, but I'm happy to be pointed in the direction of the appropriate pod(s).

Cheers,

Brad

Replies are listed 'Best First'.
Re: What does "my Foo $bar" do?
by Joost (Canon) on Aug 08, 2004 at 12:34 UTC
    This syntax is used in combination with the fields and base pragmas to detect access to non-declared attributes at compile time.

    It works like this (tested in 5.8.5):

    #!/usr/bin/perl -w BEGIN { print "Compile time\n"; } package Foo; use fields qw(some_attribute); sub new { return fields::new(@_); } sub test { my Foo $self = shift; # note the my Foo $self $self->{does_not_exist} = 1; } package main; my Foo $foo = Foo->new(); print "run time\n"; $foo->test(); print "ok\n"; __OUTPUT__ Compile time No such pseudo-hash field "does_not_exist" in variable $self of type F +oo at test.pl line 16.
    and if we remove the Foo in my Foo $self at line 15 (in sub test {})
    Compile time run time No such pseudo-hash field "does_not_exist" at test.pl line 16.

    Programming Perl 3 and the warnings also mention pseudo-hashes, but since perl v5.8.x 5.9 they've been replaced by restricted hashes in the "fields" pragma.

    updated: fields.pm still uses pseudohashes in perl 5.8.x

Re: What does "my Foo $bar" do?
by tachyon (Chancellor) on Aug 08, 2004 at 09:12 UTC

    It looks like a NOP to me....

    C:\>perl -MO=Deparse -e"package Foo; my Foo $bar=42" package Foo; my $bar = 42; -e syntax OK C:\>

    The other example is invalid syntax, dunno what you think it should do.

    cheers

    tachyon

      I have a hard time believing it is a NOP. I actually first saw it right at the top of the synopsis section of the fields doc:

      [snip] package Foo; use fields qw(foo bar _Foo_private); sub new { my Foo $self = shift; [snip]

      As for the attributes example, the attributes doc claims the example is syntactically valid. I realize it is not supposed to "do" anything (other than, I would hope, compile); I'm just trying to grok attributes and fields and ran into these stumbling blocks.

      Brad

        Does this convince you?
        C:\>perl -MO=Concise,-ascii -e"package Foo;my $bar = 23;" 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(Foo 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV 23] s ->4 4 <0> padsv[$bar:1,2] sRM*/LVINTRO ->5 -e syntax OK C:\>perl -MO=Concise,-ascii -e"package Foo;my Foo $bar = 23;" 6 <@> leave[1 ref] vKP/REFC ->(end) 1 <0> enter ->2 2 <;> nextstate(Foo 1 -e:1) v ->3 5 <2> sassign vKS/2 ->6 3 <$> const[IV 23] s ->4 4 <0> padsv[$bar:1,2] sRM*/LVINTRO ->5 -e syntax OK
        Looks pretty convincing to me (there is no difference between the op trees, thus it is a null op).

        MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
        I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
        ** The third rule of perl club is a statement of fact: pod is sexy.

        I have a hard time believing it is a NOP

        It is syntactically valid because it compiles. QED. It apparently does nothing accoridng to Deparse. QED also is it not? This is also valid syntax, left over from previous perls.....

        C:\>perl -MO=Deparse -e"package Foo; $Foo'bar=42" package Foo; $bar = 42; -e syntax OK C:\>

        cheers

        tachyon

Re: What does "my Foo $bar" do?
by perrin (Chancellor) on Aug 08, 2004 at 17:38 UTC
    It was supposed to be used for object typing, in conjunctin with pseudo-hashes. It had a lot of problems and is deprecated now. Damian Conway wrote about it in his book (or maybe that was a lecture I'm remembering).
        It never really worked anyway, as Damian was quick to point out. If you forget to add it somewhere, it blows the whole thing.