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

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

Thread Previous | Thread Next
From:
John W. Krahn
Date:
January 17, 2002 11:41
Subject:
Re: can't write to a file in Linux, can in Unix....?
Message ID:
3C4728D5.943881B7@acm.org
Brian Volk wrote:
> 
> Hello,

Hello,

Please change your line length to 80 characters or less

> 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.

Which book?

> 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 ./program.pl, instead I have to
> type; perl program.pl. 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 write_test.pl (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 appreciated.
> 
> 
>  #!/bin/perl

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


> $datafile = "/files/perl/test.txt";
> $resultfile = ">/files/perl/outfile.txt";  # Note the '>' charater.

Do you really have a directory in your root file system named "files"? 
You shouldn't be running as root.  You should run test programs as a
normal user in your home directory.  You shouldn't include file modes in
the file name.

my $datafile   = '/home/brian/files/perl/test.txt';
my $resultfile = '/home/brian/files/perl/outfile.txt';


> open(DATAFILE, $datafile);
> open(OUTPUTFILE, $resultfile);

You should always test that open succeeded

open DATAFILE, "< $datafile" or die "Cannot open $datafile: $!";
open OUTPUTFILE, "> $resultfile" or die "Cannot open $resultfile: $!;


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



John
-- 
use Perl;
program
fulfillment

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About