Embedding Perl into Perl

This article describes embedding a Perl script into a Perl script. The program executes through Common Gateway Interface (CGI), which reads ASCII file through file upload method 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 other system 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 (@_);
}

Comments

Most Popular Posts

TNEB Bill Calculator

TNEB Bill Calculator (New)

Technical Questions