Human Oriented

Subscribe to RSS

Some rights reserved

Except where otherwise noted, content on this site is licensed under a Creative Commons License


programming‎ > ‎

perl

"Perl is designed to give you several ways to do anything, so
consider picking the most readable one"
                            Larry Wall in the perl man page

My Perl scripts at bitbucket

hg clone http://bitbucket.org/dfa/perl-stuff/

Matching 'keyword = value' pairs

m/(\w+)\s*=\s*(.*)\s*$/ # keyword is $1, value is $2

Matching date MM/DD/YY HH:MM:SS

m|(\d+)/(\d+)/(\d+) (\d+):(\d+):(\d+)|

Removing leading and trailing blanks (trim)

s/^\s+//; s/\s+$//;

Extracting all numbers from a string

@nums = m/(\d+\.?\d*|\.\d+)/g;

IP address

m/^([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])\. ([01]?\d\d|2[0-4]\d|25[0-5])\.([01]?\d\d|2[0-4]\d|25[0-5])$/;


Pearls in Perl


from http://wonkden.net/explanation.html:

print chr ord scalar qw eof write and
print qq pop xor print chr ord qq and
quotemeta xor lc print chr ord scalar
qw seek fork x keys and print chr ord
reverse q each y binmode xor uc print
chr ord q defined and int srand print
chr ord reverse q kill hex print link
and print chr oct ord sqrt oct uc ord
scalar qw pack dump xor print chr ord
reverse q do rand xor print chr ord q
read qr xor print chr int ord reverse
qw fileno s printf and print chr sqrt
ord reverse q each chomp pack binmode

What a wonderful hack!