|
Various recipes in Perl and JavaScript
|
Social links
Class::Prototype
WWW::Spyder Javascript tricks serial() join function Smart quotes Text to Excel Developing Featherweight Web Services with JavaScript
Miscellaneous
|
|
| Various recipes in Perl... |
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, 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 sub ordinal { $_[0] =~ /^(?:\d+|\d[,\d]+\d+)$/ or return $_[0]; return "$_[0]nd" if $_[0] =~ /(?<!1)2$/; return "$_[0]rd" if $_[0] =~ /(?<!1)3$/; return "$_[0]st" if $_[0] =~ /(?<!1)1$/; return "$_[0]th"; } 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. ucFirst //----- (c)GPL, apv
String.prototype.ucFirst = function () {
return this.substr(0,1).toUpperCase() + this.substr(1,this.length);
} Usage <script type="text/javascript">
var me = new String('ashley')
document.write( me.ucFirst() )
</script> 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, '<')
qry = qry.replace(/>/g, '>')
qry = qry.replace(/\+/g, ' ')
if ( ! qry.match(/\w/) ) return false
return qry
}
//----- I like javascript now Usage In the html of a page 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). Code sub random_hex_color { # only allowed values are 3 and 6, 6 is default my $size = shift; $size = 6 if $size !~ /^3|6$/; my @hex = ( 0 .. 9, 'a' .. 'f' ); my @color; push @color, @hex[rand(@hex)] for 1 .. $size; return join('', '#', @color); } Usage 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 |
|
|
Perl Books ·
CPAN ·
mod_perl ·
Perl Monks ·
Perl Mongers ·
Perl Journal ·
Use Perl ·
Perl Jobs ·
ActiveState ·
perldoc.perl.org ·
O’Reilly Perl ·
W3Schools tutorials ·
Ovid's CGI Course ·
Catalyst ·
Perl at Wikipedia
Text, original code, fonts, and graphics ©1990-2008 Ashley Pond V. |
||