i think you can chomp the 'return'

chomp ( my $result = <STDIN> )  , which is a common practice

or you can use Term::ReadKey /  Term::ReadLine  to control the read the
keyboard input more well





On 4/17/09, [email protected] <
[email protected]> wrote:
>
> Send Perl-Win32-Users mailing list submissions to
>         [email protected]
>
> To subscribe or unsubscribe via the World Wide Web, visit
>         http://listserv.ActiveState.com/mailman/listinfo/perl-win32-users
> or, via email, send a message with subject or body 'help' to
>         [email protected]
>
> You can reach the person managing the list at
>         [email protected]
>
> When replying, please edit your Subject line so it is more specific
> than "Re: Contents of Perl-Win32-Users digest..."
>
>
> Today's Topics:
>
>    1. active perl 5.8.8 build 820 inconsistent behavior with $input
>       = <STDIN>;  (Greg Aiken)
>    2. RE: active perl 5.8.8 build 820 inconsistent behavior with
>       $input    =<STDIN>;  (Brian Raven)
>
>
> ----------------------------------------------------------------------
>
> Message: 1
> Date: Thu, 16 Apr 2009 08:25:19 -0700
> From: "Greg Aiken" <[email protected]>
> Subject: active perl 5.8.8 build 820 inconsistent behavior with $input
>         =       <STDIN>;
> To: <[email protected]>
> Message-ID:
>         <[email protected]>
> Content-Type: text/plain; charset="us-ascii"
>
> I seem to be experiencing inconsistency when I invoke;
>
>
>
> $user_input = <STDIN>;
>
>
>
> in some programs the behavior is as expected.  at that point in the program
> I type in some input from keyboard then the moment I press the 'enter' key,
> the program flow returns back to the executing Perl program.
>
>
>
> other times, I get a rather unusual response.
>
>
>
> I type in user input from the keyboard and when I press the 'enter' key,
> instead of having program flow return back to the program, the enter key is
> instead OUTPUT TO THE DOS CONSOLE SCREEN and the cursor drops down one line
> on the console screen!  when this happens, the program flow does NOT return
> back to the program and my program just hangs there.
>
>
>
> any explanation for this?
>
>
>
> more importantly, whats the fix?
>
>
>
>
>
>
>
>
>
> today it just happened again.  the relevant block of code is here.
>
>
>
> sub main_loop {
>
>
>
>         while (1) {
>
>
>
>                 system("cls");
>
>
>
>                 print "Test tcp/ip client/server request/response from
> server x using port 80\n\n
>
> Press:
>
>
>
>    'H' to test HTTP request/reponse
>
>    'X' to test XML  request/reponse
>
>    'Q' to Quit
>
>
>
> Test which application protocol:\n";
>
>
>
>                 my $ui = <STDIN>;
>
>
>
>                 if      ($ui =~ /x/i) {
>
>                         my $request = &assign_xml_request_from_xml_file;
>
>                         &attempt_request_response($request);
>
>                 } elsif ($ui =~ /h/i) {
>
>                         my $request = &assign_http_request_from_http_file;
>
>                         &attempt_request_response($request);
>
>                 } elsif ($ui =~ /q/i){
>
>                         print "\nProgram Terminated\n";
>
>                         close $main::socket;
>
>                         exit;
>
>                 } else {
>
>                         next;           #loop
>
>                 }
>
>
>
>         }
>
>
>
> }
>
>
>
>
>
> the full program is here:
>
>
>
>
>
> use IO::Socket;
>
> use Strict;
>
> use Warnings;
>
>
>
> our $socket;
>
>
>
> &open_tcp_socket;
>
> &main_loop;
>
>
>
>
>
>
>
> ### sub routines
> ###########################################################
>
>
>
> sub open_tcp_socket {
>
>
>
>             $main::socket = new IO::Socket::INET ( Proto      => "tcp",
>
>                                                 PeerAddr =>
> "siteundertest.com",
>
>                                                 PeerPort => "80",
>
>                                ) or die "Error: Cannot create tcp/ip socket
> ($!)\n";
>
>             $main::socket->autoflush(1);
>
> }
>
>
>
>
>
> sub main_loop {
>
>             not duplicated in email - this is found above
>
> }
>
>
>
>
>
> sub assign_xml_request_from_xml_file {
>
>             open (IN, "xml.request");
>
>             undef $/;
>
>             my $xml_request = <IN>;
>
>             return $xml_request;
>
> }
>
>
>
>
>
> sub assign_http_request_from_http_file {
>
>             open (IN, "http.request");
>
>             undef $/;
>
>             my $http_request = <IN>;
>
>             return $http_request;
>
> }
>
>
>
>
>
> sub attempt_request_response {
>
>             my $response;
>
>             $main::socket->send($_[0]);
>
>             $main::socket->recv($response, 8192);
>
>             system("cls");
>
>             print "Response =\n\n$response";
>
>             sleep(10);
>
> }
>
>
>
> -------------- next part --------------
> An HTML attachment was scrubbed...
> URL:
> http://listserv.ActiveState.com/pipermail/perl-win32-users/attachments/20090416/56e65340/attachment-0001.html
>
> ------------------------------
>
> Message: 2
> Date: Thu, 16 Apr 2009 17:06:28 +0100
> From: "Brian Raven" <[email protected]>
> Subject: RE: active perl 5.8.8 build 820 inconsistent behavior with
>         $input  =<STDIN>;
> To: <[email protected]>
> Message-ID: <[email protected]>
> Content-Type: text/plain; charset="us-ascii"
>
> From: [email protected]
> [mailto:[email protected]] On Behalf Of
> Greg Aiken
> Sent: 16 April 2009 16:25
> To: [email protected]
> Subject: active perl 5.8.8 build 820 inconsistent behavior with $input
> =<STDIN>;
>
> > I seem to be experiencing inconsistency when I invoke;
> >
> > $user_input = <STDIN>;
> >
> > in some programs the behavior is as expected.  at that point in the
> program I type in some input from keyboard > then the moment I press the
> 'enter' key, the program flow returns back to the executing Perl
> program.
> >
> > other times, I get a rather unusual response.
> >
> > I type in user input from the keyboard and when I press the 'enter'
> key, instead of having program flow return > back to the program, the
> enter key is instead OUTPUT TO THE DOS CONSOLE SCREEN and the cursor
> drops down one
> > line on the console screen!  when this happens, the program flow does
> NOT return back to the program and my
> > program just hangs there.
> >
> > any explanation for this?
> >
> > more importantly, whats the fix?
> >
> > today it just happened again.  the relevant block of code is here...
>
> Actually, I think the relevant block of code may be further down.
>
> [Some stuff deleted]
>
> > sub assign_xml_request_from_xml_file {
> >             open (IN, "xml.request");
> >             undef $/;
>
> You have just globally changed the input record separator to slurp mode,
> for all input. This may be the cause of your problem. This is one of the
> few instances where 'local' is needed, i.e. to temporarily change the
> value of a special variable. Make that:
>
>     local $/;
>
> >             my $xml_request = <IN>;
> >             return $xml_request;
> > }
> >
> > sub assign_http_request_from_http_file {
> >             open (IN, "http.request");
> >             undef $/;
> >             my $http_request = <IN>;
> >             return $http_request;
> > }
>
> The above two functions look identical apart from the filename, so why
> not use a single function. For example, here is one I wrote earlier:
>
> sub read_file {
>     my $fn = shift;
>     open my $fd, "<", $fn or die "Failed to open $fn: $!\n";
>     local $/;
>     my $data = <$fd>;
>     close $fd;
>     return $data;
> }
>
> Note the 3 argument form of open, and the localised file handle. Or
> course, if you can install File::Slurp, you don't even need to write
> one.
>
> Also, its better to call subs without the '&' prefix, unless you know
> what that does, and you need it.
>
> HTH
>
> --
> Brian Raven
>
>
> This e-mail may contain confidential and/or privileged information. If you
> are not the intended recipient or have received this e-mail in error, please
> advise the sender immediately by reply e-mail and delete this message and
> any attachments without retaining a copy.
>
> Any unauthorised copying, disclosure or distribution of the material in
> this e-mail is strictly forbidden.
>
>
>
> ------------------------------
>
> _______________________________________________
> Perl-Win32-Users mailing list
> [email protected]
> To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs
>
>
> End of Perl-Win32-Users Digest, Vol 33, Issue 4
> ***********************************************
>



-- 
            Yours Sincerely
                    Zeng Hong
_______________________________________________
Perl-Win32-Users mailing list
[email protected]
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs

Reply via email to