UMD2UCSF
UMD2UCSF

Reputation: 9

Appending into file

So I'm a brand new to writing scripts and I am currently using Perl. I wrote a script and the very last thing I need it to do is append the output in a .txt file that is already there. I've been trying to get a simple script to work that appends to see if my syntax is correct and it is not working. Advice?

#Appending.pl
#!/usr/bin/perl
use strict;
use warnings;

my $appending_test;


open(my $APPENDING, ">>$appending_test");
print $APPENDING "I am appending";

Upvotes: 0

Views: 71

Answers (1)

stevieb
stevieb

Reputation: 9296

You don't specify a filename for open() to open...

#!/usr/bin/perl

use warnings;
use strict;

my $filename = 'output.txt';

open my $fh, '>>', $filename
  or die "Can't open the file $filename: $!";

print $fh "I am appending...\n";

As just a side note, the more idiomatic way to open a file is to use the three-arg open, and output a statement with the error message if the open fails as I have done in the code snip above.

Upvotes: 4

Related Questions