Just commenting out the second
open works for me, but there are still problems with your script.
You
use strict; which is good, but you should also
use warnings;
Giving the filename the suffix ".rtf" does not make it a rich text file, it is just a plain text file (apologies if you knew that).
When you print
"@stuff" the action of the double quotes is to place (by default) a space between each element, and this will appear in the file.
Here is a tided version:
#!/usr/bin/perl
#this program is supposed to take user input put it into an array and
+then add
#that text into a log file.
use 5.010;
use strict;
use warnings;
print "Enter some text then hit control+D and we'll try to send it to
+the perlogfile:\n";
my @stuff = <STDIN>;
open (LOG, '>>', 'perlogfile.rtf') or die "Unable to open logfile: $!"
+;
print LOG @stuff;
close (LOG);
File handleWhen a program asks the operating system to use a file (
open) then several checks have to be made. Obviously it has to find where the physical file is, but it also has to check the file permissions against (on UNIX) the process's user id and maybe group id. To do that (again, on UNIX) it has to read from disc the file's
inode. Clearly all that is a lot of work and takes time. If we had to do that every time we wanted to write a line to a file then things would be slow. So, we
open the file just once and store all that information internally in RAM, so that it can be referred to whenever we want to write to the file. The
file handle is our reference to that internal information, so we use that each time we access the file.
Downside is that this information uses up memory, so we tidy it all up when we do a
close.
Perl is not alone in using this mechanism - most programming languages do something similar. To complicate matters, there are (at least) two ways to describe a file handle. You are using the old-fashioned way with an uppercase global name - LOG. That's often good enough, and still very commonly used. A more modern way is to use a
lexical variable, which has the advantage (among others) of not being global - important in a large multi-file application
open (my $fh, '>>', 'perlogfile.rtf') or die "Unable to open logfile:
+$!";
print $fh @stuff;
close ($fh);
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.