Softpanorama
(slightly skeptical) Open Source Software Educational Society

May the source be with you, but remember the KISS principle ;-)

Google   


Perl Tips/Snippets

News Recommended Links Perl Style Perl Programming Environment Perl as a command line utility tool Perl Debugging

 

AWK one liners Shell Tips and Tricks VIM Tips Humor Etc
  1. Dynamic activation of the debugger (from Perl debugged):
    while (<INPUT>)
       {
       $DB::trace = 1, next if /debug/;
       $DB::trace = 0, next if /nodebug/;
       # more code
       }

    When run under the debugger, this enables tracing when the loop encounters an input line containing "debug" and ceases tracing upon reading one containing "nodebug". You can even force the debugger to breakpoint by setting the variable $DB::single to 1, which also happens to provide a way you can debug code in BEGIN blocks (which otherwise are executed before control is given to the debugger).
     

  2. Sometimes it makes sense to use regular expressions instead of substr. One such task is extraction of component of date, for example:

    $cur_date='20060325';

    (year, $month, $day)=$cur_date=~/(\d{4})(\d\d)(\d\d)/;

  3. Perl cross-reference reports. The B::Xref module can be used to generate cross-reference reports for
    Perl programs.

    perl -MO=Xref[,OPTIONS] scriptname.plx
     

  4. Setting a value of parameter to default value

    # --- process the second parameter 
    $msglevel=($ARGV[1]) ? $ARGV[1] : $msglevel; # defaults is the three digit constant(see below)
    ($msglevel1, $msglevel2, $testing) = split(//,$msglevel); # get flags
  5. Creating timestamp
    # Timestamp
    #
    ($sec,$min,$hour,$mday,$mon,$year,$wday,$yday,$isdst)=localtime(time);
    $year+=1900;
    $mon++;
    for ($mon, $mday, $hour, $min, $sec) {
    	if (length($_)==1) {
    		$_="0$_";
    	}
    } 
  6. Move via link/unlink (should be the same filesystem)
    
    	
    link($_[0], $target);
    if (-e $target) {
    unlink($_[0]);
    } else {
    logger("SFail to move the file '$_[0]' to '$home/$_[1]/$target' \n");
    return;
    }
  7. Removing duplicates: here the second part will be executed only if $new{$match} is still undefined:
    if ( $new{$match}++ || !( $tags{$match} = sprintf( "%s\t%s\t?^%s\$?\n", $match, $ARGV, $_ ) ) )
     

Recommended Links


In case of broken links please try to use Google search. If you find the page please notify us about new location
Google     

oreilly.com -- Online Catalog Perl Cookbook Chapter 8: File Contents (HTML Format)

Perl tips Understanding flow control

Dekan's Solace - Perl Tips

Perl tricks and tips

 




Copyright © 1996-2008 by Dr. Nikolai Bezroukov. www.softpanorama.org was created as a service to the UN Sustainable Development Networking Programme (SDNP) in the author free time. Submit comments This document is an industrial compilation designed and created exclusively for educational use and is placed under the copyright of the Open Content License(OPL). Original materials copyright belong to respective owners. Quotes are made for educational purposes only in compliance with the fair use doctrine.

Standard disclaimer: The statements, views and opinions presented on this web page are those of the author and are not endorsed by, nor do they necessarily reflect, the opinions of the author present and former employers, SDNP or any other organization the author may be associated with. We do not warrant the correctness of the information provided or its fitness for any purpose.

Last updated: October 11, 2008