Log in

View Full Version : Another of my Perl Contraptions


TheMatrix
June 6th, 2011, 07:25 PM
I love Perl too much :P
It has almost everything. One thing that I find is missing is a good serialize function. True, you could use FreezeThaw from CPAN, but I don't really like it that much.

So, I wrote a Perl Script that just makes a temporary PHP file, stores an array in it and prints that. The Perl Script catches that and returns that to the user, in this case a browser. Anyways, enough talk, let's get to the script:

#!/usr/bin/perl -w

use strict;
use CGI qw( -debug ); #For if you're using the command line

my $CGI = new CGI;

print $CGI->header( "text/plain" );
print "Here is the output of your serialization:";
print "\n=========================================================================\n\n";

#Open PHP file
my $fn = "/tmp/serialize".$$.".php";
open( FILE, ">$fn" ) or die( "Cannot open: $!" );
print FILE "<?php\n";
print FILE '$_array = array( ';
foreach( $CGI->param("s") ) {
print FILE '"'.$_.'", '; #Add each element from the URL-paramatre: "s" to a PHP array.
}

print FILE ");\n";
print FILE 'print serialize( $_array );'."\n"; #Return the serialised data
print FILE '?>';
close FILE;

#Open PIPE and print
open( PIPE, "/opt/lampp/bin/php -q $fn |" ) or die( "Cannot open: $!" ); #I use LAMPP, which has the PHP in that location. You might want to replace "/opt/lampp/bin/php" with "`which php`"
while( <PIPE> ) {
print "$_\n";
}
close PIPE;

print "\n=========================================================================\n";
unlink( $fn ) #Clean up when done to prevent DoS attacks


There are probably better ways of doing this, but hey, this is just version 1.
Maybe it will be of use to someone, someday. You never know....