Archive for the ‘Perl’ Category
Eval Code Blocks or subroutines in a Perl Regular Expression
Some times you need some intelligence behind your Perl regular expressions, so you need to capture some pieces of text into $1, $2,… and then process them before you replace them in the string.
Simple example you need to convert multiple dates included in a big string from 12/24/2009 to 2009-12-24.
You could do the following:
my $line = "abc","cde","12/24/2009","fff","11/23/2008","sss";
my $rr = \&my_sub;
$line =~ s/(\d{1,2})\/(\d{1,2})\/(\d{4})/$rr->($1,$2,$3)/ge;
print $line;
sub my_sub {
my ($s1, $s2, $s3) = @_;
return sprintf("%04d-%02d-%02d",$s3,$s1,$s2);
}
Notice the /ge in the regular expression? That is what gets your code executed before the substitutions get made. This being Perl I know there are another 200 ways of doing this differently, and I am sure 198 are faster and simpler. Care to comment? What is your way of doing this?
Easy Debugging for your Perl Catalyst Web Applications
I decided to give Catalyst a try and see how it works for me.
When writing and code I feel that the easiest it is to debug it the better the quality of the product, as such although I love perl, I hate the perl debugger. It is all text/command line, very unfriendly and time consuming.
So long ago I decided to use a perl graphical debugger and after testing several packages I finally settled with ActiveState’s Komodo IDE.
It is a great IDE, and great debugger, where you can inspect all your code and variables in a nice, friendly way.
I also supports remote dubbuging which comes in handy to debug Catalyst applications.
- Download the Komodo IDE and remote debugging tools from ActiveState and install them locally on your system, if your using the MyApp_servre.pl to develop and test it, or in the target system where you application resides.There are several versions of the remote debugging tools, make sure the one you download matched your Komodo version, and make sure you download the right platform as well.
- tar zxf Komodo-PerlRemoteDebugging-5.1.0-27487-macosx-x86.tar.gz (in my case I am using MAC,)
- mkdir /usr/local/lib/perl
- cp -R Komodo-PerlRemoteDebugging-5.1.0-27487-macosx-x86 /usr/local/lib/perl
- vi /etc/profile
and add the lines, please replace the 55217 with the port your Komodo IDE expects, you can get that value in the Debug Menu, Listener Status (Port field).
export PERL5LIB=/usr/local/lib/perl/
export PERLDB_OPTS=RemotePort=localhost:55217
export DBGP_IDEKEY=
save /etc/profile and execute: . /etc/profile to read it (the dot is important.)
Now you can open your controller in Komodo and add the variable $DB::single=1 where you want your breakpoint.
# controller file: database.pm
sub index : Path :Args(0) {
my ( $self, $c ) = @_;
# template file we want to use
$c->stash->{template} = 'list_users.tt';
$DB::single = 1; # Debugger Breakpoint
# here we selected the ModelName::table_name
$c->stash->{users} = $c->model('OrdersDB::users');
}
- Start you application server with
perl -d script/MyApp_server.pl (-d starts debug mode) - leave your Komodo open, open your browser and point it to http://localhost:3000/database
- I you go back to Komodo you’ll your application the Debugger stopped at your breakpoint ($DB::single).
Enjoy the easy life of debugging Catalyst applications
Web Application Development
I am sort of an old school programmer, in the sense that when I started, there were no GUIs, web interfaces, or fancy IDEs. I never really developed any code professionally although I have done a few projects here and there, more for the fun of it than for profit.
I have developed a lot of small plugins for WordPress, or Trac, but never have I started a Web Application from scratch. I now want to write a small order management web app for my company but the reality is that I don’t know where to turn to. I would like to use one of the new Web Application Frameworks, but I don’t want to have to learn Ruby or Python. I am most comfortable with Perl.
So what do you suggest I do? Catalyst, Jifty, any other options?
Your comments are welcome!