NB: These pages were mostly written in 2001 or so. The résumé dates are accurate but the code is aged and unlike whiskey, 8 year-old code doesn't usually taste better. For a look at my current skills and to see my CPAN modules, sample code, and code discussions, please see these pages instead: Perl resources and sample code and PangyreSoft.
Various recipes in Perl and JavaScript
Social links
View Ashley Pond V's profile on LinkedIn

Other pages

Perl: TT2 context

A quick snippet useful for letting your templates know their context.

my $pre_define = {
    Server => $ENV{SERVER_NAME},
    Live => $ENV{SERVER_NAME} =~ /\.com$/ ? 1 : 0,
# or in PRE_DEFINE if you want them for PRE_PROCESS
my $Config = { VARIABLES => $pre_define, };
my $template = Template->new($Config);
$template->process( ... );

Then your templates know if they are live and what their proper server is. Eg, [% Live ? 'Live server!' : 'Dev server' %]

Here’s a snippet to get the server name with JavaScript too.

Perl: Ordinals and a TT2 filter

There is a module by the lovely Sean Burke to do abbreviated ordinals already: Lingua::EN::Numbers::Ordinate. Still, it’s an interesting and pretty easy problem, and the module is pretty obscure so it’s not likely to be on your webhost. There’s also an example of how to use it as a Template Toolkit filter.

Ordinal subroutine
Test a list of ordinals
print ordinal($_), " " for 0 .. 21;
0th 1st 2nd 3rd 4th 5th 6th 7th 8th 9th 10th 11th 12th 13th 14th 15th
16th 17th 18th 19th 20th 21st
As a Template::Toolkit filter
my %Config =
    FILTERS => { ordinal => sub { ordinal($_[0]) }
my $tt2 = Template->new(\%Config);
# et cetera ...
Then you can call it in your templates
This is your [% user.visits | ordinal %] visit!

Here is a link to show you how you can do ordinals with JavaScript. Or use Perl and the Lingua::EN::Number::Ordinate with TT2.

JavaScript: ucFirst()

Prototyping in JavaScript is a great way to extend base object types. In this case we find JavaScript’s string handling lacking compared with Perl’s. All we want it to titlecase a string just like Perl’s ucfirst(). So we add the Perl functionality to JavaScript strings by adding a prototype to the String object.

Here is a technique for doing titlecase with JavaScript.

//----- (c)GPL, apv
String.prototype.ucFirst = function () {
   return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
<script type="text/javascript">
var me = new String('ashley')
document.write( me.ucFirst() )

Perl: Kill unprintables

Sometimes characters make it into your data that just don’t belong. This recipe kills them dead (please don’t sue me, Raid) by substituting them out. Be careful, It alters files in place but it will give you a backup with the suffix .bk in the same folder/directory so you have the original saved if it does something terrible. Warning: Don’t use it on binary files (images, database files, etc); they’re supposed to contain what they contain.

It’s a one liner and it’s feedback free.

Kill unprintables code
perl -pi.bk -e 's/[^\s[:print:]]//g'
Kill unprintables usage
jinx[607]>perl -pi.bk -e 's/[^\s[:print:]]//g' [filenames]

JavaScript: Referrer query catcher

This little function allows you to get the query term out of a referring URL. So if someone finds you on a search engine, you have instant access to how they did it. The information is also probably available from your web logs but this way you can use it dynamically in a page for feedback or autopopulating boxes or selecting related items to display on the page.

The regex below isn’t perfect but it catches the query correctly in better than 95% of the search engines I’ve run it against. Certain search engines, like AOLsearch, don’t keep the query in the URL so it catches the query ID instead.

In a file named “utils.js”
//----- might be a better version but this is pretty solid
//----- (c)GPL, apv
function getRefQuery () {
  var Refer = document.referrer
  if ( ! Refer ) return false
  q = Refer.match(/(q(([^=]*)|kw)?|p)=([^"&;]+)/)
  if ( ! q ) return false

//----- no cross-script hacking, please
  var qry = unescape( q[ ( q.length - 1 ) ] )
  qry = qry.replace(/</g, '&lt;')
  qry = qry.replace(/>/g, '&gt;')
  qry = qry.replace(/\+/g, ' ')
  if ( ! qry.match(/\w/) ) return false
  return qry
//----- I like javascript now
In the html of a page

<head> <script type="text/javascript" src="util.js"></script> <title>...</title> </head><body> You came here by searching for: <script type="text/javascript"> var q = getRefQuery() document.write( q ? q : 'nothing in particular' ) </script> <noscript> ...you have to turn on JavaScript, 'fraidy cat </noscript>
Live demo of it
You came here searching for 

Perl: random hex colors

Creating random hex colors in perl in regular (6 chars) and CSS-lite (3 chars).

for ( 1 .. 8 ) {
    print random_hex_color(), " ";
print "\n";
for ( 1 .. 12 ) {
    print random_hex_color(3), " ";
#98b6e3 #18dcc9 #4f79f5 #2a8513 #c5c7b0 #77953c #d95481 #8ba629
#ba9 #5a8 #b46 #968 #d7b #929 #885 #234 #987 #c68 #de0 #6a4
Search these pages via Google
Text, original code, fonts, and graphics ©1990-2009 Ashley Pond V.