Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

filename with question mark

by bdugay (Novice)
on Oct 03, 2012 at 16:55 UTC ( #997098=perlquestion: print w/ replies, xml ) Need Help??
bdugay has asked for the wisdom of the Perl Monks concerning the following question:


I am using File::Find function. I'm using Perl (v5.12.3) running on Mac OS X Lion (10.7.5). I am opening each file that is found and doing an MD5 digest. I am referencing each file using $File::Find::name. When it encounters a file with a trailing question mark, I get an error that the file is not found and the script errors out:

': No such file or directory at ./ line 166.D/Users/work/Documents/Icon

Actual file is: /Volumes/Macintosh HD/Users/work/Documents/Icon?

When I try to list it from the shell, tab complete displays it as $ ls -lh /Volumes/Macintosh\ HD/Users/work/Documents/Icon^M
-rw-r--r--@ 1 myuser  staff     0B Mar 14  2008 /Volumes/Macintosh HD/Users/work/Documents/Icon?

I cannot find anyway to successfully handle this situation. Can anyone offer some suggestions?

Thanks and regards, Brian

Comment on filename with question mark
Select or Download Code
Replies are listed 'Best First'.
Re: filename with question mark
by kcott (Canon) on Oct 03, 2012 at 17:52 UTC

    G'day Brian,

    Welcome to the monastery.

    I also have Mac OS X Lion (10.7.5); I'm running Perl v5.14.2.

    File::Find and $File::Find::name work for me:

    $ ls -l abc? -rw-r--r-- 1 ken staff 0 4 Oct 03:13 abc? $ perl -Mstrict -Mwarnings -E ' use File::Find; sub wanted { /\?$/ && say $File::Find::name } find(\&wanted, q{.}); ' ./abc?

    Filename completion also works. If I type ls -l abc<TAB><RETURN>, I get:

    $ ls -l abc\? -rw-r--r-- 1 ken staff 0 4 Oct 03:42 abc?

    Please post enough of to reproduce your problem.

    -- Ken

      Hi Ken, thanks for testing. The last character of your file appears to be different than mine. I don't think providing my code at this point is relevant. Going by Anonymous' comment, I'll investigate how to handle file with what appears to be a ^M (\r) at the end of a filename. -Brian

        I'll investigate how to handle file with what appears to be a ^M (\r) at the end of a filename. -Brian

        Well, it really shouldn't need special handling, other than making sure when you warn/die... the filename is escaped, because \r among other chars doesn't show up well in console :)( Basic debugging checklist )

        open should just open the file, so first thing to try is feeding it proper Dumpered filename -- if that works, you know what to work on next, which is

        how the filename gets to your program, if its readdir, it should work, if its @ARGV or something else, you might need to decode utf8 ... see utf8, how to unicode filenames?, how to make a filename in unicode characters

Re: filename with question mark
by Anonymous Monk on Oct 03, 2012 at 17:00 UTC

    and doing an MD5 digest.

    How, by shelling out to md5sum?

    show code? use Data::Dump 'pp' to format filenames

    Use String::ShellQuote to quote it?

    The filename appears to be "Icon\r" ( ^M means \r )

    Try  perl -le " print -s qq[....Icon\r]

      Hi Anonymous,

      I'm using Digest::MD5, but that is irrelevant because the problem occurs while trying to open file, prior to any processing. You make a good point about the last character of the file. I think that is where the problem lies. String::ShellQuote looks interesting, I'll investigate that. -Brian

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://997098]
Approved by kcott
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (3)
As of 2016-05-30 03:29 GMT
Find Nodes?
    Voting Booth?