Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

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

by bradcathey (Prior)
on Nov 03, 2003 at 04:25 UTC ( [id://304000]=perlquestion: print w/replies, xml ) Need Help??

bradcathey has asked for the wisdom of the Perl Monks concerning the following question: (subroutines)

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

Originally posted as a Categorized Question.

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

Replies are listed 'Best First'.
Re: How do I place a subroutine in an external file and call it?
by bradcathey (Prior) on Nov 04, 2003 at 01:20 UTC

    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.

Re: How do I place a subroutine in an external file and call it?
by G V Sriram (Initiate) on Dec 24, 2012 at 05:42 UTC
    With help of Exporter function also you can call the external package function. Then it is not needed to call the function with moduleName::fucntionName

    Originally posted as a Categorized Answer.

Re: How do I place a subroutine in an external file and call it?
by Toxa (Novice) on Dec 22, 2004 at 10:25 UTC

    Hi,

    First of all you will create a directory called TEST, after that you will create a package called ( for example ) external_file.pm and inside of this package you will define your subroutine. To access this package in your code :

    BEGIN { use TEST::external_file ; }

    if you have any problem to create a package, look this site : (link to site containing copyrighted material removed)

    Cheers, Toxa

    Originally posted as a Categorized Answer.

Re: How do I place a subroutine in an external file and call it?
by lakshmananindia (Chaplain) on Nov 19, 2008 at 08:57 UTC
    I will use modules in this case. For example: spliting is my subroutine. package Mypackage; use strict; use warnings; sub splitting { .......... return <what you want> } Then in the main program. use Mypackage; my $a=splitting();

    Originally posted as a Categorized Answer.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://304000]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-23 13:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found