Archive

Archive for the ‘Perl’ Category

Perl Version of PHP’s “ucwords” Function

December 15th, 2009 Masao 1 comment
my $string = 'this is a string of text';
$string =~ s/\b(\w+)\b/ucfirst($1)/ge;
print "$string\n";
Categories: PHP, Perl Tags:

How To Fix a Perl Memory Leak

September 3rd, 2009 Masao No comments

If you’re using HTML::TreeBuilder, you might find a memory leak when creating lots of trees. You must explicitly delete the trees because HTML::Tree does not use weak refs. Here’s how to delete the ref:

use HMTL::TreeBuilder;

my $formatter = HTML::FormatText->new();

my $html_tree = HTML::TreeBuilder->new_from_content($line);
my $plain_text = $formatter->format($html_tree);
$html_tree->delete();

Or if you use HTML::Parse:

my $html_tree = parse_html($line);
$html_tree->eof();
my $plain_text = HTML::FormatText->new()->format($html_tree);
$html_tree->delete();

Or using Object::Destroyer

use Object::Destroyer;

my $html_tree = parse_html($line);
$html_tree = Object::Destroyer->new($html_tree, ‘delete’);
$html_tree->eof();

my $plain_text = HTML::FormatText->new->format($html_tree);

Source: perlmonks.org

Categories: Perl Tags:

How To Redirect STDERR to STDOUT in Perl

August 28th, 2009 Masao 1 comment

Redirecting STDERR to STDOUT in Perl:

open STDERR, '>&STDOUT';

Reassign raw file handles:

*STDERR = *STDOUT;

Redirect all output to a logfile:

open(LOG,">/tmp/foo.log");
*STDERR = *LOG;
*STDOUT = *LOG;

Example:

#!/usr/bin/perl
use strict;
use warnings;
warn "regular error";
open STDERR, '>&STDOUT';
warn "redirected error";

Output:

[kitamura@web3 perl]$ ./stderr.pl 2>/dev/null
redirected error at ./stderr.pl 6.
Categories: Perl Tags:

Extract parts of string into array using Perl regular expression

November 25th, 2008 Masao No comments

How to go directly from string to an array using Perl Regex extraction:

my ($first, $second, $third) = ( $string =~ /not(.*?)this(.*?)stuff(.*?)/ );
Categories: Perl Tags:

How To Profile a Perl Script

June 17th, 2008 Masao No comments

Perl Profiling can be done by using the DProf debugging switch:

# perl -d:DProf test.pl
hello

# ls
test.pl  tmon.out

# dprofpp tmon.out

Total Elapsed Time = 0.009982 Seconds
User+System Time = 0.009982 Seconds
Exclusive Times
%Time ExclSec CumulS #Calls sec/call Csec/c  Name
100.   0.010  0.010      1   0.0100 0.0100  warnings::BEGIN
0.00       – -0.000      1        -      -  strict::import
0.00       – -0.000      1        -      -  strict::bits
0.00       – -0.000      1        -      -  warnings::import
0.00       -  0.010      2        – 0.0050  main::BEGIN

Common options:

-O cnt Show only cnt subroutines. The default is 15.

-r Display elapsed real times rather than user+system times.

-s Display system times rather than user+system times.

-u Display user times rather than user+system times.

Categories: Perl Tags:

libMagick.so.10: cannot open shared object file

June 3rd, 2008 Masao No comments

Can’t load ‘…/auto/Image/Magick/Magick.so’ for module Image::Magick: libMagick.so.10: cannot open shared object file: No such file or directory at /usr/lib/perl5/5.8.8/i386-linux-thread-multi/DynaLoader.pm line 230.

  1. Add /usr/local/lib to /etc/ld.so.conf
  2. Run ldconfig as root.
Categories: ImageMagick, Linux, Perl Tags:

Change URLLIST in CPAN due to lagging

May 7th, 2008 Masao No comments

To remove the first entry in the urllist, do this:

o conf urllist shift

This will make CPAN use the next FTP site in the queue.

Categories: Perl Tags:

How to Fork in Perl

March 26th, 2008 Masao No comments

Kick off multiple processes simultaneously in Perl.

my $num = 10;

my @children;

for (my $i = 0; $i < $num; $i++) {
  my $pid = fork();
  if ($pid) { # parent
    push @children, $pid;
  } elsif ($pid == 0) { # child
    print "child $i\n";
    sleep 5;
    exit;
  } else {
    print STDERR "couldn't fork\n";
  }
}

foreach my $child (@children) {
  waitpid($child, 0);
}
Categories: Forking, Perl Tags: