Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

Using subroutine between scripts

by chriso (Sexton)
on Nov 07, 2003 at 15:51 UTC ( #305331=perlquestion: print w/replies, xml ) Need Help??

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

I have a subroutine that displays data from a mysql table. I have several scripts which, after completing a task, will then display the contents of that table. Rather than copying the subroutine into each script, how can I use the subroutine from one file? If I need to save the file as a module, does it have to be placed in a special perl directory or can I keep it in my cgi-bin? Thanks.

Replies are listed 'Best First'.
Re: Using subroutine between scripts
by TomDLux (Vicar) on Nov 07, 2003 at 16:14 UTC
    # Create a module with the subroutine(s) you want to share package MyLibrary; sub func_1 { ... } # ------------------------------------------------- 42 # non-zero indicates successful loading of module # ------------------------------------------------- # then in your cgi script: use lib '.'; use MyLibrary; print MyLibrary::func_1( @args );

    --
    TTTATCGGTCGTTATATAGATGTTTGCA

      Thanks for your input. In the script you said:

      use lib '_';

      What is this statement? Are you using it as an example and I should replace the _ with something?

      Thanks.

Re: Using subroutine between scripts
by ChrisR (Hermit) on Nov 07, 2003 at 16:21 UTC
    This may not be the standard way of doing things but I find that it works well for me. I just create a library file (not a module) and put all my subs in it. I use a .lib extension for these files to reduce confusion. You could use any extension you want though. Any way, create the .lib file and put your subs in there, including any use statements for modules required by the subs. Be sure to put a valid statement outside any of the subs to keep perl from bombing out. I just put 1; as the last line of the library file. To use this library, put a require '/path to file/file.lib'; in the main program. This will allow you to call any of the subs in the library file from the main program. There's my $.02 worth.
    Library file example:
    #!/usr/bin/perl use Some::Module; sub Sub1 { ... some code return somevalue; } 1;
    Example main program:
    #!/usr/bin/perl -w use strict; use warnings; require '/path to library/libraryfilename'; my $test = Sub1(1,3,"a"); print $test; exit;
    Again, this is probably not the standard way of doing things but it very easy and quick.
Re: Using subroutine between scripts
by asarih (Hermit) on Nov 07, 2003 at 16:22 UTC
    You should definitely put it in a module. That's what code reuse is for; when you want to tweak the subs, you only have to do it once. @INC always include ., so you can put the module file in the same directory as the scripts themselves. If your scripts exist in many directories, then you could do use lib qw[ /module/dir /more/module/dir]; to tell perl to look in the correct directory.
Re: Using subroutine between scripts
by Ninthwave (Chaplain) on Nov 07, 2003 at 16:22 UTC
    It depends on how often you would see it used. To start with put the sub in a file called something.lib You will not need the shebang line. At the end of the file have the last line be just 1; example:
    sub my_silly_sub() { print "This is a silly sub"; } 1;
    And in the perl scripts that use it use
    require "something.lib";
    Depending on the need to use it across lots of scripts or a few, you may than want to move its location to an area common to your scripts. But I wouldn't make it a module until you find it spreading. But than again there are many opinions on what to do. Choose what is best for you.
    "No matter where you go, there you are." BB
      I always use the shebang line in my libraries but it does make sense that you don't need it since the library should never be used by itself. Can you think of any problems that my arise from having the shebang in a library?
        I also often put a shebang in my libraries. It can be helpful to text editors in figuring out what kind of file it is. Emacs, for example, can infer the file type by looking at the first few characters. If I create a Perl library starting with a shebang line, Emacs will open it in Perl mode, regardless of the filename. I've been doing this for years and have never had any problems with it.

        No not really but I was just taught you don't need the shebang line so I never put it in. I personally see the shebang line as the beginning of a script so I leave it out of libraries. I would ask other monks if there are any problems with including it but I can't think of any.

        "No matter where you go, there you are." BB

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others chanting in the Monastery: (2)
As of 2022-09-25 02:42 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    I prefer my indexes to start at:




    Results (116 votes). Check out past polls.

    Notices?