PERL - Practical Extraction and Report Language

Updated on 07 Jun 2009, at 13:35:06

Special thanks to Salih Ergül, Klaus Rusch and Michael Baierl for their valuable contributions.

Misc

How can I install Perl?
See downloads at perl.com
How can I install Perl on Windows?
Where can I find Perl modules?
Where can I find Perl modules for Windows?
Where can I find a working "make" for Windows?

Variable Syntax

How can I use a variable that includes another variable?
$a = "b";
$b = "test";
print ${$a};
Output> test
How can I retrieve a hash in the order that I entered?
use Tie::IxHash;
tie %h, "Tie::IxHash";     # Keeps the input order
%h = ("key2" => "value2",
  "key1" => "value1",
  "key3" => "value3");
while (($k, $v) = each %h) {
  print "\nkey = $k, value = $v";
}
Output>
key = key2, value = value2
key = key1, value = value1
key = key3, value = value3
How can I retrieve a hash with sorted keys?
%h = ("key2" => "value2",
  "key1" => "value1",
  "key3" => "value3");
foreach $k (sort keys %h) {
  print "\nkey = $k, value = $h{$k}";
}
Output>
key = key1, value = value1
key = key2, value = value2
key = key3, value = value3
What is the length of an array?
@a = ("b", "c", "d", "e");
print scalar @a;
Output> 4

Strings

How can I replace "old" with "new" in $a?
$a = "this is old";
$a =~ s/old/new/;
print $a;
Output> this is new
How can I check if $a includes "test"?
$a = "this is a test";
print $a =~ /test/ ? "includes" : "doesn't include";
Output> includes
How can I check if $a does not include "test"?
$a = "this is a teeeeest";
print $a !~ m|test| ? "does not include" : "includes";
Output> does not include
How can I write arbitrary quotes to a string?
$a = qq{"this is 'a string' that has 'many quotes'". Ok?};
print $a;
Output > "this is 'a string' that has 'many quotes'". Ok?
How can I escape characters in a string?
$a = "\\b @ c is (d)";     # " ", "@", "(", ")" will be escaped
$a = quotemeta $a;
print $a;
Output > \\b\ \@\ c\ is\ \(d\)

Files

How can I open and close a file?
use FileHandle;
$f = "testfile.txt";
$fh = new FileHandle $f, "r" or die "Can't open file $f: $!\n";     # To read
$fh = new FileHandle $f, "w" or die "Can't open file $f: $!\n";     # To write
$fh->close;
How can I work with files recursively in a directory?
use File::Find;
use FileHandle;
$d = ".";     # Name of the starting directory
sub filePrint {
  print "Filename: $_\n";
  print "Dirname: $File::Find::dir\n";
  print "Full filename: $File::Find::name\n";
}
find(\&filePrint, $d);     # find(\&name_of_the_function, name_of_the_starting_dir)
How can I write to a file?
use FileHandle;
$f = "testfile.txt";
$fh = new FileHandle $f, "w" or die "Can't open file $f: $!\n";
print $fh "test";
$fh->close;
How can I read stdin or all files from the command line with globbing in Windows shell?
@ARGV = ($#ARGV < 0 ? @ARGV = ('&0') : map { glob } @ARGV);
How can I get the dirname and filename for a given fullname?
use File::Basename;
$fullname = "dirname/filename.txt";
$dirname = dirname $fullname;
$filename = basename $fullname;
print "dirname: $dirname\nfilename: $filename\n";

Web

How can I create a dynamic html file?
# Write the followings to cgi-bin/a.pl and invoke http://<webserver>/cgi-bin/a.pl
use CGI qw(:all);
print header;
print "Hello world!";
How can I print param()?
# Write the followings to cgi-bin/a.pl and invoke http://<webserver>/cgi-bin/a.pl
use CGI qw(:all);
print header;
@params = param();
foreach my $p (sort @params) {
  print "$p = ".param($p)."<br />";
}

Graphics

How can I create a barcode?
use GD::Barcode::EAN13;
binmode(STDOUT);
print GD::Barcode::EAN13->new('123456789012')->plot(Height=>100, Width=>800)->png;
Output: 123