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

Hello, all:

I run perl, say at C:\, and try open file in other directory:

use strict; use warnings; $in_file="e:/perl/practice/test.txt"; $out_file="e:/perl/practice/testout.txt"; open (OUT, ">$out_file") or die "cannot open $out_file\n"; open (IN, "<$in_file") or die "Can't open file\n";
it did not seem open, what is wrong? Thanks!

Replies are listed 'Best First'.
Re: open file that is not in default directory
by Anonyrnous Monk (Hermit) on Jan 15, 2011 at 18:34 UTC
    it did not seem open

    How do you tell?  What exactly happened?  Did it die with "Can't open..."?  If so, print the system error message $! (and possibly also $^E) — it'll likely give you a clue what's wrong.

    open (IN, "<", $in_file) or die "Can't open file '$in_file': $!";

      How do you tell? What exactly happened? Did it die with "Can't open..."?

      It did not die, but I also did not hear the pop sound, so either it didn't open or it had been open before.

Re: open file that is not in default directory
by Jim (Curate) on Jan 16, 2011 at 00:05 UTC

    It didn't seem to open, or it didn't open? :-) What actually happened exactly?

    One obvious problem is that you enforce discipline upon yourself and your Perl program, and then you are undisciplined. When you use the use strict pragma, you must then declare your lexical variables with my or else your program won't run.

    Here's how I would write the same program in Modern Perl:

    #!perl use strict; use warnings; use autodie qw( open close ); my $in_file = 'E:/Perl/Practice/TestIn.txt'; my $out_file = 'E:/Perl/Practice/TestOut.txt'; open my $in_fh, '<:encoding(UTF-8)', $in_file; open my $out_fh, '>:encoding(UTF-8)', $out_file; # Do groovy stuff with the text... close $in_fh; close $out_fh; exit 0;

    This assumes your input text file is in the UTF-8 character encoding form of the Unicode coded character set. If it's not, then replace the encoding with whatever the correct encoding is (e.g., Windows-1252).

    By the way, it doesn't matter where you run your Perl script from. The only files specified in your program are absolute paths, not relative paths.

      Thanks, Jim. You are right.

Re: open file that is not in default directory
by Khen1950fx (Canon) on Jan 16, 2011 at 03:29 UTC
    When something seems like it's not open, then use tell. In this sample script, I used die; so, if you think that something's wrong, try this:
    #!/usr/bin/perl use strict; use warnings; my $file = '/DEPENDENCIES.NEW'; open FILE, '>', $file or die "Couldn't open FILE: $!\n"; if ( tell(FILE) != -1) { print "FILE is open\n"; } else { print "FILE is not open\n"; }

      That seems rather pointless. What will 'tell' do for you in this case that "open ... or die" doesn't? If Perl is able to assign that filehandle, it succeeds; if it's not, then it fails. The only thing I can even imagine making a difference is if the file is deleted between the 'open' and the 'tell' calls.

      -- 
      Education is not the filling of a pail, but the lighting of a fire.
       -- W. B. Yeats