Parsing GET/POST requests[edit]
Use CGI it's more reliable:
eg:
use CGI qw(:standard param); my $field = param('field')
Print Environment Variables[edit]
while (($key, $value) = each(%ENV)) { print "<br>$key -> $value"; }
A simple server[edit]
#! /usr/bin/perl -w # server0.pl use strict; use Socket; # use port 7890 as default my $port = shift || 7890; my $proto = getprotobyname('tcp'); # create a socket, make it reusable socket(SERVER, PF_INET, SOCK_STREAM, $proto) or die "socket: $!"; setsockopt(SERVER, SOL_SOCKET, SO_REUSEADDR, 1) or die "setsock: $!"; # grab a port on this machine my $paddr = sockaddr_in($port, INADDR_ANY); # bind to a port, then listen bind(SERVER, $paddr) or die "bind: $!"; listen(SERVER, SOMAXCONN) or die "listen: $!"; print "SERVER started on port $port\n"; # accepting a connection my $client_addr; while ($client_addr = accept(CLIENT, SERVER)) { # find out who connected my ($client_port, $client_ip) = sockaddr_in($client_addr); my $client_ipnum = inet_ntoa($client_ip); my $client_host = gethostbyaddr($client_ip, AF_INET); # print who has connected print "got a connection from: $client_host","[$client_ipnum]\n"; # send them a message, close connection print CLIENT "Smile from the server"; close CLIENT; }
(http://www.devarticles.com/art/1/430/(3)
Perl Split File[edit]
Takes a file and splits it into individual files. The delimeter between each file is ~filename, eg it takes a file formatted:-
~output1 blah blah blah ~output2 moo moo moo
and creates two files titled output1 and output2 with the respective contents from the original file:
#!/usr/bin/perl $filename=$ARGV[0]; $openhandle=0; open (DATA,"<$filename"); while (<DATA>) { if (/^~(.*)/) { if ($openhandle) { close OUTPUT; } open (OUTPUT, ">$1"); $openhandle=1; } elsif ($openhandle) { print OUTPUT; } } close OUTPUT; close DATA;
- strip white space: s/\s+$//;