Archive for the ‘Perl’ Category

Perl valid email regular expression

Tuesday, February 1st, 2011

# Check for valid email address

/
^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z0-9-]+(\.[a-z0-9-]+)*$
/

Perl string substitution substr

Tuesday, February 1st, 2011

example:

my $text = “This is one heck of a story”;

my $swap = “heck”;

my $replacement = “hell”;

substr($text, index($text, $swap), length($swap), $replacement);

print $text;

Perl chop chomp

Tuesday, February 1st, 2011

sub spacer

{ print “<br />”; }

my $text = “This is one heck of a story”;

# remove the last character

print chop($text); spacer(); # returns y

print $text; spacer(); # we can see that it also DID alter the variable

# remove newlines from the end of a string only

$text = $text . “\n\n”;

print $text; spacer();

print chomp($text); spacer(); # again, returns # of items “chomp”ed 🙂

print $text; spacer();

Perl output pretty HTML

Tuesday, February 1st, 2011

# outputting pretty HTML

# basically a matter of including \n
# and using Perl qq function

my (@time,@days,@months,$time,$days,$months);

@time = localtime;

@days = qw(Sunday Monday Tuesday Wednesday Thursday Friday Saturday);

@months = qw(January February March April May June July August September October November December);

print qq(\n<html>\n<head>\n<title>Get the date</title>\n</head>\n<body>\n);

print qq(Today is <strong>$days[$time[6]], $months[$time[4]] $time[3]</strong>.);

print “\n</body>\n”;

print ‘</html>’;

session_start() php when to do it again!

Thursday, April 29th, 2010

I was stuck for a bit there.

I have a page that loads via AJAX, but I had to modify it to work for Google’s googlebot #! system…

The googlebot logic senses that it’s googlebot, and uses PHP includes instead of the AJAX!

Well the includes don’t need the session_start() again, but the AJAX loads DO!

So, NOT to be forgotten, session_start() will work per LOAD, hard to explain that, but you can wrap your head around it…

fancy!

BETTER SOLUTION: Sessions manager class, framework or similar…

Perl Regular Expressions cheat sheet

Monday, March 8th, 2010

/.*/ : matches anything
/.*oose/ : wild card .*

[] : class
[a-z] : any lower case letter
[A-Z] : any upper case letter
[a-zA-Z] : any upper or lower case letter, notice runs together
[a-zA-Z0-9] : any upper/lower or zero to nine
[ a-z] : any lower case letter OR a space!
[\-] : any hyphen, notice the excerpting from special inner expression functionality \
[^a] : negated class, match if sentence does NOT have ‘a’

shorthands:
\d = [0-9]
\D = [^0-9]
\w = [a-zA-Z0-9_]
\s = [ \t\n\r\f]
\S = [^ \t\n\r\f]

In search string NOT classes additional things:
^ at beginning means it must start to match
$ at the end means it must end to match
\b then string means must be beginning of word
string then \b means it must be end of word
\B before or after means it must NOT be start or end

example:
my $example = “This is still a very strange sentence”;
if($example =~ m/\bstill\b/){ print “This string HAS the exact word ‘still’ in it somewhere at least once, not a variation thereof such as stillness”; }

example 2:
my $phone = (000) 000-0000;
if($phone =~ /^\(\d\d\d\) \d\d\d\d$/) { print “phone number formatted correctly”; }