|
Posted by xhoster on June 5, 2008, 3:38 pm
Please log in for more thread options > I found the following demo in a book regarding usage of "select" ...
>
> 1 select(FH1);
> 2 print "goes to FH1.\n";
> 3
> 4 $TempHandle = select(FH2);
> 5 print "goes to FH2.\n";
> 6
> 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?
>
> open(FH1, '>print1.txt');
> open(FH2, '>print2.txt');
Yes, and you should check those opens for failure.
>
> Line 5 would still uses FH1 and goes into print1.txt, as FH2 has not
> been selected;
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.
> and line eight will uses $TempHandle or FH2 and goes
> into print2.txt, as FH2 is is now selected through $TempHandle
> assignment?
$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)
> 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?
I think you understand the select backwards.
Xho
--
-------------------- 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
this fact.
|