There are several ways to do that, as always... one elegant way of doing that is using the AutoLoader (as e.g. POSIX does it) together with Exporter. To use that, you have to make up a package file (say, General.pm)

package General; use AutoLoader qw(AUTOLOAD); use Exporter; # functions your main script might want to import our @EXPORT_OK = qw( func1 func2 ... ); 1;

and wrap the content of each file into a subroutine:

original:

#!/usr/bin/perl # file prog1.pl # main program my ($arg_a, $arg_b) = @ARGV; ; # ... more perl code # end main

wrapped:

#!/usr/bin/perl # file func1.al package General; # the function must be compiled into its package. sub func1 { # main program my ($arg_a, $arg_b) = @_; ; # ... more perl code # end main } unless (caller) { func1(@ARGV); } 1; # <-- important!

The package file General.pm lives anywhere where perl can find it. Make a directory ./auto/General/ in the directory where General.pm lives and stuff the wrapped function files (*.al files) there. Write the file ./auto/General/autosplit.ix

package General; # optionally put your subs here to predeclare them at package loading sub func1 ; 1; # last line, important

That's it. Now you can use General.pm in your main script

#!/usr/bin/perl use General qw(func1 otherfunc); # functions from General you want to +call directly my $result = func1($arg_a, $arg_b);

and each function file will be compiled on demand, that is, only if you actually are calling its function.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

In reply to Re: alternative way to subs by shmem
in thread alternative way to subs by steph_bow

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.