Do you have a question? Post it now! No Registration Necessary. Now with pictures!
- Posted on
- select and filehandle
June 5, 2008, 7:08 pm
rate this thread
2 print "goes to FH1.\n";
4 $TempHandle = select(FH2);
5 print "goes to FH2.\n";
7 select ($TempHandle);
8 print "goes to FH1.\n"
This is in Perl for Dummies, 4th ed, p182.
My questions are:
It seems if I need to verify this, would need to add the following in
the pl file?
Line 5 would still uses FH1 and goes into print1.txt, as FH2 has not
been selected; and line eight will uses $TempHandle or FH2 and goes
into print2.txt, as FH2 is is now selected through $TempHandle
However, my understanding contradicts to the suggestions in the book,
such as line 5 would goes in print2.txt, and line 8 would goes in
print1.txt. My verification also says the author is correct.
What's wrong with me?
Re: select and filehandle
Yes, but you should get into the habbit of writing open statements
like this instead:
open my $FH1, '>', 'print1.txt' or die "Could not open print1.txt:
I don't understand why you're saying that. Line 4 pretty clearly
$TempHandle = select(FH2);
That makes FH2 the selected filehandle, and stores whatever *was* the
selected filehandle (which in this case was FH1) in $TempHandle.
I think you have a misunderstanding of the return value of select().
select() takes one argument, the filehandle you wish to select. It
returns whatever filehandle was selected immediately prior to this
Line 1 makes FH1 the selected filehandle. The program ignores the
return value of select() in this case.
Line 4 makes FH2 the selected filehandle, and stores FH1 in
$TempHandle (because FH1 was selected prior to this line)
Line 7 makes $TempHandle (which is FH1) the selected filehandle again,
and again ignores the return value of select().
Re: select and filehandle
Yes, and you should check those opens for failure.
Hunh? The selection of FH2 is in line 4, so why hasn't it been executed?
Unless you are refering to line 5 of some hypothetical program which has
its lines renumbered because of the insertion of the open statements, in
which case you would have to show us that program.
$TempHandle holds FH1, not FH2. single-argument select selects the new
file handle (FH2 on line 4) and returns the old, previously selected, file
handle (FH1 on line 4, into $TempHandle)
I think you understand the select backwards.
-------------------- http://NewsReader.Com/ --------------------
The costs of publication of this article were defrayed in part by the
payment of page charges. This article must therefore be hereby marked
advertisement in accordance with 18 U.S.C. Section 1734 solely to indicate
- » FAQ 4.67 Why does passing a subroutine an undefined element in a hash create it?
- — Previous thread in » PERL Discussions