Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I'm trying to put routines in seperate files, and call them from a perl process. I understand that this is what happens with modules, if you declare

use xxx::xxx;

at the begining of a perl file, but can't find out how this relates to home made routines. I'm interested in passing parameters to these routines, and using their output.

I appologise if I'm missing something fundamental about perl architecture here, not looking for a complete answer but a opointer to a resource...

best wishes,
Tobie

Replies are listed 'Best First'.
Re: routines as separate files?
by hardburn (Abbot) on Aug 13, 2003 at 16:49 UTC

    The basics are easy enough. Say you want to make a module named My::Foo. You put a package My::Foo; at the top of the module file to declare what package it is in. After that, you put all your subroutines you want in that file. Modules need to return a true value to indicate that they were loaded successfuly, so you put a 1; at the very end of the file. It is common to put an __END__ declaration after that, but not required.

    To access the module, you need to place it in some directory specified in the @INC global array. Since the module is named My::Foo, which uses the :: sperator, we need to place our module in the correct subdirectory under some @INC dir. The :: seperates the directory path, so we place the module under My/Foo.pm (note the .pm extention, which all Perl modules have).

    There are a few ways to call your subroutines. For all of them, you have to put a use My::Foo; before you call any of them. Then you could call a subroutine named bar() like this:

    My::Foo->bar(); # 'My::Foo' will be the first param passed My::Foo::bar(); # doesn't touch the param list

    You can also use the Exporter module to call bar() without specifiying the full module--see the documentation for Exporter.pm.

    That's the quick-and-dirty overview, which glosses over a lot of cool stuff.

    ----
    I wanted to explore how Perl's closures can be manipulated, and ended up creating an object system by accident.
    -- Schemer

    Note: All code is untested, unless otherwise stated

Re: routines as separate files?
by blue_cowdawg (Monsignor) on Aug 13, 2003 at 16:48 UTC

        Hi, I'm trying to put routines in seperate files, and call them from a perl process. I understand that this is what happens with modules, if you declare use xxx::xxx;

    You might want to check out perldoc perltoot. This will nudge you in the right direction.

    An example, first the package:

        package MyPackage; use Exporter; use vars qw / @EXPORTS @ISA /; my @EXPORTS=qw / mysub /; my @ISA = qw/ Exporter /; sub mysub { print "mysub is going to return \"mysub\"\n"; return "mysub"; } 1;
    and now a main program calling it:
        #!/usr/bin/perl -w use strict; use warnings; use diagnostics; use MyPackage; print mysub(),"\n";

    Like I said... perldoc perltoot is your friend.


    Peter @ Berghold . Net

    Sieze the cow! Bite the day!

    Nobody expects the Perl inquisition!

    Test the code? We don't need to test no stinkin' code!
    All code posted here is as is where is unless otherwise stated.

    Brewer of Belgian style Ales

Re: routines as separate files?
by dragonchild (Archbishop) on Aug 13, 2003 at 16:43 UTC
    Read up on Exporter, which comes with the standard Perl installation. That is exactly what you're looking for.

    A word of advice - use @EXPORT_OK, not @EXPORT.

    ------
    We are the carpenters and bricklayers of the Information Age.

    The idea is a little like C++ templates, except not quite so brain-meltingly complicated. -- TheDamian, Exegesis 6

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: routines as separate files?
by revdiablo (Prior) on Aug 13, 2003 at 18:33 UTC

    Other monks have doled out excellent advice (as usual), but I thought it might be worth emphasizing the fact that storing homemade routines in separate files is exactly what modules are for. You say I understand that this is what happens with modules. I think where you misunderstand is that you may not realize you can make your own modules very easily. This is exactly what all the other monks are giving you advice on how to do.

    (Hopefully my post doesn't come off sounding too condescending; I am just trying to explain a connection you didn't seem to get when you posted your question.)