--- [EMAIL PROTECTED] wrote:
> 
> I am a total beginner to perl. I wrote the program below to open a program
> file, read it line by line, and write each line to an output file after
> first putting two string lines out there. It compiles without errors but the
> output is a filename with zero length. What did I do wrong? Thank you!
> 
> 
> #!/usr/bin/perl -w
> use strict;
> 
> open (infile, "<<moo.cgi");
> open (outfile, ">>snert.cgi");
> 
> my @linesin = <infile>;
> 
> foreach (@linesin) {
> 
> print outfile "output line 1\n";
> print outfile "output line 2\n";
> print outfile "$_\n";
> 
> }
> 
> close (infile);
> close (outfile);

Richard,

For a "total beginner", your code looks pretty good.  There are, however, a few things 
we can do
to help this code.  First, you have a problem:

    open (infile, "<<moo.cgi");

I am not aware of any "<<" mode for opening files.  Since you don't test to see 
whether or not the
file open was successful, this silently fails.  Here's a quick cleanup that assumes 
you want to
open the first file as read-only and the second file as append.

    #!/usr/bin/perl -w
    use strict;
    
    open INFILE, "< moo.cgi"     or die "Cannot open moo.cgi for reading: $!";
    open OUTFILE, ">> snert.cgi" or die "Cannot open snert.cgi for appending: $!";
    
    my @linesin = <INFILE>;
    
    foreach (@linesin) {
        #print OUTFILE "output line 1\n";
        #print OUTFILE "output line 2\n";
        print OUTFILE $_;
    }
    
    close (INFILE);
    close (OUTFILE);

I commented out two lines as I wasn't sure of your intent.

Cheers,
Curtis "Ovid" Poe

=====
"Ovid" on http://www.perlmonks.org/
Someone asked me how to count to 10 in Perl:
push@A,$_ for reverse q.e...q.n.;for(@A){$_=unpack(q|c|,$_);@a=split//;
shift@a;shift@a if $a[$[]eq$[;$_=join q||,@a};print $_,$/for reverse @A

__________________________________________________
Do You Yahoo!?
Send FREE video emails in Yahoo! Mail!
http://promo.yahoo.com/videomail/

-- 
To unsubscribe, e-mail: [EMAIL PROTECTED]
For additional commands, e-mail: [EMAIL PROTECTED]

Reply via email to