Solved

Perl: Need to break STDIN while loop from within an if statement within another if statement.

Posted on 2003-11-06
8
44,559 Views
Last Modified: 2011-08-18
While reading STDIN I need to be able to break out of the while loop and stop the STDIN from reading the rest of the file if a condition is met. This has to be done from an if statment imbedded in another if statement...   see below:


while(<STDIN>){

if(something){do soemthing}

if(something else){do somethingelse}

if(something here)
{
do stuff

   if(code is true)
   {
   break the while loop and stop the STDIN from continuing.
   }

do other stuff

}

I want to end up down here....    to continue the script after breaking the loop.




Any information would be much appreciated. Thanks :)

0
Comment
Question by:dr34m3rs
8 Comments
 
LVL 18

Expert Comment

by:kandura
ID: 9693082
Use 'last' keyword to do that. It will take you outside the enclosing loop.

Note that if you have another loop inside the outer loop, then last takes you outside the inner loop.
If you want to jump outside the outer loop, use a label just before the outer loop, and call 'last LABEL;' to jump out of the outside loop.

See the perlfunc manpage for a better description, but here's an example:

OUTER:                                  # a label for the outer loop
while($something) {
    # ... do stuff

     while($inner) {
         # ... do stuff
          last if $cond1;               # this will take you to COND1

          last OUTER if $cond2;    # this will take you to COND2
     }
     # COND1
     # do more stuff in outer loop
}
# COND2

HTH,
Kandura
0
 
LVL 1

Author Comment

by:dr34m3rs
ID: 9696320
The label worked kandura, however it did not break the STDIN loop until after STDIN was finished reading the incoming data...

Basically my script reads the incoming data from an uploading file, finds out what the filename is and if a checkbox named overwrite was checked. If the filename exists and the overwrite box was not checked, I need to exit the STDIN loop before the upload data is read to 100%...    I don't want to keep people waiting around for an hour for their upload to finish, only to find out it already exists. ;)

Code as it is now::

OUTER;

while(<STDIN>){

         if(this exists){dothis}

         if(this exists){dothis}

         if(this is true && this is true)
         {
         &call_file_exists_sub;
         last OUTER;                       ### break to OUTER label, doesn't break STDIN data read though...
         }

}

I have add points on this question...
0
 
LVL 2

Expert Comment

by:rootkiddy
ID: 9696664
Please describe what you are trying to accomplish by while(<STDIN>).  As you may be needing a different call as STDIN is buffered.
0
 
LVL 1

Author Comment

by:dr34m3rs
ID: 9696713
I am capturing HTML upload from an HTML form. I am able to manipulate the upsteam while it's uploading, I just am unable to break out of the upload without closing/stopping the browser itself from uploading the data...
0
Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

 
LVL 1

Author Comment

by:dr34m3rs
ID: 9696716
upstream**
0
 
LVL 18

Accepted Solution

by:
kandura earned 450 total points
ID: 9696749
Well, the loop has been exited, meaning that your script won't read more from STDIN unless you do that somewhere else.
What this doesn't do is tell the browser (or whoever is on the other end), that they should stop sending data.

Which, in general, you can't do.

You might try to close STDIN, but then you're no longer able to read from cliient request.
You might try to close STDOUT, but then you're no longer able to send data to the client.

The safest way is to call exit() , and be done with it altogether. Your script is done, and the webserver will tell the client that
no more data is needed, or going to arrive.

So try

    close STDIN;
    last OUTER;

and see how that works.

Else, print out anything you might still want to send to the client, and call exit().

Let us know...
0
 
LVL 1

Author Comment

by:dr34m3rs
ID: 9696951
Kandura,

You are 100% right. The problem isn't the script, it's the browser.

Close STDIN didn't do anything for the browser...

The browser has no idea what is going on...   and just keeps sending data. So, I am going to give you the points, because now I understand it enough to be able to come up with a solution...   I hope. :)

I am thinking something with a javascript and flash combo always checking a server side file to see if code was written to it, and if the code is true, force browser to finish...    not sure though.

Any ideas?

Thanks  for all your help kandura :)
0
 

Expert Comment

by:mkdl
ID: 12164108
last OUTER will not work,  use  goto OUTER. By the way you can use exit. That will solve the purpose simply

0

Featured Post

Top 6 Sources for Identifying Threat Actor TTPs

Understanding your enemy is essential. These six sources will help you identify the most popular threat actor tactics, techniques, and procedures (TTPs).

Join & Write a Comment

Suggested Solutions

Email validation in proper way is  very important validation required in any web pages. This code is self explainable except that Regular Expression which I used for pattern matching. I originally published as a thread on my website : http://www…
There are many situations when we need to display the data in sorted order. For example: Student details by name or by rank or by total marks etc. If you are working on data driven based projects then you will use sorting techniques very frequently.…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
It is a freely distributed piece of software for such tasks as photo retouching, image composition and image authoring. It works on many operating systems, in many languages.

707 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now