Yeah, you can either use a positive integer as a file handle, or you can use a text label or you may use a variable name with a dollar sign in front of it, but if you do that, then you have to declare the variable my $INPUT_FILE_HANDLE; before you can use it. Notice, I like to write the < > signs and the file name as one string. That way my Perl script will be backward compatible with older DOS Perl. (I currently use TinyPerl 5.8, but it's neat when I can run the same script in DOS as well.) Also, don't forget to close your opened file handles before the program ends. ;)
#!/usr/bin/perl -w
use strict;
use warnings;
open(INPUT_FILE_HANDLE, '< c:/boot.ini') or die('Cant open file for re
+ading');
open(OUTPUT_FILE_HANDLE, '> c:/outfile.txt') or die('Cant open file fo
+r writing');
print <INPUT_FILE_HANDLE>;
print OUTPUT_FILE_HANDLE "Hello world!!!\n";
close OUTPUT_FILE_HANDLE;
close INPUT_FILE_HANDLE;
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.