Who's Online

We have 2 guests online

lyoner.jpg

Home arrow Business arrow Perl Snippets
Perl Snippets PDF Print E-mail
Written by Andreas Schiffler   
Monday, 18 September 2006
Useful perl code fragments.
base64.pl
#!/usr/bin/perl -w
use strict;
use CGI qw(:standard);
use MIME::Base64 qw(encode_base64);
my $filename;
my $buffer;
$filename = param('f');
print "Content-type: text/html\r\n\r\n";
print "\<FILE name='$filename' encoding='base64'\>\r\n";
open(FILE, "$filename") or die "$!";
while (read(FILE, $buffer, 60*57)) {
 print encode_base64($buffer);
}
print "\<\/FILE\>\r\n";

url-encode.pl
sub URLEncode {
 my $theURL = $_[0];
 my $MetaChars = quotemeta( ';,/?\|=+)(*&^%$#@!~:');
 $theURL =~ s/([$MetaChars\"\'\x80-\xFF])/"%" .
 uc(sprintf("%2.2x",         ord($1)))/eg;
 $theURL =~ s/ /\+/g;
 return $theURL;
}
Unicode to ASCII Conversion 
use Unicode::String;
local $/; # slurp mode
$line=<>;
$u=Unicode::String::utf8($line);
$latin1=$u->latin1;
print $latin1;

Load a Google-News Search as RSS Feed
use LWP::UserAgent;
# RSS feed to load
$url = "http://news.google.com/news?hl=en&ned=&q=cancer&ie=UTF-8&output=rss";
# Load data
$ua = LWP::UserAgent->new;
$ua->timeout(5);
$ua->agent("Mozilla/5.0");
$response = $ua->get($url);
if ($response->is_success()) {
# Data is content of request
$rss_xml = $response->content;
# Info and count
$result .= "XML loaded from URL '$url' ...\n";
} else {
# Load failed
$result .= "Load error for URL '$url' ...\n";
$result .= $response->status_line . "\n";
}
print $result;
 
Last Updated ( Friday, 29 December 2006 )
 
< Prev   Next >
© 2009 Home of Ferzkopp
Joomla! is Free Software released under the GNU/GPL License.