Re: Perl-Win32-Users Digest, Vol 33, Issue 4

2009-04-16 Thread Michael Zeng
t; not duplicated in email - this is found above
>
> }
>
>
>
>
>
> sub assign_xml_request_from_xml_file {
>
> open (IN, "xml.request");
>
> undef $/;
>
> my $xml_request = ;
>
> return $xml_request;
>
> }
>
>
>
>
>
> sub assign_http_request_from_http_file {
>
> open (IN, "http.request");
>
> undef $/;
>
> my $http_request = ;
>
> 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" 
> Subject: RE: active perl 5.8.8 build 820 inconsistent behavior with
> $input  =;
> To: 
> Message-ID: 
> Content-Type: text/plain; charset="us-ascii"
>
> From: perl-win32-users-boun...@listserv.activestate.com
> [mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of
> Greg Aiken
> Sent: 16 April 2009 16:25
> To: perl-win32-users@listserv.activestate.com
> Subject: active perl 5.8.8 build 820 inconsistent behavior with $input
> =;
>
> > I seem to be experiencing inconsistency when I invoke;
> >
> > $user_input = ;
> >
> > 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 = ;
> > return $xml_request;
> > }
> >
> > sub assign_http_request_from_http_file {
> > open (IN, "http.request");
> > undef $/;
> > my $http_request = ;
> > 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
> Perl-Win32-Users@listserv.ActiveState.com
> 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
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


RE: active perl 5.8.8 build 820 inconsistent behavior with $input =;

2009-04-16 Thread Brian Raven
From: perl-win32-users-boun...@listserv.activestate.com
[mailto:perl-win32-users-boun...@listserv.activestate.com] On Behalf Of
Greg Aiken
Sent: 16 April 2009 16:25
To: perl-win32-users@listserv.activestate.com
Subject: active perl 5.8.8 build 820 inconsistent behavior with $input
=; 

> I seem to be experiencing inconsistency when I invoke;
> 
> $user_input = ;
> 
> 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 = ;
> return $xml_request;
> }
> 
> sub assign_http_request_from_http_file {
> open (IN, "http.request");
> undef $/;
> my $http_request = ;
> 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
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs


active perl 5.8.8 build 820 inconsistent behavior with $input = ;

2009-04-16 Thread Greg Aiken
I seem to be experiencing inconsistency when I invoke;

 

$user_input = ;

 

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 = ;

 

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 = ;

return $xml_request;

}

 

 

sub assign_http_request_from_http_file {

open (IN, "http.request");

undef $/;

my $http_request = ;

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);

}

 

___
Perl-Win32-Users mailing list
Perl-Win32-Users@listserv.ActiveState.com
To unsubscribe: http://listserv.ActiveState.com/mailman/mysubs