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

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
Calling one script from within another

 



jamiep
Deleted

Jun 8, 2001, 12:00 PM

Post #1 of 4 (678 views)
Calling one script from within another Can't Post

I'm really, really sorry if this has been posted before and if it is a really dim question, but having searched through different forums I haven't been able to come up with a satisfactory answer. I need to call a common 'footer' script from within another script, effectively creating a footer in my html output from the main script. Is there any easy way of doing this? Its quite urgent so any quick response would be really appreciated.

Thanks in advance.



wickedxter
User

Jun 8, 2001, 12:50 PM

Post #2 of 4 (674 views)
Re: Calling one script from within another [In reply to] Can't Post

require "<name of file>.pl";

usage:
#these are called from the require file

&header;
&footer;



rGeoffrey
User / Moderator

Jun 8, 2001, 1:05 PM

Post #3 of 4 (671 views)
Re: Calling one script from within another [In reply to] Can't Post

This is a good time to make a module to use from each script. As an example consider these two files...

testPage.pl


Code
#!/usr/local/bin/perl 

use strict;
use Page;

print &Header ('test of Page.pm'), "Some content would be here", &Footer ();

Which calls Page.pm


Code
#!/usr/local/bin/perl 

package Page;
use strict;

BEGIN{
use Exporter ();
use vars qw ($VERSION @ISA @EXPORT);
$VERSION = 0.10;
@ISA = qw (Exporter);
@EXPORT = qw (&Header &Footer);
}

################################################ subroutine header begin ##
################################################## subroutine header end ##

sub Header
{
my ($title) = @_;

$title = qq(<title>$title</title>) if ($title);

#///////////////////////////message
my $string = <<EOF;
Content-type: text/html

<html>
<head>
$title
</head>
<body marginheight="0" marginwidth="0" topmargin="0"
leftmargin="0" rightmargin="0" bottommargin="0">

EOF
#///////////////////////////message

return ($string);

}

################################################ subroutine header begin ##
################################################## subroutine header end ##

sub Footer
{

#///////////////////////////message
my $string = <<EOF;


<hr>
<center>Copyright &copy; 2001 by me</center>
</body></html>
EOF
#///////////////////////////message

return ($string);

}

###########################################################################
###########################################################################

1; #this line is important, without it your module may not work correctly

You can modify Header () and Footer () to return any text you want, but this should get you moving in the right direction. Page.pm should exist in the same directory as all the scripts that use it or in one of the directories in your @INC. If you are not sure which directories they are type...


Code
perl -V

on the command line or at a DOS prompt. The last section of stuff you get will be the contents of the @INC.


And now for a bit of shameless advertising. I will be presenting a talk on "Making a Module" at YAPC::America::North in Montreal next week. After the talk I will return to this thread and include a link to the slides, but I don't want to spoil the surprise by making them public yet. The original title was "Making a Module: And All the Crap that CPAN Wants Too", but I have to send my slides through the lawyers at work so I have simplified the title.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?


rGeoffrey
User / Moderator

Jun 18, 2001, 5:42 PM

Post #4 of 4 (643 views)
Re: Calling one script from within another [In reply to] Can't Post

The slides are now available at http://www.platypiventures.com/perl/present/MakeModule/001.html.

--
Sun Sep 9, 2001 - 1:46:40 GMT, a very special second in the epoch. How will you celebrate?

 
 


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

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