Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

PERL/Windows file open error

by mgabalins (Initiate)
on Mar 04, 2019 at 15:27 UTC ( [id://1230848]=perlquestion: print w/replies, xml ) Need Help??

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

Hi, I am looking for some suggestions/ideas. I have following code:
#!/usr/bin/perl use strict; use warnings; use File::Spec; use IO::File; use autodie; my $FH; my @lines; my $fn = 'D:\DATA\Software\MFLOW2\MOD_CL\INSTALL\MOD_CL\INSTALL\RDBMS\ +c$template.pks'; #my $fn = '..\todo.txt'; $fn = File::Spec->rel2abs( $fn ) ; print "file: $fn len=" . length($fn) . "\n"; open ($FH,'<',$fn); @lines=<$FH>; close($FH);
Which gives me error "No such file or directory" on file open. File definitely exists - I can reach it and open it with file manager. Some files are opened normally, most give this error. I am really out of ideas and some help will be appreciated. Environment: Windows 10 x64, PERL: Strawberry perl v. 5.24 Thanks in advance. Sincerely Maris

Replies are listed 'Best First'.
Re: PERL/Windows file open error
by davido (Cardinal) on Mar 04, 2019 at 17:17 UTC

    On line 12 (right after my $fn = ...), add this line: warn "Filename: <<<$fn>>>\n";. And observe what comes between the <<< and >>> delimiters. I suspect that you do not have a file named c$template.pks. It may seem like the obvious solution is to change the single quotes to double quotes, but then the backslashes will get interpreted as escapes, and will evaporate. This is one of the joys of dealing with Windows paths that include backslashes. There are two solutions. First, you can just keep it as is but use concatenation:

    my $fn = join('', 'D:\DATA\Software\MFLOW2\MOD_CL\INSTALL\MOD_CL\INSTA +LL\RDBMS\c', $template, '.pks');

    Or you could use forward slashes, and let Perl sort out with Windows what the path delimiters are:

    my $fn = "D:/DATA/Software/MFLOW2/MOD_CL/INSTALL/MOD_CL/INSTALL/RDBMS/ +c$template.­pks";

    According to perldoc perlport in the DOS and Derivatives section:

    System calls accept either / or \ as the path separator.

    That means under a Windows system you can use forward slashes. This doesn't work if you're exposing the path to the command line shell, but works just fine for system calls such as open.


    Dave

Re: PERL/Windows file open error
by poj (Abbot) on Mar 04, 2019 at 15:51 UTC
    \MOD_CL\INSTALL\MOD_CL\INSTALL

    Are those repeats in the path correct ?

    poj
Re: PERL/Windows file open error
by Laurent_R (Canon) on Mar 04, 2019 at 15:46 UTC
    Hi,

    maybe try to change this line:

    open ($FH,'<',$fn);
    to something like this:
    open $FH, '<', $fn or die "Failed to open file $fh $!";
    This way, you can ascertain the name of the file you were trying to open and figure out whether there is something wrong with the content of $fh.

    Update (15:50 UTC): I see now that you're actually printing the file name right before, so what I suggested is useless. And also that you're using autodie. Never mind.

Re: PERL/Windows file open error
by thanos1983 (Parson) on Mar 04, 2019 at 16:05 UTC

    Hello mgabalins,

    Welcome to the Monastery. On the parameter my $fn = ..... c$template.pks is this a predefined parameter (that we can not see in the script) or the actual name of the file is c$template.pks?

    I mean you have a file named e.g. my $template = 'file';?

    If so you need to change the single quotes '' to "". Sample: my $fn = "D:\DATA\Software\MFLOW2\MOD_CL\INSTALL\MOD_CL\INSTALL\RDBMS\c$template.pks";

    Looking forward to your reply.

    BR / Thanos

    Seeking for Perl wisdom...on the process of learning...not there...yet!
      my $fn = "D:\DATA\Software\MFLOW2\MOD_CL\INSTALL\MOD_CL\INSTALL\RDBMS\c$template.pks";

      Note that the backslashes need to be escaped in this case.

Re: PERL/Windows file open error
by mgabalins (Initiate) on Mar 04, 2019 at 19:58 UTC
    Hi,

    First of all - thanks for all responses. It appears that paths was really wrong (thanks poj). Only thing in my defense is that it was generated automaticly by line.

    File::Spec->rel2abs( $fn ) ;

    and I did copy generated line (which failed to open) to my mini-sample without looking at it hard enough. Now I'am going to fight with File::Spec.

    Sincerely Maris
Re: PERL/Windows file open error
by BillKSmith (Monsignor) on Mar 04, 2019 at 16:12 UTC
    Is your path a true absolute path or does it use links? I do not understand links in windows. I do know that often, they do not work the way that I expect.
    Bill

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (7)
As of 2024-04-19 16:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found