in reply to directories and charsets

A couple of possible traps:

1) readdir returns only the filename whereas -f needs (in general) the path appended to the front. Example:

sub Traverse { my $dir = shift; opendir my $dh, $dir or return 0; for my $file ( grep !/^\.\.?$/, readdir $dh ) { my $path = "$dir/$file"; if ( -d $path ) { Traverse( $path, @_ ); } elsif ( -f $path ) { ProcessFile( $path, @_ ); } else { warn "$path not -d or -f\n"; } } closedir $dh; }
2) carriage control will be different but there is a dos program called unix2dos as well as a unix program called dos2unix that translates accordingly. For example:
sub ProcessFile { my ( $path, $windows ) = @_; my $pid = 0; if ( $windows ) { open \*FH, "<$path" or die "$!: $path"; } else { $pid = open \*FH, "dos2unix $path |" or die "$!: dos2unix $path |"; } my $fh = \*FH; # file content (<$fh>) is now unix/dos transparent close $fh; $pid and waitpid $pid,0; }

-M

Free your mind

Replies are listed 'Best First'.
Re^2: directories and charsets
by soliplaya (Beadle) on Mar 15, 2007 at 15:34 UTC
    Thank you for the Traverse sub.

    Frustratingly this sub(), when traversing my file tree, works fine, while my own humongous program doesn't and chokes on any filename that contains "non-ascii" characters, and I cannot as yet find what I am doing different.

    Except that I am doing quite a bit of concatenation of strings and storage in intermediate variables, and I suppose that somewhere perl's smart handling of automatic internal string conversion to utf8-when-needed is biting me.

    My real program scans several file trees, remembers in each the oldest file (storing the path in a table), then sorts the table on some formula, and passes the "best" path to another portion of the program for processing. It is in that other part of the program, when trying to use the path to open the file, that the problem occurs.
    What I imagine is that somewhere along the line, what was initially read as a "bytes" entry, becomes an internal "utf8" string, and then open() or stat() do not recognise that filename anymore. Does that make sense ?

      The Traverse sub does plenty of assigning of the filenames too - readdir has to work on these first, then its loaded into $file then that forms part of $path, so it can't be that!

      I strongly suggest using perl -d on your program - you can then use the x, W, and b and r commands to track down what is really happening to this data.

      Apart from that, I would say that Traverse works because it's too simple not to :)

      -M

      Free your mind