http://qs321.pair.com?node_id=755475

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

Brethren, Excuse the cryptic title, but it does pretty well describe my problem. I've lately switched boxes and decided to get around to fixing some annoying 5.8x warnings that my PGN modules produce only to run into the following:
Couldn't open 'C:/Perl/site/lib/Chess/PGN/db/ECO': No such file or dir +ectory Compilation failed in require at C:\Cygwin\home\hsmyers\pgn\toEPD.pl l +ine 7. BEGIN failed--compilation aborted at C:\Cygwin\home\hsmyers\pgn\toEPD. +pl line 7.
Now having just put the file in question into the location in question clearly I was not pleased with this error! Skipping lightly over the amount of wasted time involved I determined that XP doesn't let you use forward slashes interchangeably with backward slashes as in all previous situations.

So it comes to this; am I crazy and have missed the real problem? Or am I right and this is the problem. In any event, does anyone know of a work-around?

--hsm

"Never try to teach a pig to sing...it wastes your time and it annoys the pig."

Replies are listed 'Best First'.
Re: XP and slashes?
by moritz (Cardinal) on Apr 04, 2009 at 20:38 UTC
    If you open a file from within perl, you can use the forward slashes, even on window. See the section "Files and Filesystems" in perlport.

    So your problem must be something else.

      Yes, the problem is something else... basically it is how to find a file relative to a .pm file when the .pm file loads and not relative to the file using the .pm file. The XP stuff was a red herring. Work ensues on the other.

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."

        Yes, the problem is something else... basically it is how to find a file relative to a .pm file

        From within the module,

        use Cwd qw( realpath ); use File::Basename qw( dirname ); use File::Spec::Functions qw( catdir ); my $module_dir_qfn = dirname(realpath(__FILE__)); my $db_dir_qfn = catdir($module_dir_qfn, 'db');

        Update: Updated to use OP's dir name.

Re: XP and slashes?
by ikegami (Patriarch) on Apr 04, 2009 at 22:54 UTC

    I determined that XP doesn't let you use forward slashes interchangeably with backward slashes

    You are mistaken. Not only Perl but the actual OS supports "/" as a separator.

    C:\Temp>echo foo >foo.txt C:\Temp>perl -e"open my $fh, '<', $ARGV[0] or die $!; print <$fh>;" c: +\temp\foo.txt foo C:\Temp>perl -e"open my $fh, '<', $ARGV[0] or die $!; print <$fh>;" c: +/temp/foo.txt foo

    I'm on WinXP. I'm not using cygwin, however. And I don't know how cygwin sees the file system, but I quick lookaround indicates /cygwin/c/Perl/site/lib/Chess/PGN/db/ECO should work.

      See answer earlier. As for being mistaken here is an example:
      C:\>cd c:/perl The system cannot find the path specified. C:\>cd c:\perl
      And while I would certainly never claim to not be mistaken early and often, I'm not about that. This only applies to the command line as near as I can tell so not relevant to my problem...

      --hsm

      "Never try to teach a pig to sing...it wastes your time and it annoys the pig."

        This only applies to the command line as near as I can tell so not relevant to my problem...

        Indeed. Your example only shows that "cd" doesn't accept "/" as a separator. You'll find the same for most of MS's command-line tools because they use "/" to mark options. However, if it's quoted...

        C:\>cd "c:/temp" C:\Temp>

        Update: Actually, it works unquoted for me too.

        C:\>cd c:/temp C:\Temp>

        Even if the slash is the first character

        C:\>cd /temp C:\Temp>
        The underlying Win32 API accepts either / or \, and has done since at lease NT 3.1, however some applications only take \. cmd.exe is one of them, of which cd is a built-in. MS-DOS and PC-DOS used to be the same (although I doubt there can be many people using the 16-bit DOS VMs any more).
Re: XP and slashes?
by morgon (Priest) on Apr 04, 2009 at 22:15 UTC
    I don't work a lot on XP but afaik both forward and backward slashes work in paths - I ususally use "/" myself and never had a problem so far.

    I would therefore think your problem is probably something else.

    But if you can show that there are cases where "/" won't work on Windows, please try to isolate the problem and post a demonstration here.

    And while I am at (this is not addressing your problem but maybe someone else may find it useful):

    1) It's probably best to use File::Spec to build path-expressions in a portable way.

    2) If you want to build a path-expression for windows using "\" as separator keep in mind that you cannot simply do "$dir\$file" (which is not syntactically wrong but probably won't give you the path you want).

      The only time I've had issues with slashes in file paths is when using Win32::OLE.