Solved

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

Posted on 2003-11-06
8
44,571 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
Best Practices: Disaster Recovery Testing

Besides backup, any IT division should have a disaster recovery plan. You will find a few tips below relating to the development of such a plan and to what issues one should pay special attention in the course of backup planning.

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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Search in text file in column and compare 4 47
PHP equivalent of opening a com object 5 84
Edit Odbc.ini using perl module 2 83
Regex rule to match two different url 5 72
Many time we need to work with multiple files all together. If its windows system then we can use some GUI based editor to accomplish our task. But what if you are on putty or have only CLI(Command Line Interface) as an option to  edit your files. I…
A year or so back I was asked to have a play with MongoDB; within half an hour I had downloaded (http://www.mongodb.org/downloads),  installed and started the daemon, and had a console window open. After an hour or two of playing at the command …
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…
Nobody understands Phishing better than an anti-spam company. That’s why we are providing Phishing Awareness Training to our customers. According to a report by Verizon, only 3% of targeted users report malicious emails to management. With compan…

773 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