It’s the details, silly Perl error

Just today I was troubleshooting what seemed to be a spurious error in the logs of a script I wrote. The script itself isn’t all that important to this discussion but what I found in troubleshooting irked me to no end and I felt like I had to write about it.

In my script I have two places where I run a “find” command via the system() call to do very specific work. In this case doing recursive permissions and file mode changes on a directory.

Take a look at the offending line?

#
# stuff before this

my $cmd = "find /mydir -type f -exec chmod 644 {} \;";

#
# Stuff after

Looks innocent doesn’t it?   What you have there is an interpolation bomb for a lack of better term for it.  The error I was seeing was “find: missing argument to `-exec'” which after a bit of thought I realized the command the shell was actually seeing was “find mydir -type f -exec chmod 0644 {} ; ” which would be missing the backslash as find would expect to be there. Whoops.

The reason for this would be everything inside the double quotes gets interpolated and the interpolation in play here was an unnecessary escape of the semicolon.  Modifying my code with a single quote thusly:

my $cmd = 'find /mydir -type f -exec chmod 0644 {} \;';

 

You know… really I should be using File::Find instead… but sometimes laziness gets the better of me…

 

About: peter


2 thoughts on “It’s the details, silly Perl error”

  1. My developer is trying to convince me to move to .net from PHP.

    I have always disliked thе idea becɑᥙse of the expenses.
    Βut he’s tryiong none the lesѕ. I’ve been using Movable-type on various websites for about a
    year and am anxious about switching to another platform.
    I have heard excellent things about blogengine.net. Is there a way I сan impߋrt all my wordpress posts into it?

    Ꭺny help would be greatly appreciated!

Leave a Reply

Your email address will not be published. Required fields are marked *