Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

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

by bradcathey (Prior)
on Nov 03, 2003 at 04:25 UTC ( #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!
  • 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, 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
  • 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.
  • Log In?

    What's my password?
    Create A New User
    [talexb]: 2001 sounds about right .. got myself a separate box just to run Linux on. Seriously clueless.

    How do I use this? | Other CB clients
    Other Users?
    Others making s'mores by the fire in the courtyard of the Monastery: (10)
    As of 2018-07-17 17:51 GMT
    Find Nodes?
      Voting Booth?
      It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?

      Results (374 votes). Check out past polls.