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

Why does the relative positioning of these code elements matter?

C:\test>type t-Moose.pl #! perl -slw use strict; { package test; use Moose; has x => ( is => 'rw' ); sub doit { my( $self ) = @_; print $self->{ x }; print ${ $self->{ x } }; } } our @c = 12345; my $o = test->new( x => \$c[ 0 ] ); $o->doit; C:\test>perl t-Moose.pl SCALAR(0x229cd4) 12345

Invert the ordering and:

C:\test>type t-Moose.pl #! perl -slw use strict; our @c = 12345; my $o = test->new( x => \$c[ 0 ] ); $o->doit; { package test; use Moose; has x => ( is => 'rw' ); sub doit { my( $self ) = @_; print $self->{ x }; print ${ $self->{ x } }; } } C:\test>perl t-Moose.pl Use of uninitialized value in print at t-Moose.pl line 14. Can't use an undefined value as a SCALAR reference at t-Moose.pl line +15.

Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
"Too many [] have been sedated by an oppressive environment of political correctness and risk aversion."

Replies are listed 'Best First'.
Re: Do Mooses always want to face north?
by ferreira (Chaplain) on Jun 14, 2008 at 13:00 UTC

    My wild guess is that has is not given any magic (like compile-time action) -- as Moose design seems not to favour source filters (but who favours source filters anyway?). That way, what happens is that "test" does not has "x" when the "new" runs. But this is just a hunch as I have not found the time to play with Moose yet.

    Update: Note that disparaged behaviour is not noticed when classes are confined to their own files and used (when everything at the package source would run at compile-time from the perspective of the caller of use).

Re: Do Mooses always want to face north?
by dragonchild (Archbishop) on Jun 14, 2008 at 13:59 UTC
    ferreira is absolutely correct. has() is run-time, not compile-time. The only solutions are to break them out into their own packages, put the class first, or wrap the package in a BEGIN block (and you already have the block part).

    My criteria for good software:
    1. Does it work?
    2. Can someone else come in, make a change, and be reasonably certain no bugs were introduced?
Re: Do Mooses always want to face north?
by ikegami (Patriarch) on Jun 14, 2008 at 16:40 UTC

    In the second snippet, you're using the "module" before it has had a chance to execute. Specifically, you're creating the object before has has had a chance to execute.

    Change

    ... { package test; ... } ...

    to

    ... BEGIN { package test; ... } ...

    use has an implicit BEGIN, but Perl doesn't provide a means for user function such as has to have an implicit BEGIN.

    PS - There's a lot of moose (the life-size fiberglass kind) in Toronto, and they face in every which direction.

      Yep. I added the BEGIN{} per dragonchild's post and problem gone.

      PS - There's a lot of moose (the life-size fiberglass kind) in Toronto, and they face in every which direction.

      A place I used to live many years ago has concrete cows, but they all tend to face in the same direction, (at any given time) just like their flesh and blood counterparts.


      Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
      "Science is about questioning the status quo. Questioning authority".
      In the absence of evidence, opinion is indistinguishable from prejudice.
        Interesting. In the Sonoma County area in California they've got artistically painted cows all around.

        My favorite is the one at Infineon Raceway with the exhaust pipes coming off the back end. :)


        Revolution. Today, 3 O'Clock. Meet behind the monkey bars.

        I would love to change the world, but they won't give me the source code

Re: Do Mooses always want to face north?
by moritz (Cardinal) on Jun 14, 2008 at 14:10 UTC
    this moose looks west. (Sorry, no relation to your question, just to your title ;-)
      Moose always face north, because it keeps their a*ses warm. :-)

      I'm not really a human, but I play one on earth CandyGram for Mongo