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

I cannot figure out why this is failing so consistently. I feel like there is some small thing I am doing that is causing it to fail to parse the path.
use warnings; use strict; use IO::File; use feature('say'); sub openFile{ my $file = shift; #open my $fh, '>' , $file or die $!; my $fh = IO::File->new(); if ($fh->open(">> $file")){ say "I opened $file"; $fh->close; } else { say "failed to open $file $! "; } } openFile('C:\Users\Shain\AppData\Roaming\WaterAccount\file.log'); openFile("C:\Users\Shain\AppData\Roaming\WaterAccount\file.log"); openFile('$ENV{APPDATA}\WaterAccount\file.log'); openFile("$ENV{APPDATA}\WaterAccount\file.log"); openFile('C:\\Users\\Shain\\AppData\\Roaming\\WaterAccount\\file.log') +; openFile("C:\\Users\\Shain\\AppData\\Roaming\\WaterAccount\\file.log") +; openFile('$ENV{APPDATA}\\WaterAccount\\file.log'); openFile("$ENV{APPDATA}\\WaterAccount\\file.log"); openFile('C:\/Users\/Shain\/AppData\/Roaming\/WaterAccount\/file.log') +; openFile("C:\/Users\/Shain\/AppData\/Roaming\/WaterAccount\/file.log") +; openFile('C:/Users/Shain/AppData/Roaming/WaterAccount/file.log'); openFile("C:/Users/Shain/AppData/Roaming/WaterAccount/file.log"); openFile('$ENV{APPDATA}/WaterAccount/file.log'); openFile("$ENV{APPDATA}/WaterAccount/file.log");

Output:

C:\Users\OWNER\Documents\Work\Perl>perl file.pl Unrecognized escape \S passed through at file.pl line 22. Unrecognized escape \A passed through at file.pl line 22. Unrecognized escape \R passed through at file.pl line 22. Unrecognized escape \W passed through at file.pl line 22. Unrecognized escape \W passed through at file.pl line 25. failed to open C:\Users\Shain\AppData\Roaming\WaterAccount\file.log No + such file or directory failed to open C:SERSSHAINAPPDATAROAMINGWATERACCOUNT♀ILE.LOG Inv +alid argument failed to open $ENV{APPDATA}\WaterAccount\file.log No such file or dir +ectory failed to open C:\Users\OWNER\AppData\RoamingWaterAccount♀ile.lo +g Invalid argume nt failed to open C:\Users\Shain\AppData\Roaming\WaterAccount\file.log No + such file or directory failed to open C:\Users\Shain\AppData\Roaming\WaterAccount\file.log No + such file or directory failed to open $ENV{APPDATA}\WaterAccount\file.log No such file or dir +ectory failed to open C:\Users\OWNER\AppData\Roaming\WaterAccount\file.log No + such file or directory failed to open C:\/Users\/Shain\/AppData\/Roaming\/WaterAccount\/file. +log No suc h file or directory failed to open C:/Users/Shain/AppData/Roaming/WaterAccount/file.log No + such file or directory failed to open C:/Users/Shain/AppData/Roaming/WaterAccount/file.log No + such file or directory failed to open C:/Users/Shain/AppData/Roaming/WaterAccount/file.log No + such file or directory failed to open $ENV{APPDATA}/WaterAccount/file.log No such file or dir +ectory failed to open C:\Users\OWNER\AppData\Roaming/WaterAccount/file.log No + such file or directory

Replies are listed 'Best First'.
Re: Unable to open file.
by ww (Archbishop) on Dec 24, 2014 at 15:06 UTC

    The 'plaints about "Unrecognized escape(s)" stem from your use of single-quoted (non-interpolated) $doze paths. If you comment out the lines with those, you'll resolve that problem.

    Is the name of the log actually supposed to be "file.log" or were you supposed to supply a real filename -- maybe something like water.log or account.log or similar?

    Since you haven't told us what's in YOUR $ENV{APPDATA}*1, I'm not inclined to even hazard a guess about the failures in attempts to use whatever those values are, other than to re-iterate para 2.

    Update/clarification:
    1 Mine, on a Win7 box, contains a long list of dirs. Does \\WaterAccount exist in yours? This code may help:

    #!/usr/bin/perl use 5.018; use warnings; my $key; foreach $key (keys(%ENV)) { printf("%-10.10s: $ENV{$key}\n", $key); }

    (Also corrected singulars to plural in para 3.)
    </UPDATE: >



    check Ln42!

Re: Unable to open file.
by Anonymous Monk on Dec 24, 2014 at 06:52 UTC

    If the error messages aren't making sense, turn on diagnostics to get more verbose error messages

    If those aren't making sense, check perlintro then explain what you're confused about

Re: Unable to open file.
by kaushik9918 (Sexton) on Dec 24, 2014 at 08:01 UTC

    Should work fine. Infact, works fine for me. Unless there is something wrong with your win file paths I guess?