Cool/useful short examples of Perl?

David Precious davidp at preshweb.co.uk
Mon May 30 13:54:33 BST 2011


On Monday 30 May 2011 11:40:57 Leo Lapworth wrote:
> Hi,
> 
> I'm working on http://learn.perl.org/ and I'd like to have a few rotating
> example of what can be done with Perl on the home page.
> 
> The first two I've thought of are below, does anyone have others?
> 
> They don't have to use CPAN modules, one liners are fine as long as it
> is simple to see what they do. I'll have a 'more' link which goes on to
> show full example with line by line explanations.

Disclaimer: this is one of my modules so I'm obviously biased, but I figure 
it's a reasonable example of a common task being made easy - doing a database 
query and outputting the result as a HTML table quickly:

use HTML::Table::FromDatabase;

my $sth = $dbh->prepare('select * from widgets');
$sth->execute;

my $table = HTML::Table::FromDatabase->new(
    -sth => $sth,
    -borders => 0,  # all HTML::Table options are valid
);
$table->print;


Another good example would be the newish edit_file() / edit_file_lines() from 
File::Slurp, allowing stupidly-easy in-place editing of files, for instance:

edit_file { s/foo/bar/g } 'filename' ;
edit_file_lines { $_ = '' if /foo/ } 'filename' ;


Cheers

Dave P

-- 
David Precious  ("bigpresh")
http://www.preshweb.co.uk/

   "Programming is like sex. One mistake and you have to support
   it for the rest of your life". (Michael Sinz)


More information about the london.pm mailing list