You can defininitely have modules all in the same file. You can have modules across multiple files. You're just missing a few key ideas.

The first is that you don't need Exporter if you're doing OO or are in the same file. Exporter is if you want to make a repository of functions, then bring some of them into another file. If you're all in the same file and package, then everything has access to the right symbols.

If you're doing OO, then just do a PACKAGE->new just like normal. use is only to compile another file which has a certain naming convention, and bring it into your namespace. If you have a class in your file, just work with it as if it had been in another file and you'd use'd it.

The third is that you're not going back to the main package. You need to tell the interpreter where the first line to execute is. That's the first line in the main package. Try the following:

package Foo; sub new { print "In Foo::new\n"; my $self = bless {}, 'Foo'; return $self; } package main; my $bar = Foo->new; print "$bar\n";
That should work just fine.

------
We are the carpenters and bricklayers of the Information Age.

Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.


In reply to Re: packages / modules in the same file by dragonchild
in thread packages / modules in the same file by jptxs

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.