Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Perl stricter than my OS regarding file/dir names

by jffry (Hermit)
on Feb 15, 2006 at 20:04 UTC ( [id://530497]=perlquestion: print w/replies, xml ) Need Help??

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

In my OS, any character ASCII > 0 and ASCII < 128 are valid characters to use in a file or directory name. However, Perl's -d operator seems to be balking at this. It's mkdir command handles it well enough, though.

I get this error:

Unsuccessful stat on filename containing newline at MyModule.pm line 2 +10.
Here is all I'm doing:
next if -d "$dir/$current_filename";
I don't see any advice in Q&A files or Q&A directories. Perldoc -X doesn't specify what might be the problem, and my searches aren't turning up anything, either.

Is there some way to tell Perl's -d function, "It's OK. Nevermind the newline, already! My OS, he likes it, all right?"

Replies are listed 'Best First'.
Re: Perl stricter than my OS regarding file/dir names
by PodMaster (Abbot) on Feb 15, 2006 at 22:41 UTC
    'perldoc perldiag'
    Unsuccessful %s on filename containing newline

    (W newline) A file operation was attempted on a filename, and that operation failed, PROBABLY because the filename contained a newline, PROBABLY because you forgot to chomp() it off. See chomp in the perlfunc manpage.

    If you grep the perl source, you'll find these tests
    ######## # doio.c [Perl_my_stat Perl_my_lstat] use warnings 'io' ; stat "ab\ncd"; lstat "ab\ncd"; no warnings 'io' ; stat "ab\ncd"; lstat "ab\ncd"; EXPECT Unsuccessful stat on filename containing newline at - line 3. Unsuccessful stat on filename containing newline at - line 4.
    ######## # pp_sys.c [pp_stat] use warnings 'newline' ; stat "abc\ndef"; no warnings 'newline' ; stat "abc\ndef"; EXPECT Unsuccessful stat on filename containing newline at - line 3.
    So to disable that particular warning, through warnings, you simply write
    no warnings 'newline'; next if -d "$dir/$current_filename";
    perllexwarn lists newline among others (though it doesn't really explain what warnings it blocks).

    MJD says "you can't just make shit up and expect the computer to know what you mean, retardo!"
    I run a Win32 PPM repository for perl 5.6.x and 5.8.x -- I take requests (README).
    ** The third rule of perl club is a statement of fact: pod is sexy.

Re: Perl stricter than my OS regarding file/dir names
by skx (Parson) on Feb 15, 2006 at 21:22 UTC

    Strange most operating systems disallow one of "/" and "\"...

    Steve
    --
      root@XXXX:/tmp/testme # touch \\ root@XXXX:/tmp/testme # ls -rlt total 0 -rw-r--r-- 1 root system 0 Feb 15 15:39 \

      But...

      You're right about / ! Don't use the directory separator! How embarrassing! Actually, it ,is legal, but far too problematic. I'll have to skip that one for now.
Re: Perl stricter than my OS regarding file/dir names
by davidrw (Prior) on Feb 15, 2006 at 20:06 UTC
    what OS? and why would you want a newline (or any other low ascii char) in a directory name?
      If I'm writing a test of the OS's file system, I should use all documented legal characters to create directories & files as part of that test.

      Due to NDA, I don't feel comfortable saying which UNIX flavor I'm dealing with.

Re: Perl stricter than my OS regarding file/dir names
by dsb (Chaplain) on Feb 15, 2006 at 20:11 UTC
    Are you initializing $dir or $current_filename with values from STDIN? If so, chomp'ing them might help.


    dsb
    This @ISA my( $cool ) %SIG
      No, I am creating $dir and $current_filename using random ASCII values > 0 and < 128. I need to keep the newlines and other funky characters as-is. I think I'm only having trouble with newlines at this point.

        You aren't telling us if the directory exists or not. Perl only gives you a helpfull warning, just in case you are trying to stat a file/directory with a newline in it - which would be a common programming error.

        If you want to avoid the warning, just use

        { no warnings 'newline'; next if -d "$dir/$current_filename"; };

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://530497]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-20 11:32 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found