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

Hi everyone. I am a newbie to Perl, but need to use it in school. My question should be fairly easy to answer for you guys, when I look at other problems that people have. I use Strawberry Perl for Windows 7, and I have tested it using perl -v - it works fine. When I attempt to execute my "program" with: perl readwrite.pl 0720-da.txt ...it tells me it can't be opened, because no such file or directory exists, quoting: Can't open perl script "readwrite.pl": No such file or directory. It is written in Notepad and is located in the correct folder alongside its txt-file. The perl file looks like this:
#!/usr/bin/perl # readwrite.pl while ($line = <STDIN>) { chomp($line); @tokens = split(" ", $line); for $tok (@tokens) { ) print "$tok "; } print "\n"; }
Can you by any chance spot what's wrong with this picture? It's a very simple program although I am so new to Perl, that it seems like an unsolveable riddle to me. Thank you.

Replies are listed 'Best First'.
Re: "Can't open perl script..." :-(
by johngg (Canon) on Feb 15, 2010 at 16:16 UTC

    A wild stab in the dark but could it be that Notepad has saved your file as readwrite.pl.txt?

    Cheers,

    JohnGG

Re: "Can't open perl script..." :-(
by Utilitarian (Vicar) on Feb 15, 2010 at 15:22 UTC
    In the same directory what happens when you
    type readwrite.pl

    print "Good ",qw(night morning afternoon evening)[(localtime)[2]/6]," fellow monks."
Re: "Can't open perl script..." :-(
by Corion (Patriarch) on Feb 15, 2010 at 16:20 UTC

    In the medium run, you want to use a text editor more suited to programming than notepad.exe. On Windows, I can recommend the Padre editor, which can even work from an USB stick. It numbers your lines, has a built-in syntax check (through Perl) and various other nice features. Also, its author, szabgab, wants it to be friendly towards newcomers to Perl, so your experience with it can help improve it.

Re: "Can't open perl script..." :-(
by cdarke (Prior) on Feb 15, 2010 at 15:36 UTC
    I also get Can't open perl script "readwrite.pl": No such file or directory. Probably because I do not have such a file. Either the perl script is not in the current directory or you mis-spelt the name.

    By the way, your script has an errant ) on the line after the for (although I doubt that is related to your problem).
Re: "Can't open perl script..." :-(
by Anonymous Monk on Feb 15, 2010 at 15:25 UTC
Re: "Can't open perl script..." :-(
by 7stud (Deacon) on Feb 15, 2010 at 16:22 UTC
    When I attempt to execute my "program"

    The devil is in the details. You need to know:

    • The full path to your program.
    • You issued your command at some prompt. You need to be aware of what directory that prompt is 'pointing to'.

    The easiest thing to do is: 'cd' to the directory that contains your program, then issue the same command again. Better yet, set the default prompt to the directory where you are going to store all your perl programs, then you won't have to do any 'cd'ing.

    Always use these two lines at the top of your programs:

    use strict; use warnings; use 5.010; #if you are using version 5.10 or higher.

    Always declare your variables with 'my', e.g.:

    my $count = 1; while (my $line = <STDIN>) { ...
      @ Anonymous Monk - The commando prompt tells me so. Again, I am new to Perl thus not familiar with the LSP. :-)
      @ JohnGG - Your stab was correct, but the problem seemed to be sorted by installing Padre (thanks Corion for that hint).
      @ Stud - I appreciate suggestions, but would you mind explaining what the addition of these specific lines do?

      Unfortunately, this was not the end of my problems. When I execute it now typing: "perl readwrite.pl 0720-da.txt" the cursor simple moves down a line blinking in all voidness. Don't even know how to return to the directory, so I have to click on command promt cross to shut it down every time. I get no error messages whatsoever, only the cursor moves to a new and empty line doing nothing. Why? :-(

      Really appreciate any help you can provide. And please ask if anything appears unclear. Thanks again.

        First of all, welcome to the Perl community!

        As to your last question,

        <STDIN>

        will read whatever you type at the command line until you hit

        Ctrl-Z <ENTER>(on Windows) or

        Ctrl-D(on *nix)

        Mentioning that you "need to use Perl in school" reminds me of my having to learn it so that I could maintain some Perl code for my job. I started reading Learning Perl and promptly fell in love with the language.

        EDIT: Corrected for use on Windows. Thanks AnomalousMonk!
Re: "Can't open perl script..." :-(
by 7stud (Deacon) on Feb 16, 2010 at 06:16 UTC
    @ Stud - I appreciate suggestions, but would you mind explaining what the addition of these specific lines do?
    They make sure that you write correct perl code. If you don't, they give you errors and warnings.
    Unfortunately, this was not the end of my problems. When I execute it now typing: "perl readwrite.pl 0720-da.txt" the cursor simple moves down a line blinking in all voidness.

    That is what your perl program tells your operating system to do.

    If you don't know what your code does, it seems to me that you picked a poor example to try and run. I suggest you back up and run an easier program, like one that prints 'hello world'. And instead of trying to learn about random things in some haphazard order, at the very least you need to read 'perlintro'. Better yet, get a beginning book on perl, so that you can learn the language in a useful order.