Link to home
Start Free TrialLog in
Avatar of dr34m3rs
dr34m3rs

asked on

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

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

Avatar of kandura
kandura

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
Avatar of dr34m3rs

ASKER

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...
Please describe what you are trying to accomplish by while(<STDIN>).  As you may be needing a different call as STDIN is buffered.
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...
upstream**
ASKER CERTIFIED SOLUTION
Avatar of kandura
kandura

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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 :)
last OUTER will not work,  use  goto OUTER. By the way you can use exit. That will solve the purpose simply