Beefy Boxes and Bandwidth Generously Provided by pair Networks
Problems? Is your data what you think it is?
 
PerlMonks  

problem opening and reading from a file

by Anonymous Monk
on Jul 02, 2003 at 20:59 UTC ( #270951=perlquestion: print w/replies, xml ) Need Help??

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

ok Im lost as to why I can not open a file...
ok first the version im runnning
$ perl -v
This is perl, version 5.005_03 built for sun4-solaris
I dont have root or privleages so i cant change this :(

#!/usr/bin/perl -w open(OUT,"> /export/home/foo/bar/test.csv") or die "Cannot open output + file; print OUT "foo,bar,totally foobar\n"

This works just fine I tested by #'ing out all lines but those 2

When I add this line it doesnt work and I do not understand why?
open(READ,"/export/home/foo/bar/foo.log") or die "Cannot open foo.log\ +n"; while ($line = <READ>) {

probably have my head up my ..... but I just cant figure it out. I get the following error
syntax error at test.pl line 4, near "open"
Execution of test.pl aborted due to compilation errors

ohh wise monks can you help?

Title edit by tye

Replies are listed 'Best First'.
Re: What the @#%* lost again!
by Abigail-II (Bishop) on Jul 02, 2003 at 21:15 UTC
    There's no semi-colon after the print statement on line 2.

    Abigail

Re: What the @#%* lost again!
by dws (Chancellor) on Jul 02, 2003 at 21:14 UTC
    probably have my head up my ..... but I just cant figure it out.

    It would help if you could copy/paste the exact snippet that's giving you the error. Don't just retype it, because that risks introduced typos, as in your first snippet (which misses a trailing quote).

Re: What the @#%* lost again!
by samtregar (Abbot) on Jul 02, 2003 at 21:17 UTC
    You didn't say what happens when it "doesn't work" but I'm guess you mean that the script die()s. If that's the case you can find out what went wrong by including the special $! variable in your die() message. For example:

        open(OUT,">/foo.csv") or die "Cannot open output file: $!";

    Then when you run the script you'll see a message indicating what exactly is wrong. For example, if I run the above code without permission to open '/foo.csv' I'll see:

        Cannot open output file: Permission denied

    -sam

      I dunno, I thought the inclusion of this error was a pretty good indication of what doesn't work:

      syntax error at test.pl line 4, near "open" Execution of test.pl aborted due to compilation errors

      Or maybe I'm just reading too much into it. ;)

        Huh, right you are. I somehow managed to read right past that.

        -sam

Re: What the @#%* lost again!
by Cody Pendant (Prior) on Jul 03, 2003 at 00:24 UTC
    Unless I'm crazy, it's the fact that the quotes aren't closed in this:
    open(OUT,"> /export/home/foo/bar/test.csv") or die "Cannot open output + file;

    That's causing the problem.

    What does "use Diagnostics" tell you?



    “Every bit of code is either naturally related to the problem at hand, or else it's an accidental side effect of the fact that you happened to solve the problem using a digital computer.”
    M-J D
Re: What the @#%* lost again!
by Nkuvu (Priest) on Jul 02, 2003 at 21:13 UTC
    Well I can't tell since you didn't post all of your code, but shouldn't print OUT "foo,bar,totally foobar\n" have a semicolon? This could just be a cut-n-paste error, though.
Re: What the @#%* lost again!
by Anonymous Monk on Jul 02, 2003 at 23:48 UTC
    I think that you may you need to put a semi-colon at the end of your code:
    print OUT "foo,bar,totally foobar\n"
    ie like this:
    print OUT "foo,bar,totally foobar\n";
    John
Re: What the @#%* lost again!
by svsingh (Priest) on Jul 02, 2003 at 21:02 UTC
    This is just a guess, but I think read is a reserved word. Does the script work if you change READ to soemthing else, like READDATA?

    Update: Added code markers.

      READ isn't a reserved word in Perl, but read is. But it's fine to use filehandles with the same name as (some) all-caps words with a special meaning, as shown below:
      #!/usr/bin/perl use strict; use warnings; open BEGIN, "/etc/passwd" or die; print while <BEGIN>; __END__

      The program compiles and runs without problems.

      Abigail

        Thanks for letting me know that Abigail. I didn't realize you could use reserved words in filehandles. I guess it's just more of a recommendation that you don't use them. Thanks again.
      sure doesnt .. :( i dont get it? There is no crying in perl or I would!
Re: What the @#%* lost again!
by logan (Curate) on Jul 02, 2003 at 22:42 UTC
    Try it without the parentheses around the open command. It will also help if you check the value of $! on die(), as it holds the error that caused the script to die. This worked fine for me:
    #!/usr/bin/perl -w open READ,"test.csv" or die "Cannot open test.csv\n: $!"; while ($line = <READ>) { print STDOUT "line = $line"; }

    -Logan
    "What do I want? I'm an American. I want more."

Re: What the @#%* lost again!
by Anonymous Monk on Jul 03, 2003 at 00:32 UTC
    Thanks! To all of you! as you can guess from the response it was a case of not seeing the forest for the trees! It was a missing semicolon Abigail,John,Nkuvu and crew THANKS SO MUCH Ill try and remove my head from my ... I just cant see very well in that position. This is the best place for quick and correct answers thanks again oh wise monks!
Re: problem opening and reading from a file
by D-Volta (Initiate) on Jul 03, 2003 at 19:48 UTC
    did you forget a ; at the end of:

    print OUT "foo,bar,totally foobar\n"

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://270951]
Approved by allolex
Front-paged by hsmyers
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (7)
As of 2020-07-02 12:25 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?