Click here to get back home

select and filehandle

 HomeNewsGroups | Search | About
 comp.lang.perl.misc    Post an article   get this group's latest topics as an RSS feed add this group's latest topics to your My MSN content add this group's latest topics to your My Yahoo content
Subject Author Date
select and filehandle April 06-05-2008
Posted by April on June 5, 2008, 3:08 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');

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
assignment?

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?


Posted by Paul Lalli on June 5, 2008, 3:35 pm
Please log in for more thread options
> I found the following demo in a book regarding usage of "select" ...
>
> 1 =A0select(FH1);
> 2 =A0print "goes to FH1.\n";
> 3
> 4 =A0$TempHandle =3D select(FH2);
> 5 =A0print "goes to FH2.\n";
> 6
> 7 =A0select ($TempHandle);
> 8 =A0print "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, 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:
$!";

> Line 5 would still uses FH1 and goes into print1.txt, as FH2 has not
> been selected;

I don't understand why you're saying that. Line 4 pretty clearly
says:
$TempHandle =3D select(FH2);

That makes FH2 the selected filehandle, and stores whatever *was* the
selected filehandle (which in this case was FH1) in $TempHandle.

> and line eight will uses $TempHandle or FH2 and goes
> into print2.txt, as FH2 is is now selected through $TempHandle
> assignment?
>
> What's wrong with me?

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.

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().

Paul Lalli

Posted by April on June 5, 2008, 4:27 pm
Please log in for more thread options
and another mistake is that I thought line 4 only feeding the
variable, but actually it had done two things, selected the argument
value, and fed the variable with the prior value.


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.

Posted by April on June 5, 2008, 4:22 pm
Please log in for more thread options
Thanks Paul and Xho, now I know my mistake is that I thought select
would take and return the argument value ... thanks again!


Similar ThreadsPosted
Re: select and filehandle June 5, 2008, 6:10 pm
IO::Select::select() says no readable data even if there are August 24, 2006, 2:53 am
The select() (IO::Select) function has a limit? June 2, 2008, 5:24 am
Select October 10, 2006, 4:40 pm
Help with select December 18, 2006, 8:13 am
IO::Select extension January 22, 2005, 1:51 am
How to select subroutine June 20, 2006, 6:32 pm
Using DBI, Set Select to Variable August 27, 2007, 12:35 am
select number September 10, 2007, 11:49 am
www::mechanize $mech->select February 4, 2005, 5:28 pm

Our other projects:

Art Dolls, Fairies and Mermaids - Sunnyfaces.net

Roy's Linux, Programming and Search Engines messages

1-Script XML SitemapXML Sitemap