Perl Version of PHP’s “ucwords” Function
my $string = 'this is a string of text'; $string =~ s/\b(\w+)\b/ucfirst($1)/ge; print "$string\n";
my $string = 'this is a string of text'; $string =~ s/\b(\w+)\b/ucfirst($1)/ge; print "$string\n";
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
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.
How to go directly from string to an array using Perl Regex extraction:
my ($first, $second, $third) = ( $string =~ /not(.*?)this(.*?)stuff(.*?)/ );
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.
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.
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.
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);
}
Recent Comments