If I understand correctly, you want to have multiple modules together with the main script all in the same file. One way I can think of, without excessive rewrite, is by using a scalar reference to point to required package code at run-time, sort of a poor-man's dynamic link library.
#!/usr/bin/perl -w
my $pkg;
BEGIN {
if ( $^O =~ /dar/ ) { $pkg = \&mac::get_answer }
elsif ( $^O =~ /sol/ ) { $pkg = \&sun::get_answer }
elsif ( $^O =~ /aix/ ) { $pkg = \&aix::get_answer }
else { die "unknown os." }
};
print "answer is " . &$pkg() . "\n";
exit 0;
package mac;
sub get_answer { return "Apple" }
package sun;
sub get_answer { return "Sun" }
package aix;
sub get_answer { return "IBM" }
__END__
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.