Category Archives: Perl

How To Show Different Colors Per Level Using Log4perl

Use the following appender to show different colors. You can also customize the color per level. log4perl.appender.SCREEN = Log::Log4perl::Appender::ScreenColoredLevels log4perl.appender.SCREEN.color.INFO = white log4perl.appender.SCREEN.color.FATAL= bold underline blink red on_white attributes BOLD, DARK, UNDERLINE, UNDERSCORE, BLINK colors BLACK, RED, GREEN, YELLOW, BLUE, … Continue reading

Posted in Log4perl, Perl | Leave a comment

How To Print in Color Using Perl

http://perldoc.perl.org/Term/ANSIColor.html use Term::ANSIColor; print color ‘bold blue’; print “This text is bold blue.\n”; print color ‘reset’; print “This text is normal.\n”; print colored (“Yellow on magenta.”, ‘yellow on_magenta’), “\n”; print “This text is normal.\n”; print colored ['yellow on_magenta'], ‘Yellow on … Continue reading

Posted in Perl | Leave a comment

How To Log to Multiple Files Using Log4perl

How to log to multiple files using a single Log4perl logger. Example: you have an INFO level logger, but you want to log warnings and errors to separate files. You also want to log to the screen. ### Throw everything … Continue reading

Posted in Log4perl, Perl | Leave a comment

How To Access Values in Perl Hash as an Array, Given a List of Keys

my @fields = ( “country_name”, “region_name”, “city_name”, ); while (my $row = $sth->fetchrow_hashref) { my %row = %$row; printf $format, @row{ @fields }; }

Posted in Perl | Leave a comment

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”;

Posted in PHP, Perl | Leave a comment

How To Change Your CPAN urllist

# cpan cpan> o conf init … Connecting to ftp.perl.org|64.27.65.115|:21… connected. Logging in as anonymous … Logged in! ==> SYST … done. ==> PWD … done. ==> TYPE I … done. ==> CWD /pub/CPAN … done. ==> SIZE MIRRORED.BY … … Continue reading

Posted in CPAN, Perl | Leave a comment

How To Fix a Perl Memory Leak

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(); … Continue reading

Posted in Perl | Leave a comment

How To Redirect STDERR to STDOUT in Perl

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, … Continue reading

Posted in Perl | 1 Comment

Extract parts of string into array using Perl regular expression

How to go directly from string to an array using Perl Regex extraction: my ($first, $second, $third) = ( $string =~ /not(.*?)this(.*?)stuff(.*?)/ );

Posted in Perl | Leave a comment

How To Profile a Perl Script

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 … Continue reading

Posted in Perl | Leave a comment

libMagick.so.10: cannot open shared object file

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. Add /usr/local/lib to /etc/ld.so.conf Run ldconfig as root.

Posted in ImageMagick, Linux, Perl | Leave a comment

Change URLLIST in CPAN due to lagging

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.

Posted in Perl | Leave a comment

How to Fork in Perl

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) … Continue reading

Posted in Forking, Perl | Tagged | Leave a comment