Hello monks.

I suppose recursive 'uses' should be avoided in general cases, but still, I'd like to understand what's going on with this simple code: A1 and A2 are using C, which uses A2. Warning and strict are used in all files.

File A1.pm

package A1; use A2; use C; sub functionA { print "In A1: ".C::TYPE->{VEG}."\n"; A2::functionA(); }

File A2.pm

package A2; use C; sub functionA { print "In A2: ".C::TYPE->{VEG}."\n"; }

File C.pm

package C; use A2; use constant { TYPE => { FRUIT => 1, VEG => 2, MEAT => 3, }, };

Now we can test:

use A2; A2::functionA(); # prints "In A2: 2"

and

use A1; A1::functionA(); # prints "In A1: 2\nIn A2: 2"

Right. But if we inverse the order of 'use' in A1.pm, so C is 'used' before A2, the second test leads to a non-understanding of external constant TYPE: "Can't use bareword ("C::TYPE") as a HASH ref while "strict refs" in use at A2.pm line 7". Why so?

Question 2: If we keep this order, I can fix the problem by calling in A2.pm "C->TYPE->{VEG}" instead of "C::TYPE->{VEG}". Enlightenments welcome! : )

Cheers.

Locinus (perl v5.12.3 built for MSWin32-x86-multi-thread)


In reply to Behaviour on recursive use by locinus33

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, details, 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, summary, 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.