Sun Dublan
Perl Notes 

Regular Expressions

  • My IP address regular expression (two steps):

    $octet_rx = qr{(?:25[0-5]|2[0-4]\d|[0-1]?\d{1,2})};
    $ip_rx = qr{(?:$octet_rx\.){3}$octet_rx};

    This is a quicker but less accurate IP address regex:

    $ip_rx = qr{\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}};

  • To perform a regular expression on a text file:

    $ perl -pe 's/string/replace/g; ...' file.txt > file-out.txt

     

  • Force each word to be lower case with the first letter upper-case:

    $line =~ s/(\w+)/\u\L$1/g;

     

Reserved Variables

  • The $> variable

    The $> variable is the effective UID. Doing something like this: $user = (getpwuid($>))[0]; is much more efficient than doing $user = `whoami`;

    ($username, $password, $uid, $groupid, $quota, $comment, $gcos, $dir, $shell, $expire) = getpwuid($>);

     

10/25/2004 Webmaster: Troy Bowman