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



jamiep
Deleted

Jun 8, 2001, 12:00 PM


Views: 721
Calling one script from within another

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


Views: 717
Re: Calling one script from within another

require "<name of file>.pl";

usage:
#these are called from the require file

&header;
&footer;



rGeoffrey
User / Moderator

Jun 8, 2001, 1:05 PM


Views: 714
Re: Calling one script from within another

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


Views: 686
Re: Calling one script from within another

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?