I will attempt to explain how to put some generally useful function into a "module" so that you can call it from other programs.

Best way, is by example:
Make a file called TEST_GIZMO.pm and stick this code into it:

#!/usr/bin/perl -w use strict; package TEST_GIZMO; use vars qw(@ISA @EXPORT @EXPORT_OK %EXPORT_TAGS $VERSION); use Exporter; our $VERSION=1.00; our @ISA = qw(Exporter); our @EXPORT = qw(test1 test2); our @EXPORT_OK = qw(); #some avanced stuff but it is possible #to only export a subset of functions by #default and allow others to be imported #"by request". sub test1 { print "test1 in TEST GIZMO worked!\n"; } sub test2 { print "test2 in TEST GIZMO worked!\n"; } 1; ### this is obscure stuff, but you need this line !!!! ### by default Perl returns the value of the last line in a ### sub or package. In this case, it means that this package ### inclusion "worked". In the C world or command line, ### shell world, 0, zero means "it worked", non-zero is ### an error code, but not in THIS case of Perl. "1" is the ### right return value here! Any way trust me, you need a ### 1 return value at the end of a .pm module.
Now make a file called test_module.pl and stick this code into it:
#!/usr/bin/perl -w use strict; use TEST_GIZMO; test1; test2; TEST_GIZMO::test1; #this is "fully qualified name", a way to #invoke test1 even if that name wasn't #explicitly exported.
When you run test_module.pl, you should see:
test1 in TEST GIZMO worked!
test2 in TEST GIZMO worked!
test1 in TEST GIZMO worked!

I hope that this is enough "boiler plate" for you to create your own library functions. There is of course a lot to this subject, but I hope I got you started!


In reply to Re: Calling Subroutines of package from another program? by Marshall
in thread Calling Subroutines of package from another program? by anbutechie

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.