Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2024-03-19 11:38 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found