APM: perl -i "failure mode"

Wayne Walker wwalker at broadq.com
Sun Mar 16 10:37:51 CST 2003

I just got bit by an interesting behavior of perl -i.  The short story
is that the -i option to perl will change softlinks into real files.
This is a proper behavior, just not what one expects.

If you understand the following command, then there's probably nothing
useful for you in the rest of the email. Those who don't understand the
following line, esdpecially sysadmins or config mgmt folks, may find the
rest of the email well worth looking at :)

find . -name Makefile | xargs -n 1 perl -i -p -e 's/...MAKE_CONTINUE../(test "\$(MAKE_CONTINUE)" -eq 1)/'

For those that don't know the perl command line switches that well, here
are the three I use in this example:

-i      "edit In place" - takes one filename at a time from the command
        line args and makes that file's contents STDIN, renames the
        file, and writes STDOUT to a new file of the same name.

-p      "awk Print" - essentially wraps your program in a print loop:
                while (<>)  # reads a line into $_
                        <your program code>  # modifies $_ as appropriate
                        print; # prints $_

-e      "execute" - run this line of perl code

I ran:

find . -name Makefile | xargs -n 1 perl -i -p -e 's/...MAKE_CONTINUE../(test "\$(MAKE_CONTINUE)" -eq 1)/'

which in turn ran:

perl -i -p -e 's/...MAKE_CONTINUE../(test "\$(MAKE_CONTINUE)" -eq 1)/' ./Makefile  # and 20 other Makefiles

which ran like:
                while (<>)  # reads a line into $_
                        s/...MAKE_CONTINUE../(test "\$(MAKE_CONTINUE)" -eq 1)/;
                        print; # prints $_

Now this did exactly what it was supposed to do.  It read and rewrote 18
Makefiles for me for a total of 54 edits.

But, I was in a "shadow tree" (a directory tree full of softlinks to the
real files).  I expected it to modify the real files, but what it did
was remove my softlinks and replace them with real files (properly


Wayne Walker

www.broadq.com :)  Bringing digital video and audio to the living room

More information about the Austin mailing list