Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

I killed my head

by rvosa (Curate)
on Jul 25, 2007 at 12:40 UTC ( #628674=CUFP: print w/ replies, xml ) Need Help??

This one should actually go under "idiotic uses for perl" or some such.

Recently, I installed LWP - and during the build it asked me if I wanted to install the GET, HEAD and POST aliases. I wasn't thinking, but I was installing system-wide on my osx laptop (never a good combination).

Shortly thereafter, I was running this great big build system that all of a sudden started failing mysteriously. Sure enough: somewhere in ./configure a call was made to head, i.e. the textutil that prints the first n lines in text files or a piped string. Because osx is case-insensitive (why, for the love of god?) and I installed LWP system-wide, /usr/bin/head (the util) was overwritten by /usr/bin/HEAD (the lwp-request alias).

What to do?

Because I didn't feel like hunting the internets for darwinports of the gnu textutils I decided to rename the lwp-request aliases HEAD, GET and POST into lwp-HEAD, lwp-GET and lwp-POST (this required some tweaking of the scripts, because they compose requests based on $0), then recreated the head util as a perl script. What it needed to do was still somewhat defined in the man page, and on another - solaris - machine I recreated the corner cases (bad options and values), so I ended up with the following (I coded it using strict and warnings and IO::File, but decided to remove all dependencies once it seemed to work):

#!/usr/bin/perl # okay, so I messed up, whatever, I'll just rewrite the head utility : +-P # default lines to read my $lines = 10; # there might be two command line arguments -n $i, # which we need to remove from @ARGV if ( @ARGV ) { # process args, will implement both -n 10 and -10 if ( $ARGV[0] =~ qr/^-(.+)$/ ) { my $flag = $1; # using traditional api if ( $flag eq 'n' ) { $lines = $ARGV[1]; # if it's not an integer above zero, print msg + and quit if ( $lines !~ qr/^\d+$/ or $lines <= 0 ) { print "head: Invalid \"-n $lines\" opt +ion\n"; print "usage: head [-n #] [-#] [filena +me...]\n"; exit 1; } # remove args from @ARGV shift(@ARGV); shift(@ARGV); } # maybe first arg is -10, not actually used by darwin, + though! elsif ( $flag =~ qr/^\d+$/ ) { $lines = $flag; # if it's not an integer above zero, print msg + and quit if ( $lines <= 0 ) { print "head: Invalid \"-n $lines\" opt +ion\n"; print "usage: head [-n #] [-#] [filena +me...]\n"; exit 1; } # remove arg from @ARGV shift(@ARGV); } # some other flag was used! else { print "head: illegal option -- $flag\n"; print "usage: head [-n #] [-#] [filename...]\n +"; exit 1; } } } # additional arguments are file names if ( @ARGV ) { # more than one file? print header my $more_than_one_file = $#ARGV; # need to add a line break before ==> XXX <== after first file my $files_read = 0; # loop over remaining args for my $arg ( @ARGV ) { open ( my $handle, '<', $arg ) or print "$arg: $!\n" a +nd exit 1; print "\n" if $files_read++; print "==> $arg <==\n" if $more_than_one_file; my $counter = 0; PRINT_FROM_FILE: while ( defined( my $line = <$handle> + ) ) { print $line; last PRINT_FROM_FILE if ++$counter == $lines; } close $handle; } # no further behaviour exit 0; } my $counter = 0; PRINT_FROM_STRING: while(<>){ print; last PRINT_FROM_STRING if ++$counter == $lines; }

Lessons learned:
  • Think before you install system-wide.
  • Exact command line arguments of head differ between darwin and solaris (latter also does "head -20 <file>").
  • Exit values are ill defined, just 0 and >0 (I did 1 for the latter).
  • Behavior on binary files is undefined.
In the end, though, the great big build system now works again.

Comment on I killed my head
Download Code
Re: I killed my head
by wazoox (Prior) on Jul 25, 2007 at 14:38 UTC

    Nice shot! I've had a couple of case-related problems with MacOS X and CPAN too :)

    # Think before you install system-wide. # Exact command line arguments of head differ between darwin and solaris (latter also does "head -20 <file>").

    I guess this is because Solaris is a System V, while OS X is a BSD.

Re: I killed my head
by jczeus (Monk) on Jul 26, 2007 at 06:41 UTC
    Until I installed 10.4 on a new Mini a couple of months ago, I wasn't even aware of the fact that HFS+ isn't case sensitive. You are now able to choose "HFS+ case sensitive", which I did. I wonder why this is not the default.
Re: I killed my head
by parv (Priest) on Jul 27, 2007 at 03:20 UTC
    Why, don't the non-system files plop in /usr/local (& system ones in /{,bin,usr} etc.)?
      If you install using an older version of LWP, it uses the standard OS X Perl, and installs where the Perl binary goes: /usr/bin.

      I did this to myself too on my first OS X box. I found it faster to reinstall OS X and go get coffee. I then built my own Perl in /usr/local after that.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: CUFP [id://628674]
Approved by polettix
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others taking refuge in the Monastery: (13)
As of 2014-12-18 13:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (51 votes), past polls