russelj9 has asked for the wisdom of the Perl Monks concerning the following question:

Hi all. So I was writing my script and behaves wonderfully. But then I install it and let it do its thing and it gets caught up on an apostrophe. I won't explain the code because I have it below. But basically if I run this code on a file named with an apostrophe it gets hung up every time when I compute the md5 sum.

And yes I've already tried renaming the file but it's produced automatically from iWeb. I went into iWeb and got rid of all apostrophes, but it's still showing up in the filename. I'm running OS X by the way and by the end of my code the file with the apostrophe should end up on a remote server (the server is linux based).

Anyways I think it'd be nice to know how to handle this in perl... So here's the code (minimal example given)-

#!/usr/bin/env perl -w use strict; use warnings; use File::Find; use Digest::MD5::File qw(file_md5_hex); my $local_dir = $ARGV[0]; my %localData; finddepth ( \&wanted, $local_dir); sub wanted { if (-f $_) { my $fileName = $File::Find::name; unless ( $fileName =~ m{.*~\..*|\.DS_Store|\.svn|CVS} ) { $fileName =~ s{$local_dir(.+)}{\.$1}; $localData{$fileName} = file_md5_hex($File::Find::name); } } }

Replies are listed 'Best First'.
Re: Something to do with text encoding?
by pc88mxer (Vicar) on Feb 13, 2008 at 16:51 UTC

    One issue I see with your code is that you're not quotemeta-ing $local_dir in

    $fileName =~ s{$local_dir(.+)}{\.$1};

    To be safe you should quote the meta characters in $local_dir with either quotemeta or the \Q and \E escapes:

    $fileName =~ s{\Q$local_dir\E(.+)}{\.$1};

    It is possible that it could be a filename encoding issue (a quick google search suggests that MacOS uses Unicode for file paths.) Can you determine what code-point (i.e. character ordinal) the apostrophe is in your file names? It possibly could be a non-ASCII character.

      Well I got it fixed by doing this:

      $localData{$fileName} = file_md5_hex(decode('UTF-8', $File::Find::name +));

      I didn't know about the quotemeta so I'm glad to know. Though if I passed a quotemeta to file_md5_hex it still couldn't find the file.

      I still have problems when I try to put the file on the remote server but I'll make another post for that

      Good to know thanks.