Beefy Boxes and Bandwidth Generously Provided by pair Networks
Pathologically Eclectic Rubbish Lister
 
PerlMonks  

comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
I think I'd have to pour over this code awhile to figure out exactly what it is that's going wrong. I did notice one thing though.
open (FILE, "$file") || die "Can't open $file"; $tmp=<FILE>; foreach(split(/\t/,$tmp)){ push(@hashkeys, $_); } while (<FILE>) { $tmp=$_;

The problem here is that you've slurpped the file into $tmp. So it can't read it again in your while loop unless you add the like:

seek(FILE,0,0);

That will start reading again at the beginning of FILE. That should allow you to loop through the contents of the file using while.

That said I think you are making this problem much harder than it should be. I do have a couple of questions though: You said that you were trying to change the colors of the page based on the amount of time that had passed. Time since what? Since the page was last accessed? Since it was updated?

Either way it sounds like you could benefit from HTML::Template (availiable from CPAN). Here's a short example that would change the background color based on the time of day:

#!/usr/bin/perl use strict; # Should always use this use HTML::Template; # Loads the HTML::Template module my $template = HTML::Template->new(filename=>"htmlbackground.tmpl"); my $hour = (localtime)[3]; my $bgcolor; if($hour < 12) { $bgcolor = "#00ff00"; # Green for the morning } elsif($hour == 12) { $bgcolor = "#0000ff"; # blue for noon } else { $bgcolor = "#ff0000"; # red for afternoon } # Set the template's variables to the varibles in the script $template->param(bgcolor=>$bgcolor); #Print the template out print "Content-type:text/html\n\n"; print $template->output(); exit;

Then in a separate HTML file called htmlbackground.tmpl:

<html><body bgcolor="<tmpl_var name=bgcolor>"> Hey I can change the background!</body></html>

When you call the script it substitutes <tmpl_var name=bgcolor> for the background color variable based on the time of day. If you would prefer to save it as a static file simply open a new file and print the template's contents to it rather than to STDOUT (which in this case is the webserver).

Hope that helps


In reply to Oh my, code overload by cfreak
in thread Problem with while loop by Hopeless

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2022-12-09 11:45 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?