Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

How do I place a subroutine in an external file and call it?

( #304000=categorized question: print w/ replies, xml ) Need Help??
Contributed by bradcathey on Nov 03, 2003 at 04:25 UTC
Q&A  > subroutines


Answer: How do I place a subroutine in an external file and call it?
contributed by bradcathey

We'll use the module model, and utilize the use Module protocol in the calling program. The alternative is to use require but Programming Perl (the blue Camel book), states that "In general, however, use is preferred over require because it looks for modules during complilation, so you learn about any mistakes sooner". Okay, here we go:

In the external file, or module, containing the subroutine:

  1. package the name of the module at the top (no shebang is needed)
  2. end your script with a true value, conventionally 1;
  3. save with initial uppercase letter and .pm extension

Here's the sample code for a module that validates user input for date and time (for untainting purposes):

package Validate; # use package to declare a module sub validate_date { my $val = shift; return() unless $val =~ /^(\d{2})-(\d{2})-(\d{4})$/; return "$3-$1-$2"; } sub validate_time { my $val = shift; return() unless $val =~ /^(\d{2}):(\d{2}) (am|pm)$/i; return uc("$1:$2 $3"); } 1;

In the calling program:

  1. declare the external file with use Module. The .pm and :: (double colons) are assumed by Perl.
  2. call the routine with Module::sub_name($variable_to_pass)

Here's the sample program that calls the Validate module:

#!/usr/bin/perl -w #would use -wT in production to check for taint print "Content-type: text/plain\n\n"; use strict; use CGI::Carp qw(fatalsToBrowser); # only for development use Validate; my $date = "12-01-2003"; my $time = "12:23 pm"; # would come from user input and so needs to +untainted my $valid_date = Validate::validate_date( $date ); if ( $valid_date ) { print "$valid_date is okay\n"; } else { print "$date is invalid\n"; } my $valid_time = Validate::validate_time( $time ); if ( $valid_time ) { print "$valid_time is okay\n"; } else { print "$time is invalid\n"; } exit;

For an object-oriented approach, checkout out bobn's comment.

Many thanks to The Mad Hatter, Roger, ysth.

Please (register and) log in if you wish to add an answer



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • 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, 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, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • Outside of code tags, you may need to use entities for some characters:
            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.
  • Log In?
    Username:
    Password:

    What's my password?
    Create A New User
    Chatterbox?
    and the web crawler heard nothing...

    How do I use this? | Other CB clients
    Other Users?
    Others about the Monastery: (2)
    As of 2014-07-26 12:35 GMT
    Sections?
    Information?
    Find Nodes?
    Leftovers?
      Voting Booth?

      My favorite superfluous repetitious redundant duplicative phrase is:









      Results (176 votes), past polls