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

Noob Question

by nick1984 (Novice)
on Oct 28, 2012 at 17:34 UTC ( #1001283=perlquestion: print w/replies, xml ) Need Help??
nick1984 has asked for the wisdom of the Perl Monks concerning the following question:

Hi guys,

I have literally started learning Perl a few days ago (using the Learning Perl O'reilly 4th edition book) and have hit a stumbling block!

I am currently attempting to get the below code to read the text from a text file and print! However I get the following error message: can't open test1.txt No such file or directory.

I have created all my scripts under: C:\Scripts\ and have also placed the test1.txt file there too!
------------------ #! /usr/bin/perl -w use strict; @ARGV = ("test1.txt"); while (<>) { chomp; print "It was $_ that I saw in some stooge-like file!\n"; } ------------------------


Replies are listed 'Best First'.
Re: Noob Question
by Corion (Pope) on Oct 28, 2012 at 18:05 UTC

    The "current directory" is not what you think it is. Perl will go looking for test1.txt in the current directory, which is not necessarily the directory the current Perl script lives in.

    Most likely you are running your script like:

    C:\>perl -w C:\Scripts\

    ... which means that the "current directory" is C:\ and Perl goes looking for C:\test1.txt, and does not find it there.

    Change either the current directory or move the file to a different place.

Re: Noob Question
by bms (Monk) on Oct 28, 2012 at 20:10 UTC

    Hello there! Well, there are a lot of ways you could go about this. I'll list three very easy to implement ones, each with a different approach

    One, simply cd into the script directory and run your script from there.

    cd C:\\Scripts

    Two, use File::Basename so you can build an absolute path to your test1.txt file using dirname:

    #!/usr/bin/env perl use File::Basename; use strict; use warnings; @ARGV = (dirname(__FILE__)."/test1.txt"); while(<>) { chomp; print "It was $_ that in some stooge like file!\n"; }

    The above code works because your test1.txt file is located in the same directory as your script and therefore, you can use dirname with the __FILE__ variable(or $0) and simply tack on the filename to get a path to your text file.

    Three, you could use an absolute path:

    #!/usr/bin/env perl use strict; use warnings; @ARGV = ("C:/Scripts/test1.txt"); while (<>) { chomp; print "It was $_ that I saw in some stooge-like file!\n"; }

    As you can see, there is more than one way to do it. There are still more ways than I have listed, but they should be sufficient :).

      Thank you very much all very good ways of resolving my issue!
Re: Noob Question
by davido (Archbishop) on Oct 29, 2012 at 01:16 UTC

    If learning Perl is your goal, you're definitely on the right track, and I just wanted to congratulate you for putting in the effort in this undertaking. The book you're working your way through is a very good resource. Your syntax is valid, and others have explained the point of failure and how to overcome it. Stick with it, and welcome to the Monastery!


Re: Noob Question
by aitap (Curate) on Oct 28, 2012 at 20:46 UTC
    In addition to previous answers, you can use Cwd to get the current working directory.
    Sorry if my advice was wrong.
Re: Noob Question
by Laurent_R (Canon) on Oct 28, 2012 at 22:51 UTC

    You obviously did not really understand the Perl syntax to open and read a file.

    Try something like this:

    #! /usr/bin/perl use strict; use warnings; open (my $fh, "<", "test.txt") or die "Could not open test file $! \n" +; while (<$fh>) { # blah blah }

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1001283]
Approved by tobyink
LanX ♪..♫ Fiesta, fiesta mexicana ... ♪..♫
[mandarin]: LanX aren't you a bit late to the party?

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (7)
As of 2018-06-18 17:43 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (110 votes). Check out past polls.