develooper Front page | perl.beginners | Postings from January 2002

Re: can't write to a file in Linux, can in Unix....?

Thread Previous
Tanton Gibbs
January 17, 2002 09:40
Re: can't write to a file in Linux, can in Unix....?
Message ID:
You probably want


instead of

so that will work as expected.

Also, you should always check the return value of open:
open( DATAFILE, $datafile) or die "Could not open datafile: $!\n";  $! will
have the system error that was given.

One of the opens may have failed.

For style issues, you should always run with warnings on and strict on.

#!/usr/bin/perl -w
use strict;

Then you will have to declare your variables with my.

my $datafile = "..."
my $resultfile = "..."

This will save you hours of headache in scripts to come :)
----- Original Message -----
From: "Brian Volk" <>
To: <>
Sent: Thursday, January 17, 2002 12:37 PM
Subject: can't write to a file in Linux, can in Unix....?


I'm a rookie to perl as well as Linux and I'm stumped.  Hopefully you can
help me out.  I've been reading a book on perl and in the book there are
sample programs that I'm testing out.

Here's the problem... I'm running RedHat 6.2, Perl 5.6 and I'm using the
bash shell.  First off, I can not run a perl program by typing
.../, instead I have to type; perl which is O.K., but I
don't like it... :-) I checked my .bash_profile and I have /usr/bin: in my
$PATH... ???   Here's the bigger problem... When I run
(program below) it will not pull the data from test.txt and write it to
outfile... It is not creating the outfile either... Now I know the program
works because I have logged into my shell account (came w/ hosting account)
and it works just fine.  My first thought was permissions, but the program
doesn't give any error messages...   Any thoughts would be greatly




$datafile = "/files/perl/test.txt";
$resultfile = ">/files/perl/outfile.txt";  # Note the '>' charater.
open(DATAFILE, $datafile);
open(OUTPUTFILE, $resultfile);

while (<DATAFILE>) {
         print OUTPUTFILE;
        print OUTPUTFILE "\n";

Thread Previous Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at | Group listing | About