Home   Notes   Contact Me

Perl

Local

External


HTML Server

my $d = HTTP::Daemon->new( LocalAddr => '127.0.0.1', LocalPort => 6123, ) 
	|| die "could not start http daemon";
 
print "Please contact me at: <URL:", $d->url, ">\n";
 
while (my $c = $d->accept) 
{
    while (my $r = $c->get_request) 
    {
        if ($r->method eq 'PUT')  
        {
                  print $r->as_string();
 
                  $h = HTTP::Headers->new;
                  $h->header('Connection' => 'close');
                  $resp = HTTP::Response->new("200", "Ok", $h);
                  $c->send_response($resp);
        }
        else 
        {
            $c->send_error(RC_FORBIDDEN)
        }
    }
    $c->close;
    undef($c);
}

File IO

You can open a file for input or output using the open() function. It's documented in extravagant detail in perlfunc and perlopentut, but in short:

open(INFILE,  "input.txt")   or die "Can't open input.txt: $!";
open(OUTFILE, ">output.txt") or die "Can't open output.txt: $!";
open(LOGFILE, ">>my.log")    or die "Can't open logfile: $!";

You can read from an open filehandle using the <> operator. In scalar context it reads a single line from the filehandle, and in list context it reads the whole file in, assigning each line to an element of the list:

my $line  = <INFILE>;
my @lines = <INFILE>;

Reading in the whole file at one time is called slurping. It can be useful but it may be a memory hog. Most text file processing can be done a line at a time with Perl's looping constructs.

The <> operator is most often seen in a while loop:

while (<INFILE>) {     # assigns each line in turn to $_ 
	print "Just read in this line: $_";
}

We've already seen how to print to standard output using print(). However, print() can also take an optional first argument specifying which filehandle to print to:

print STDERR "This is your final warning.\n";
print OUTFILE $record;
print LOGFILE $logmessage;

When you're done with your filehandles, you should close() them (though to be honest, Perl will clean up after you if you forget):


String Compares

Assuming: $x = 'a';
Comparison String Operators Example Result
Equal to eq $x eq 'a' true
Not equal to ne $x ne 'b' true
Less than lt $x lt 'b' true
Greater than gt $x gt 'b' false
Less than or equal to le $x le 'b' true
Greater than or equal to ge $x ge 'b' false

Flushing Output

$|=1;

I have no idea what this means, but it flushes everything.


Subroutines

sub ShowMyParas {
  print "my paras are @_\n";
  print "1st arg @_[0]\n";
  print "2nd arg @_[1]\n";
}

Variables

ExampleDescription
$varScalar (non-array) variable
$aVar[28]29th element of array @aVar
$p = \@aVarp is now a reference to @aVar
$$p[28]29th element of array @aVar (assuming $p = \@aVar)
$p->[28]29th element of array @aVar (assuming $p = \@aVar)
$aVar[-1]Last element of array @aVar

Program Output, Getting

$foo = `ls`;

Quoting

' Single Quoted, no interpretation '\n' prints \n
" Double Quoted, interprets "\n" prints a new newline

Quoting info at perlhorizons