In
Corion's most excellent answer, there is an extra line of code, with an incorrect comment.
The
local *FILE isn't actually required. The following snippet works with no errors. Perl has special handling for all uppercase names, which it perceives to be a typeglob. File handles, such as 'FH' and 'FILE' in the two examples, are actually typeglobs. If you change the 'FH' to 'fh' below, it will fail with bareword construct warnings.
That being said, since typeglobs are global to the program, using 'local' will localize it to the subroutine (presuming that you're in one, of course), and protect any ones used globally.
The best way to do this, for other than quick'n'dirty stuff, is to use
IO::Handle. Using a handle created with IO::Handle, you can pass the handle around as a scalar, not have to worry about namespace collision, etc.
If you have
Advanced Perl Programming, chapter 3, talks about 'Perl Variables, Symbol Table, and Scoping', and section 4 talks about file handles explicitly.
#!/usr/local/bin/perl -w
use strict;
open (FH, ">myworld") || die $!;
binmode FH;
print FH "Hello, world\n";
close FH;
--Chris
e-mail jcwren
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.