The beauty of Perl is you don't
have to do it any which way. TIMTOWTDI, after all.
But I think you've gotten some concepts confused. I had trouble initially with Perl's idea of packages because I was so used to Java. Java has a one-to-one relationship between a file and its namespace. The class java.lang.String has to live in a file called "String.class" in a directory hierarchy of java/lang. Perl on the other hand allows you to switch namespaces within the same file, or use a namespace that's totally unrelated to the file the code lives in.
In practice, there usually is a correspondence between a file (like Crypt/Blowfish.pm) and the namespace it uses (Crypt::Blowfish::). But this doesn't have to be. In your example for instance, I can put
somesub in a separate namespace
within the same file, or load a
somesub that lives in a namespace totally unrelated to the file name it is defined in:
In a file called "TestFile.pm":
package Foo::Bar;
sub somesub{
return "somesub value from package Foo::Bar!";
}
1;
In a file called "test.pl":
use TestFile;
package Common;
sub somesub {
my $value = 'somesub value from Common package!';
return $value;
}
package main;
print "Back in main package.\n";
my $rv1 = Common->somesub(); # Defined just above in this file
my $rv2 = Foo::Bar->somesub(); # Found in file TestFile.pm
print "Return Value 1: $rv1\nReturn Value 2: $rv2\n";
My point being that file names and packages are very loosely coupled in Perl. Good reading for all of this is
perlmod and
package.
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: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.