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

Greeting all, This is my first post as I'm new to Perl. Was wondering why I'm receiving a "No such file or directory". Thank you.

$file = "New Text Document.txt"; open (FILE, $file) or die "$!\n"; $epoch_timestamp = (stat(FILE))[9]; $timestamp = localtime($epoch_timestamp); $size = (stat(FILE))[7]; print "$file\t$size bytes\tModified: $timestamp\n";

Replies are listed 'Best First'.
Re: No such file or directory
by Corion (Patriarch) on Sep 30, 2010 at 21:35 UTC
    $file = "New Text Document.txt"; open (FILE, $file) or die "$!\n";

    I would venture the wild guess that there is no file New Text Document.txt in the directory from where you run your Perl program.

    Personally, I would use the following idiom, which produces a somewhat nicer error message:

    $file = "New Text Document.txt"; open my $fh, $file or die "Couldn't open '$file': $!\n";
Re: No such file or directory
by toolic (Bishop) on Sep 30, 2010 at 21:35 UTC
    It looks like you are trying to open a file named New Text Document.txt which you expect to be in the current directory from which you ran your code. Apparently, there is no file with that exact name. Check the spelling, check for spaces, check if you are really in the correct directory.

    See also Writeup Formatting Tips for how to use the code tags.

Re: No such file or directory
by rowdog (Curate) on Sep 30, 2010 at 22:44 UTC

    I'm not sure about other OSes but Linux requires you to escape spaces in the file name.

    my $file = "New\ Text\ Document.txt";

    Update: I stand corrected, I'm not sure what I was doing when I reproduced the error but I was wrong in my conclusion. Thanks to Anon for catching my error.

      Nope, neither linux nor perl require escaping of spaces, and escaping spaces like that in perl doesn't actually accomplish anything
      print "New Text Document.txt\n"; print "New\ Text\ Document.txt\n"; __END__ New Text Document.txt New Text Document.txt
      Now shells on the other hand require that you quote quote your filenames....