Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??
As a beginner, what is probably going to be most helpful is a template to cargo-cult from. When you have read all of the other resources, you will understand what all the pieces here do. But this is a "quick start".

Now I am going to suppose that you have a place somewhere to put your library of Perl stuff you are developing. For the sake of argument that will be "/your/library/path". Now let's create the Foo::Bar module. Create the file "/your/library/path/Foo/Bar.pm". Into it put this:

package Foo::Bar; use Exporter; @ISA = 'Exporter'; # This is a list of functions you are willing to export. @EXPORT_OK = qw( hello ); use strict; # of course # Functions go here. Let's be traditional sub hello { print "Hello, World\n"; } 1;
Now in each script where you might want to be able to "hello", put in the following two lines:
use lib "/your/library/path"; use Foo::Bar qw(hello);
And after that you can call the function "hello" freely.

Now why would you do this? Well the classic syndrome that I see in shell programmers who pick up Perl is that they convert a mess of shell scripts into Perl scripts, and have the Perl scripts call each other like you would in shell. This leaves you with all of the limitations of shell programming, like difficulty handling errors, problems passing around structured data, etc.

What works better in my experience is to take your scripts and write each one as above as a module. Then you can replace the scripts with trivial ones that load up the right module, and call one function in it. But now when your scripts want to call on each other's functionality they can just load up the the right modules and call functions rather than execute external scripts. This gives you all of the flexibility, better performance, and opens up the door later for you to improve how you do your error handling or get fancier in how you store and pass data...

Now be warned as you go off that what I have given is but one common form of module. There are others. And you can easily get fancier (eg by using pod). But this should be enough to actually go and try writing some...


In reply to Re (tilly) 1: What are Modules by tilly
in thread What are Modules by ddrumguy

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 avoiding work at the Monastery: (5)
As of 2024-03-29 07:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found