CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Beginner:
endless loop

 



esolve
New User

Oct 24, 2012, 10:37 AM

Post #1 of 5 (981 views)
endless loop Can't Post

I want to extract the content except http header from tcp flow files
the content is like the following
the http header ends when two ^M are met

Code
HTTP/1.1 200 OK^M 
Last-Modified: Sat, 20 Mar 2010 09:43:12 GMT^M
Content-Type: video/x-flv^M
Date: Wed, 24 Oct 2012 14:34:13 GMT^M
Expires: Wed, 24 Oct 2012 14:34:13 GMT^M
Cache-Control: private, max-age=22124^M
Accept-Ranges: bytes^M
Content-Length: 29833281^M
Connection: close^M
X-Content-Type-Options: nosniff^M
Server: gvs 1.0^M
^M
FLV^A^E^@^@^@ ^@^@^@^@^R^@^CK^@^@^@^@^@^@^@^B^@
onMetaData^H^@^@^@^O^@^Hduration^@@i<97>
=p£×^@ starttime^@^@^@^@^@^@^@^@^@^@^Mtotalduration^@@i<97>


my code for extraction is as follows,
and I run : extract.pl < tcp.flow
but it seems the loop is endless,
what is wrong with the codes? thanks!

Code
#!/usr/bin/perl 
$start=0;
$data="";
while(<STDIN>)
{
if ( $start eq 0 && $_ =~ /^\r\n/) { $start = 1; }
elsif ( $start eq 1 ) { $data = $data . $_; }
}
open(FH, ">sample.flv");
print FH $data;
close(FH);



(This post was edited by esolve on Oct 24, 2012, 10:38 AM)


Laurent_R
Veteran / Moderator

Oct 24, 2012, 2:14 PM

Post #2 of 5 (970 views)
Re: [esolve] endless loop [In reply to] Can't Post

 

Code
$start eq 0


This should probably be:


Code
$start == 0


Otherwise, from what you describe, why detecting a ^M at the beginning of the line should not be sufficient?

Something like this might do the job:


Code
while (<STDIN>) { 
next unless /^\r\n/;
print FH $_;
last;
}
print FH $_ while (<STDIN>);
close ...



rovf
Veteran

Oct 25, 2012, 5:00 AM

Post #3 of 5 (961 views)
Re: [Laurent_R] endless loop [In reply to] Can't Post

While doing a numeric equality test is certainly more logical, a textual comparision would do as well in this case, so this can't be the problem.

Assuming that the posted program is complete, the only way it could go into a endless loop would be if STDIN is feeded endlessly (via a pipe, for example). This is unlikly of course. Therefore I don't think we have a looping problem here.

Maybe the input redirection is not set up correctly, and the program is just blocking at <STDIN>.

It would be interesting to know what got printed on FH ...


Laurent_R
Veteran / Moderator

Oct 25, 2012, 3:27 PM

Post #4 of 5 (953 views)
Re: [rovf] endless loop [In reply to] Can't Post

I certainly agree with Rovf that a program like this one should not go into an endless loop, at least not if it is given a (presumably non infinite) file as input.

But the use of <STDIN> leads me to a question: how do you launch your program (i.e. can you give the exact command line that you use to run your program?).

In other words, I suspect that the program could simply be hanging because it is waiting for user input, if given no file as a parameter to work with.


FishMonger
Veteran / Moderator

Oct 25, 2012, 4:35 PM

Post #5 of 5 (949 views)
Re: [Laurent_R] endless loop [In reply to] Can't Post

FYI, the OP cross posted this Q on stackoverflow and like here, hasn't responded to anyone's response to this question.

http://stackoverflow.com/questions/13057014/endless-loop-in-perl


(This post was edited by FishMonger on Oct 25, 2012, 4:35 PM)

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives