Perl

Perl is a scripting language that excels at text processing.

Contents

[edit] 1 Library

[edit] 2 Snippets

[edit] 2.1 Starting a script

Use

#!/usr/bin/env perl

and not

#!/usr/bin/perl

The latter may not work in all environments.

Also make sure to include:

use warnings;
use strict;

[edit] 2.2 Trim function

Like PHP's trim:

sub trim
{
  map { /^\s*(\S.*\S)\s*$/ } @_;
}

[edit] 2.3 Slurp function

This function puts all of a file's contents and returns a string.

sub slurp
{
    my $file = shift( @_ );
    
    my $old_terminator = $/;
    undef $/;
    open( SLURP, $file );
    my $slurp = <SLURP>;
    close( SLURP );
    $/ = $old_terminator;
    
    return $slurp;
}

[edit] 2.4 Change endlines

This will change all Mac and Windows endlines in a file to Unix endlines:

perl -p -i.orig -e 's/\r\n|\r/\n/g' FILENAME

The original file will end up as FILENAME.orig.

[edit] 2.5 Cycle through a directory

opendir( DIR, "$directory_path" ) || die "Cannot open dir $directory_path\n";

while( my $file = readdir( DIR ))
{
  //do stuff!  
}

close( DIR );

[edit] 3 Resources

[edit] 4 References

[edit] 4.1 O'Reilly Safari Library

XHTML 1.1 CSS 2 Sec 508