CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner:
Including pre-written code



Jan 31, 2000, 7:06 AM

Post #1 of 4 (1705 views)
Including pre-written code Can't Post

Is there a way to include "pre-written" code in a Perl script. For example, I have repeatedly coded the necessary information to access the database (including user name and password) in a number of programs. Is there a way to write this information one time in one file and "include" it in all the programs accessing the database?


Administrator / Moderator

Jan 30, 2000, 10:21 PM

Post #2 of 4 (1705 views)
Re: Including pre-written code [In reply to] Can't Post


It sounds like you want to create a library. Libraries allow you to keep frequently used code in a separate file that you can "include" (using your ssi analogy) as needed.

To do this, simply put your subroutines into a separate text file, such as (make sure there's a 1; as the last line so your library returns a "true" value), and require the library when you need access to the subroutines. Suppose your had a subroutine named verify_username, which you use to check usernames. You can use the following;

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

require "/path/to/your/";

For best encapsulation, the actual code in should all be enclosed within a subroutine, so you don't inadvertantly reassign any variables from your program. You may also wish to further encapsulate your subroutines by passing variables to it, rather than relying on remember what the variable names were. For example, in your program, you can use:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

require "/path/to/your/";
my $verified = verify_username($username_to_check);

if ($verified){
# The user passed the username authentication
else {
# The user failed authentication

then in your library, you can use:

<BLOCKQUOTE><font size="1" face="Arial,Helvetica,sans serif">code:</font><HR>

sub verify_username {
my ($username) = @_;
my $verified = 0;
# more code here

# Change $verified to 1 if a username passes authentication.
return $verified;

In the above, I used the standard pass/fail scheme by giving pass a true value (1), and fail a false value (0).

So yes, you can not only set aside regularly used code, but you can also enclose (encapsulate) them into subroutines that will not affect the rest of your program... with the great side effect of not having to remember the variable names used in each subroutine.

Hope this helps... good luck!


Jan 31, 2000, 7:35 AM

Post #3 of 4 (1705 views)
Re: Including pre-written code [In reply to] Can't Post

You can execute another perl script by using the perl command: 'do'

An example: do "/user/dir1/";

An example that also does error checking:

$file = "/user/dir1/";
unless ($return = do $file)
{ warn "couldn't parse $file: $@" if $@;
warn "couldn't do $file: $!" unless defined $return;
warn "couldn't run $file" unless $return;

These come from O'Reilly's "Perl Cookbook".
Hope this helps... LJF...


Jan 31, 2000, 8:03 AM

Post #4 of 4 (1705 views)
Re: Including pre-written code [In reply to] Can't Post

Thanks for your reply. But I don't need to actually execute the code, just "drag" it in. I'm looking for something like the <--#include --> statement that instead of bringing in HTML code will bring in Perl code snipets I've previously written.



Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives