Disclaimer:
These pages about different languages / apis / best practices were mostly jotted down quckily and rarely corrected afterwards.
The languages / apis / best practices may have changed over time (e.g. the facebook api being a prime example), so what was documented as a good way to do something at the time might be outdated when you read it (some pages here are over 15 years old).
Just as a reminder.

Perl tips and developer notes

Code snippets and other Perl things

See Perl links for many links to other perl resources.


Perl tips

This is some scribblings for things I find myself thinking "ah, I did this some time ago, what was the syntax again...damn, what was it..eh..damn, I have a look at google.."

Date/Time/Unixtime

Date/Time/Unixtime

1. How to convert date to unixtime in perl (or vice versa)
  • time return number of seconds since 0 in your computer (1 jan 1970) on unix
  • localtime converts unixtime to normal dates
    To convert a specific date to its corresponding unixtime, you use Time::Local, the function looks like this:
      $time = timelocal($sec,$min,$hours,$mday,$mon,$year);
    so to convert 3rd of Sept 2000 to unixtime, (note that jan=0,feb=1,...):
       use Time::Local;
       $time = timelocal(0,0,0,'3','08','2000');
       
    Related links: Dates and Times in Perl. See also manpages for the functions used.

    search & replace in several files

    This is a script I use whenever I need to do search and replace in a bunch of files. It was meant as a quick hack, but since I does exactly what I need, I continue to use it (always make a backup of the original files in case something goes wrong).
      #!/usr/bin/perl -w
      #
      # More scripts and tips can be found at
      # https://www.edlin.org/
      #
      # Search and replace in several files
      #
      # I throw this file in my ~/bin/
      # Edit the variables $search, $replace and perhaps you want to change the globbing
      # then I jump to the directory with the files and just execute msr.pl (make sure that ~/bin is in your $PATH)
      
      use strict;
      
      my @infiles = glob("*.html");
      
      my $search ='dilbert';
      my $replace ='wally';
      
      # Here we go.........
      
      foreach my $file (@infiles){
        print "Processing $file
    ";
        open(FH,$file) || die "Cannot load $file";
        my @lines=<FH>;
        close(FH);
        my $match=0;
        foreach my $line (@lines){
          if($line =~ s/$search/$replace/g){
            $match=1;
          }
        }
      
        if($match){
          print "...Saving $file\n";
          open(FS,">$file") || die "Cannot save $file";
          print FS @lines;
          close(FS);
        }
      } 
      

    output

    when hacking some scripts it is useful to use $0 when printing out stuff, that way you can figure out which scripts that generate the output if you end up having scripts that use others scripts etc.
      print "$0: Some output from script $0\n";
      

    random integers

      my $dilbert = int ( rand(10) ) ;
      
    will generate an integer from 0 to 9 before Perl 5.004 you have to call srand; before, e.g.
      srand;
      
      my $dilbert = int ( rand(10) ) ;
      

    crypt

    Generate for example encrypted password "mypasswd" for cvs
      #!/usr/bin/perl
      
      my $passwd = crypt("mypasswd","dilbertwallyrandomtext");
      
      print "$passwd\n";
      

    Make filenames lowercase

        #!/usr/local/bin/perl -w
    
        if(!@ARGV){
            print "Will make *.GIF/JPG to lowercase\n";
            exit 0;
        }
        while($x = <'*.GIF'>) {
            $tt =lc $x;
            `mv $x $tt`;
        }
    
        while($x = <'*.JPG'>) {
            $tt =lc $x;
            `mv $x $tt`;
        }
    

    More programming related pages

    Workflow: Release process
    Workflow: Bug tracking
    Teambox (Redbooth) - Mantis connector
    Design Patterns
    Git & Github
    Go / Golang
    CVS
    CVS backup script
    Distribution process
    Installation script
    Java Server Faces
    Facelets
    jibx
    jBoss
    jBpm
    Perl tips
    Perl links
    PostgreSQL
    Python / py2app
    Shell scripts
    Xslt
    Node.js
    Facebook / Opengraph
    PHP developer notes
    Redbooth API through php
    Website optimization
    jqTableKit demo
    Javascript / html / css links










  •