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

LVL 1
dr34m3rsAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

kanduraCommented:
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
dr34m3rsAuthor Commented:
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
rootkiddyCommented:
Please describe what you are trying to accomplish by while(<STDIN>).  As you may be needing a different call as STDIN is buffered.
0
CompTIA Security+

Learn the essential functions of CompTIA Security+, which establishes the core knowledge required of any cybersecurity role and leads professionals into intermediate-level cybersecurity jobs.

dr34m3rsAuthor Commented:
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
dr34m3rsAuthor Commented:
upstream**
0
kanduraCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
dr34m3rsAuthor Commented:
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
mkdlCommented:
last OUTER will not work,  use  goto OUTER. By the way you can use exit. That will solve the purpose simply

0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Perl

From novice to tech pro — start learning today.