Archive for the ‘Perl’ Category
I had a need to build an array of months between a set month and the current month. My purpose was to let the user choose a month to build a report for.
I’m using HTML::Mason, but you get the idea.
You’ll need Date::Manip::Recur
<select name="months" id="months">
% # Loop through the date objects
% for (@dates) {
% # Assign the current month value to a scalar.
% my $curMonth = $_->value();
% # I only cared to see YYYYMM so I regex'd it.
% $curMonth =~ s/^(\d{4})(\d{2}).*$/$1$2/;
<option value="<% $curMonth %>"><% parseMonth($curMonth) %></option>
% }
</select>
<%init>
# Returns a date like "2011-01-01"
my $monStart = getOldestEntry($account);
use Date::Manip::Recur;
my $recur = new Date::Manip::Recur;
# Parse out the first of the month for every month since start until today.
my $err = $recur->parse('0:0*0:0:0:0:0',$monStart,$monStart,"Today");
# Return an array object of dates
my @dates = $recur->dates();
sub parseMonth {
use Time::Local;
use Date::Format;
my $md = shift;
# Match on YYYYMM
$date =~ m/^(\d{4})(\d{2})/;
my $year = $1; my $mon = $2;
# Convert to epoch
my $time = timelocal(0,0,0,1,$mon-1,$year);
# Return back (%B %Y) as "August 2010"
return(time2str("%B %Y", $time));
}
</%init>
I needed to quickly generate a full directory for a receptionist console. Since Trixbox doesn’t do this, I wrote some Perl to do so. It’s pretty simple, you will need to install Polycom::Contact::Directory from CPAN. It will connect to the localhost MySQL server and pull all extensions out, make an xml and save it to the appropriate path. You will need to supply the MAC address, I guess I could modify a bit to pull the MAC out of the Endpoint Manager table, but I like being able to just supply the MAC.
Thanks Zachary Blair for the easy module!
#!/usr/bin/perl -w
# Quick script to hack out a directory for a mac address. I use it for the
# receptionist's BLF on her IP650 with sidecars.
use strict;
use Polycom::Contact::Directory;
use DBI;
# Grab the MAC address from ARGV and make a file
my $mac = $ARGV[0] or die "No MAC Specified\n";
my $contactFile = "/tftpboot/polycom/contacts/$mac-directory.xml";
# Create a new Empty Directory
my $dir = Polycom::Contact::Directory->new();
# Connect to the trixbox MySQL DB
my $dbh = DBI->connect('dbi:mysql:asterisk:localhost:3306','root','passw0rd',{ RaiseError => 1});
# Pull an array ref for the extensions
my $userAry = $dbh->selectall_arrayref("SELECT extension,name FROM users ORDER BY extension");
$dbh->disconnect();
# Set counter for speed dial index
my $x = 1;
# Loop through extensions
for my $a (@$userAry) {
# Split the trixbox name into first and last.
my ($fn,$ln) = split(/\s+/,$a->[1],2);
# My contacts are generally dirty, I'll make them look better. Some people may want
# to comment this out if you have people with unique capitalization.
$fn = ucfirst(lc($fn));
$ln = ucfirst(lc($ln));
# Insert the record into the object.
# I like the labels to be: extension firstname lastname "3721 Awesome Dude"
# -- buddy_watching lets the polycom monitor BLF status. For this to work,
# you must have feature.1.name="presence" feature.1.enabled="1" in
# /tftpboot/sip.cfg
# -- Check Polycom::Contact Documentation for Options
$dir->insert(
{ first_name => $fn, # <fn> in xml
last_name => $ln, # <ln> in xml
contact => "$a->[0]", # <ct> in xml
label => "$a->[0] $fn $ln", # <lb> in xml
buddy_watching => 1, # <bw> in xml
speed_index => $x, # <sd> in xml
buddy_block => 0, # <bb> in xml
auto_divert => 0, # <ad> in xml
auto_reject => 0, # <ar> in xml
},
);
$x++;
}
# Save the contact file.
$dir->save($contactFile);
1;

Updated the corporate website today to include memcached. It was hitting our legacy application’s MSSQL database (which we have to still use), a ton, and slowing down the *choke* windows application.
Anyways, memcached saved the day! Way less hits on the database, and only took a few simple hooks to implement! I know I could have used Mason’s cache, but it isn’t distributed across servers that were not on this web server.
We use HTML::Mason for the site, so just a few simple hooks did the job.
1) Preloaded the Cache::Memcached module into my mod_perl.
2) Most of the website is driven off part number lookup. Even non parts are actually parts in our database, but just have content associated with them. So in the part retrieve Mason page, I added a line to load up memcached.
my $memd = new Cache::Memcached {
'servers' => [ "10.10.1.44:11211", "10.10.1.40:11211" ],
};
I get a $pn variable in from all other places so I check for its existence in the cache.
$mPart = $memd->get($pn);
Then just add a hook around my standard DB call and then a set after the pull and assign if we hit an else.
if (!$mPart) {
$partList = $dbh->selectrow_arrayref("SELECT blablabla from priceBook WHERE itemID = '$pn'");
$memd->set($pn,$partList,600); # Expire cache at 10 minutes (600 seconds).
} else {
$partList = $mPart;
}

Houston and Vienna for YAPCs Next Year (The Perl Foundation)
This is great! Now I don’t have to go out of town to attend the perl conference. Last year it was in Toronto, work was short on money to send me there, next year I’ll be able to just drive to it.
Wow. I didn’t realize allowing MT to trackback would get so much of a response for my rant! Brian answered a comment, and now I feel retarded. Maybe next time I feel like trackbacking, I need to re-read stuff before I comment on it, and maybe do a little bit of research while im at it.
I’m gonna have to hide my face at the next YAPC.. good thing its close to a year from now
Edit: Sorry brian d foy. See comments.
brian d foy writes in this article, O’Reilly Network: Perl versus PHP 5, that he’s seen a lot of complaints about how hard it is to install modules in Perl. Now either he doesn’t use Perl, or maybe he just doesn’t know.
perl -MCPAN -e 'install Module::Name'
That’s it. PHP doesn’t have a vast module network. I don’t hate PHP, but sooo many security issues, im ALWAYS upgrading mod_php. Perl is my language of choice. I’ve used python/php/tcl and I always come back to Perl.
Day three was good.. I really enjoyed Damian’s talk.. Overall good conference this year.. the University at Buffalo was a great place to host it. They had wireless everywhere, and plenty of places to eat on campus.. Go to tour the supercomputing lab. That was fun. Went to Niagra falls last night, and hopped the bridge to Canada..
Anyways.. I’m in the Buffalo airport, gotta get some food and hop on a place to Cleveland, OH, then to Houston, and I’m home..
YAPC day 2 going on.. I’m listening in on “Advanced Regexs” had a sub for lunch, and two cookies. Uhh thats it for now.. just keeping the site up to date.
Well, I’m here at YAPC 2004. Sitting here on the wireless, listening to the keynotes and Perl upcoming releases speech and such.
The hotel SUCKS!! The University Inn, in Buffalo, NY is horrible customer service wise. I got in last night and they said “Hey, you were supposed to be here yesterday, we canceled your reservation.” So they stuck me in a damn sofa bed for the night, and basically told me its my fault. Even though I have a confirmation email stating that my room has been reserved for the 15th and not the 14th!!! These people need a bitch slappin!
I need to strive to be more like Derek..











