• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 373
  • Last Modified:

Loops in Perl

I have a program that pings servers every couple of seconds. For this to work correctly, I have to keep writing to the screen. To do this I am using a loop. The problem is after the first iteration, "Content-Type: text/html" get printed every time it loops. How can I stop this?

Thanks
0
Paulmul
Asked:
Paulmul
  • 12
  • 5
  • 3
  • +3
2 Solutions
 
dorwardCommented:
Put the code that generates the content type header outside (before) the loop.
0
 
PaulmulAuthor Commented:
The content type header is part of a function called PrintHead() this is called every time it loops as it contains all the html data.

All the loop does is print:
 
X out of Y iterations

X+1 out of Y itereations, etc.

Is there a way to print this and avoid reprinting, I attached the code below.

##Prints HTML
sub PrintHead {
print << "EndHead";
Content-Type: text/html

<HTML>
<head>
<TITLE>IP details</TITLE>
</head>

<BODY>
<FONT FACE="Arial" SIZE="4" COLOR ="#0000FF">


<!------- HTML HEADER ------------- -->

EndHead
}

##Part that is looping
     PrintHead();
       print "<br>$i of $Time1 iterations</br>";
     PrintTail();  
0
 
dorwardCommented:
     PrintHead();
##Part that is looping
       print "<br>$i of $Time1 iterations</br>";
## End of part that is looping
     PrintTail();  
0
Keep up with what's happening at Experts Exchange!

Sign up to receive Decoded, a new monthly digest with product updates, feature release info, continuing education opportunities, and more.

 
PaulmulAuthor Commented:
What I need to do is get it to avoid calling printhead() I  can't do this though. Is there a way I could have the screen refresh so instead of "$i of $Time1 iterations" being printed several times, the screen refreshes and it only writes to one line?
0
 
ozoCommented:
Why do you say you can't do this?
0
 
PaulmulAuthor Commented:
It is the way my code is set up, I can't avoid calling printhead()
0
 
ozoCommented:
What is preventing you from changing the way your code is set up?
Could you show us the code in question?
Could you change printhead() to only print the first time it is called?
0
 
PaulmulAuthor Commented:
The code is above, printhead() is  called in the loop, I am running things for over an hour at a time. I have to keep in printing to the screen to stop it from timing out. Everything happens in a loop including writing to the screen, this is why it keeps calling printhead(). I need to refresh the screen or something but I do not know how.
0
 
ozoCommented:
Can't you just take the Content-Type: text/html out of the loop?
0
 
PaulmulAuthor Commented:
No, it's part of a header that has to be called to print HTML if I remove the line, HTML doesn't print. I don't think this can be changed. I am going to ask for the question to be deleted.

Thanks anyway.
0
 
ozoCommented:
Why can't you just print the header before you enter the loop?
0
 
PaulmulAuthor Commented:
It is the whole setup of my code. It does a lot of things at once, if I do that it will mess up.
0
 
FishMongerCommented:
Without the willingness to change the way your code is set up, how do you expect to get the results that you want?
0
 
PaulmulAuthor Commented:
I would have to completely re-write it and this isn't an option. It is part of a project and I can't change it against what I handed in, it was only a small change that I wanted to make for a demonstration, I had hoped I may only need to change one line or something but to re-write that much it is against the rules.
0
 
bkiahgCommented:
Could you change it to

if ($i == 0)  { PrintHead() }

would that work?
0
 
ozoCommented:
#would something like
sub PrintHead {
our $i;
print "Content-Type: text/html\r\n\r\n" unless $i;
$i=1;
print << "EndHead";
<HTML>
<head>
<TITLE>IP details</TITLE>
</head>

<BODY>
<FONT FACE="Arial" SIZE="4" COLOR ="#0000FF">


<!------- HTML HEADER ------------- -->

EndHead
}
#be too big of a change?
#what kinds of changes are you willing to accept?
0
 
PaulmulAuthor Commented:
I tried if ($i == 0)  { PrintHead() } and it didn't work, it said it couldn't print to the scree (due to missing the printhead()) I was hoping someone could tell me a way around this.

Ozo, I wont be in college for another week (Easter break) I will look at doing your idea then. That could be exactly what I need. Do you know if that works or are you just guessing? As I said above I couldn't use if, will this be any different?

Thanks
0
 
healthstatusCommented:
Jumping in here, if ($i == 0)  { PrintHead() } won't work, because you never set $i to 0, you create $i with no value, then you assign $i=1;  so you can either:

our $i=0;
or
if(!$i){ PrintHead() }  but it would be more elegant to

PrintHead() unless $i;
0
 
PaulmulAuthor Commented:
I tried that way, including initialising i to 0. The error it gave me was that it couldn't find the print headers section (printhead()) do you actually know if the 'unless' method will get around this error?
0
 
healthstatusCommented:
You are being extremely vague in what and where you are changing things.  printhead() doesn't equal PrintHead() you shouldn't be calling PrintHead from within itself.  It sounds like if you will put

use strict;

up at the top of your code a lot of your problems may go away.
0
 
FishMongerCommented:
The use of the strict pragma is certianly highly recommended, but I'd say that adding it in at this point would almost certianly "break" other sections of the code.

I agree that the info that we've been given so far is too vauge for us to be able to provide the best answers.  If ozo's last suggestion doesn't work then from the info that we've been given, I'd say it can't be done without modifing other sections of the code.
0
 
PaulmulAuthor Commented:
Printhead() is a sub routine, it has all the html headers in it. The program runs for an hour at more, to stop apache from timing out, it has to print something every iteration. To do this I have to put something to print in the loop (The loop is what runs the prgram). To print I have to call the PrintHead() function as this is what makes it html. every time I call PrintHead() it also prints the "context-tpye..." I need to find away around this without changing my subroutine structure (PrintHead()) this is my dilemma.

I use strict in all my programs.
0
 
healthstatusCommented:
In your main program very near to the top and before PrintHead ever gets called do this:

use vars ($been_to_printhead);
$been_to_printhead = 0;

then in the PrintHead subroutine:

sub PrintHead {
return if $been_to_printhead;
$been_to_printhead = 1;
(everything else stays the same)
0
 
PaulmulAuthor Commented:
healthstats, I'll try that next week. Thanks.
0
 
PaulmulAuthor Commented:
I can't remember what I did in the end so I am going to split the points between healthStatus and Ozo. Sorry I didn't do this earlier, I completely forgot about the question.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

Featured Post

The 14th Annual Expert Award Winners

The results are in! Meet the top members of our 2017 Expert Awards. Congratulations to all who qualified!

  • 12
  • 5
  • 3
  • +3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now