Sunday, June 6, 2010

Embedding Perl into Perl

This article describes embedding a Perl script into a Perl script. This program is an Common Gateway Interface, which reads ASCII files through file upload and displays content of the file. The Perl script upload.pl is called through the Perl script file-upload.pl using function eval{ }. This program can be modified to call by reference function, to access in other programs too.
file-upload.html
<html>
<head>
<title>File Upload</title>
</head>
<body>
<form method="post" action="/cgi-bin/file-upload.pl" enctype="multipart/form-data">
Enter the file to upload:<br>
<input name="file" size="45" type="file"><br>
<p>
<input name="reset" type="reset">
<input name="submit" value="Upload" type="submit">
</p>
</form>
</body>
</html>

file-upload.pl
#!/usr/bin/perl
eval { require "upload.pl" };
my @x = upload();
print "Content-type: text/html\n\n";
print "<font face='courier new'>",@x,"</font>";

upload.pl
#!/usr/bin/perl
use strict 'refs';
use lib '..';
use CGI qw(:standard);
use CGI::Carp qw(fatalsToBrowser);
sub upload()
{
my $file = param('file');
while (<param('file')>)
{
@_ = <$file>;
}
return (@_);
}

Note: Both Perl scripts must granted file permission (0755)

0 comments :

Post a Comment