A Perl Classic

Well if you have been working with Perl long enough you are more than likely to have run into the situation where you are playing with a code base that is shall we say a little dated.

Of course being a good modern Perl programmer you will want to take advantage object-oriented programing or newer version of modules that do more but need a more modern Perl.

Sometime you are stuck in one version of Perl and are forced to write some of you own modules like my Orignal which gave me rather simple attributes when faced with Perl 5.6 for a large project.

Well sometimes the opposite is true you do get the chance to upgrade your Perl and suddenly things stop to work so there can be just as many problems with upgrading and this is what I am blogging on tonight.

We are all set to update to perl 5.14 from what I think is at most 5.10 with a good deal of 5.6 code out there as well. On little asset I am keeping is my toolbox is called 'Classic::Perl' though it does not have many features it does have one I use from time to time. Namely it makes 'split' behave the way it use to so many years ago.

Split is one of thoes function calls that are had to live without and on a very old CGI code base you see it all the time, (I swear on of my long gone predecessors
was paid a bonus each time she used it)

So in what is called 'Classic Perl', or perl lower than 5.8, you often saw many a cgi program split being used in void context like this

split ("," , "bak-102,bak-103,bak-104,bak-105");
print join("
", @_);

The result is a nice little HTML formated list on your browser. In later Perl this would not print anything on the browser as split no longer evaluates into @_ in void or scalar context. Actually only removed in Perl 5.11 so not really 'Classic' by any means, so might explain why using split in this manner it is still very common to see,

Depending on how poorly the old code is written you may be limited on you options but a quick way to get it working is to simply put

use Classic::Perl 'split';

at the start of you module and then 'split' should work like it did before. There are other ways to ensure that split works the same such as specifying the perl version in you code like this

use 5.6.1;

but I find the Classic::Perl is a quick to eliminate this particular problem as it can be an annoying one to debug because the output is nothing which is far worse most times that a fatal error or a warning.

Anyway this is one that should help somebody else out and for those who don't care I here is a picture of some cute kittens



Are you sure about that use 5.6.1?

$ perl -wle'use 5.6.1; sub xxx { split /,/, $_[0]; print qq/GOT: $_/ for @_ }; xxx(q/foo,bar,baz/)'
Useless use of split in void context at -e line 1.
GOT: foo,bar,baz

split was changed to no longer modify @_ in Perl 5.12.0. My point is that unlike many recent Perl features, this is not affected by the version declaration.

Leave a comment

About byterock

user-pic Long time Perl guy, a few CPAN mods allot of work on DBD::Oracle and a few YAPC presentations