<< >>
justin = { main feed , music , code , askjf , pubkey };
[ <<< last (older) article | view in index | next (newer) article >>> ]

December 19, 2010
Joy, from Perl

I've recently started using Perl for text wrangling instead of PHP -- it started after much of my PHP included a lot of preg_replace and preg_match calls, which got me wondering...

There's a certain satisfaction I get from perl that I don't really get from other languages. There's something about the density that makes it quite readable, at least for the author. Here's a script I wrote to automatically add a new build configuration, based on an existing build configuration, with some modifications, to a given .dsp (VC6 project file):

    
    #!/usr/bin/perl
    $sp = "Win32 Release";
    $np = "Win32 Nitpicker";
    $spd="Release";
    $npd="Nitpicker";
    $spdir="/Release/";
    $npdir="/Nitpicker/";
    $libfile = "../../nitpicker/libcmt_nitpick.lib";
    while (<>)
    {
      chomp;
      s/\r$//;
      s/\n$//;
       
    
      print $_ . "\r\n";
      if (/^!ELSEIF .*$sp".*$/ || /^!IF .*$sp".*$/)
      {
        s/^!IF/!ELSEIF/;
        s/$sp/$np/;
        @nc = ($_);
        while (<>)
        {
          chomp;
          s/\r$//;
          s/\n$//;
          if (/^!/)
          {
            my $tmp=$_;
            foreach (@nc)
            {
    	  s/$spd/$npd/g;
    	  s/$spdir/$npdir/g;
    	  if (/^# ADD BASE CPP/)
    	  {
    	    s/\/Z\S *//;
    	    s/\/YX *//;
    	    s/\/FR *//;
    	    s/\/FD *//;
    	    s/$/ \/FR \/FD \/Zi/;
    	  }
              if (/^# ADD CPP/)
    	  {
    	    s/\/Z\S *//;
    	    s/\/M\S *//;
    	    s/\/O\S *//;
    	    s/\/FR *//;
    	    s/\/FD *//;
    	    s/$/ \/FR \/FD \/Zi \/MT \/Ot \/Og \/D "DEBUG_TIGHT_ALLOC"/;
    	  }
    	  if (/^# ADD LINK32/)
    	  {
    	    s/^# ADD LINK32 /# ADD LINK32 $libfile /;
    	    s/\/debug *//;
    	    s/\/out:/\/nodefaultlib:"LIBCMT" \/out:/;
    	    s/$/ \/debug/;
    	  }
    	  if (/^# ADD BASE LINK32/)
    	  {
    	    s/\/debug *//;
    	    s/\/map *//;
    	    s/\/dll /\/dll \/debug /;
    	    s/$/ \/fixed:no/;
    	  }
              print $_ . "\r\n";
            }
            print $tmp;
            last;
          }
          print $_ . "\r\n";
          push(@nc,$_);
        }
      }
      if (/^!MESSAGE.*$sp".*$/ || /^# Name ".*$sp" *$/)
      {
        s/$sp/$np/;
        print $_ . "\r\n";
      }
    }
    
In case anybody is interested, Nitpicker is something we've been working on that is similar to Electric Fence or other memory debuggers, but better suited for our workflow/design/etc. We'll probably GPL it once it matures.






6 Comments:
Posted by Tale on Mon 20 Dec 2010 at 01:22 from 77.168.115.x
Yeah, Perl rocks (and PHP sucks)! When I started with Perl I initially used it only for CGI scripts on our webserver, but now I use it for anything really.


Posted by Will on Sat 01 Jan 2011 at 01:50 from 70.173.150.x
Looks like read only garbage to me.


Posted by Will on Sat 01 Jan 2011 at 01:51 from 70.173.150.x
*write only

(too many NYE drinks!)


Posted by Justin on Sat 01 Jan 2011 at 10:52 from 74.66.229.x
that's what's glorious about it -- once you get into perl you can read it, too, and then it is awesome.


Posted by Anonymous guy also learning PERL on Sun 30 Jan 2011 at 18:29 from 68.204.112.x
Hey, to help readability you should add comments to your code. I know your famous and all, but I have to correct you on that. There is plenty of negative press going on about PERL. The majority of the complaints revolve around readability of PERL. Comments in the code drastically upgrades the readability of a PERL program/script. Use '#' for the love of PERL!


Posted by Justin on Fri 04 Mar 2011 at 17:21 from 74.66.229.x
I don't think this code would benefit much from comments, tbh.


Add comment:
Name:
Human?: (no or yes, patented anti crap stuff here)
Comment:
search : rss : recent comments : Copyright © 2024 Justin Frankel