I am hacking together an application under Win98 with ActiveState perl 5.6.1, that forks a child process that does most of the work. To earlier avoid confusion (futher described in Win98 Socket Woes) over parent and child copies of data, I got the bright idea to put the child in its own package, so only the child could see the application data.

OTOH, the whole thing isn't really big enough to warrant chopping into modules, so I kept it all in a single file, like so:

use strict; use warnings; &Hello::printit; package Hello; sub printit; our($b) = "Hello, world!\n"; sub printit { print $b; }

When I run the above, I get warned that $b is undefined in the print call.

If I split the above into two files, and "require Hello;" in the main, the message prints OK.

The difference must lie in how and when "our($b)=..." gets executed, and where $b gets defined. But I would have expected $b to be defined in package Hello, either way.

Any explanations would be welcome!

(Note: there's no fork in the above example; fork isn't the problem, it's just the motivation for splitting things up)

Thanks! -- Dinosaur


In reply to Source files, packages and name spaces by Dinosaur

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.