Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

Style Sheet Doesn't Display Styles

by JaredHess (Acolyte)
on Jul 19, 2006 at 19:48 UTC ( #562403=perlquestion: print w/ replies, xml ) Need Help??
JaredHess has asked for the wisdom of the Perl Monks concerning the following question:

I'm having a really strange problem. In my script I can't get my style sheet to display styles reliably. I tried searching the net and this site for information on this, but couldn't find anything. Here's a very simple test script I've been using to debug:
#!/usr/local/bin/perl print "Content-type: text/html\n\n"; my $body = "1234567890"; #my $body = "12345678901234567890"; my $length = length($body); print<<bob; <html> <head> <!-- Style Sheet Here --> <link href="training_site.css" rel="stylesheet" type="text/css"> <title></title> </head> <body> <p class="bodytext">Body variable *$body* has $length chars</p> </body> </html> bob
In the above script, if I have...
my $body = "1234567890";
... it displays the style as expected. But if I give $body a longer value like...
my $body = "12345678901234567890";
... then it doesn't display my styles. How can I fix this? Does anyone have any bright ideas? I'm perpetually new to Perl, so go easy on me. :)

Comment on Style Sheet Doesn't Display Styles
Select or Download Code
Re: Style Sheet Doesn't Display Styles
by ikegami (Pope) on Jul 19, 2006 at 20:13 UTC

    The bug is not in what you showed us. The problem is elsewhere. Did you place training_site.css in the same directory as the script? Could it be some caching issue?

      ikegami, thanks for the reply.

      Yes, the script and the .css are in the same directory. The reason I'm thinking it's something to do with Perl or my code is because I created a straight .htm file that pointed to the .css file using the same generated HTML code and it works fine. It displays styles without problems regardless of the length of what's being displayed.

      The .css file is getting called, because, as I stated in my original post, it does sometimes work.

Re: Style Sheet Doesn't Display Styles
by jdtoronto (Prior) on Jul 19, 2006 at 20:14 UTC
    It's all about visibility, or in Perl terms - scope. The page as you generate it expects to see the stylesheet in the same directory as the script, I think if you put a / in front you will force it to look at the root of the webserver ( ie domain.com/stylesheet.css ).

    Your not the first person to get tripped up by this, it happened to me some years ago now when we first started using CSS.

    jdtoronto

      Likely as your explanation is, that doesn't explain why the stylesheet get picked up if the $body variable is short and not when it is long.

      CountZero

      "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

      jdtoronto, thanks for replying.

      I'm not sure if that's it, because it is finding the sheet sometimes.

      In any case, I tried doing it with the initial slash (also tried an absolute pathway pointing to the .css), but neither improved performance.

      I thought it might be a local webserver thing, so I uploaded it and the style sheet to our production server and get the same result.

        One thing I noticed is that you don't close your <link> tag. Closing link tags is NOT optional in HTML. just re-read the specs, and in fact you're not allowed to use a closing link tag. please ignore

        Another possibility is that your server isn't sending out the correct content-type header for the css.

        In any case it's more likely an HTML/browser/server problem than a perl question :-)

Re: Style Sheet Doesn't Display Styles
by JaredHess (Acolyte) on Jul 19, 2006 at 21:00 UTC

    Ok... Another tidbit of information that may shed some light on this.

    I changed my script to read as follows:

    #!/usr/local/bin/perl print "Content-type: text/html\n\n"; my $body = "123"; my $length = length($body); my $body2 = "abc"; my $length2 = length($body2); print<<bob; <html> <head> <!-- Style Sheet Here --> <link href="./training_site.css" rel="stylesheet" type="text/css"> <title></title> </head> <body> <p class="bodytext">Body variable *$body* has $length chars</p> <p class="bodytext">Body variable *$body2* has $length2 chars</p> </body> </html> bob
    Note that both $body and $body2 are short strings of three chars each. It seems that adding the $body2 into the printed html code makes it so the styles don't display as well. *scratches head* Perhaps the problem is something else and not just long strings?

      Can we see your style sheet, please? If I put .bodytext { color: #F00 } in training_site.css, I see both paragraphs in red.
        Hmm. I wonder if it is a style sheet problem. I didn't even think of that. If so, why would it function in the normal .htm file but not in my Perl script though? Anwyay, here's the style sheet:
        a { color: #000099; } a:hover{ color:#33FF33; background-color:white; text-decoration:underline; } .smallred { font-family: Arial, Helvetica, sans-serif; font-size: 10px; color: #CC0000; } .smallblack { font-family: Arial, Helvetica, sans-serif; font-size: 10px; color:#000000; } .smallblackheading { font-family: Arial, Helvetica, sans-serif; font-size: 12px; color:#000000; font-weight: bold; } .smallredheading { font-family: Arial, Helvetica, sans-serif; font-size: 12px; color:#CC0000; font-weight: bold; } .h1shade { font-family: Arial, Helvetica, sans-serif; font-size: 36px; text-transform: lowercase; color: #000066; background-color: #CCCCCC; } .h2shade { font-family: Arial, Helvetica, sans-serif; font-size: 24px; text-transform: lowercase; color: #330066; background-color: #CCCCCC; } .h3shade { font-family: Arial, Helvetica, sans-serif; font-size: 18px; text-transform: lowercase; color:#990000; background-color: #CCCCCC; } .h1 { font-family: Arial, Helvetica, sans-serif; font-size: 36px; text-transform: lowercase; color: #000066; } .h2 { font-family: Arial, Helvetica, sans-serif; font-size: 24px; text-transform: lowercase; color: #330066; } .h3 { font-family: Arial, Helvetica, sans-serif; font-size: 18px; text-transform: lowercase; color:#990000; } .bar { font-family: Geneva, Arial, Helvetica, sans-serif; font-size: 24px; color: #000033; text-transform: lowercase; background-color: #FFCC00; border-style: solid; border-top-width: 3px; border-left-width: 0px; border-right-width: 0px; border-bottom-width: 0px; border-color: #333333; } .note { border-style: solid; border-top-width: 10px; border-color:#660033; } .bodytext { font-family: Arial, Helvetica, sans-serif; font-size: 10px; color:#000033 font-weight: normal; border:solid thin yellow; }
      From one of your follow up posts, it sounds like you're getting the desired output (HTML) regardless of the content of the $body variable. If that's the case, this is probably not a perl issue, but a Browser/Server problem as Joost mentions

        Hi duckyd, thanks for sharing your wisdom.

        You may be correct...Except that I created a .htm page and ran it on the same server in the same folder location as the Perl script, pointing to the same css file, and it functions properly. If it is a Browser/Server problem, shouldn't that fail as well?

        Since it only fails using Perl, I suspect something perlish...

        ...but you may be right. How could I tell for sure?

Re: Style Sheet Doesn't Display Styles
by Skeeve (Vicar) on Jul 19, 2006 at 22:13 UTC
    Did you:
    Try a Shift-Reload in your Browser?
    Empty the Broeser's Cache?
    Try to load the Stylesheet directly in you browser?
    View the source of the page received?

    I'm asking this because stylesheets and HTML are (amongst others) my daily business and these are the steps that usually give me the expected result or an insight where the problem lies.

    s$$([},&%#}/&/]+}%&{})*;#$&&s&&$^X.($'^"%]=\&(|?*{%
    +.+=%;.#_}\&"^"-+%*).}%:##%}={~=~:.")&e&&s""`$''`"e

      Hi Skeeve,

      1. Shift-Reload? Never heard of that. What does that do? I tried it but couldn't see it do anything.
      2. Empty the Browser's Cache? Is this the same as deleting temporary internet files? I tried that, yes.
      3. Load the style sheet directly into my browser? Do you mean doing something like this in the address bar? http://127.0.0.1:81/perl/secure/training_site.css I tried that; it only opens the style sheet inside of an editor.
      4. View the source page received? Yes, and the HTML code looks fine. In fact copying and pasting the resulting HTM code as it's own HTM file does correctly display the styles.

    Thanks for responding.

        Despite the fact it didn't help, just a short explanation:
        1. Shift-Reload (should) tell the browser not to use the cache.
        2. No. Because there might be an In-Memory cache too. But I see: You're using IE!? I have no experience with that. Sorry.
        3. Exactly. This is to verify you receive the correct CSS.
        4. If this happens when you open the pasted code, then nothing can be wrong - to my understanding - with your code. It must be a caching problem because the only difference is the source where you get your page from.
        Try to sniff on the traffic that's going on between your server and your browser and see what they are "talking" about.
        But at least try to access your Server from another computer that never visited those URLs...
Re: Style Sheet Doesn't Display Styles
by JaredHess (Acolyte) on Jul 19, 2006 at 23:15 UTC

    Joost,

    Regarding your reply and suggestions here...

    ...I installed livehttpheaders in firefox and checked the headers for both my test perl script and my HTM file using the exact same source code as the perl script:

    Perl Header (for the script that doesn't correctly diplay styles)

    http://127.0.0.1:81/perl/secure/test.pl GET /perl/secure/test.pl HTTP/1.1 Host: 127.0.0.1:81 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4 +) Gecko/20060508 Firefox/1.5.0.4 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9 +,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Cache-Control: max-age=0 HTTP/1.x 200 OK Connection: close Content-Type: text/html ---------------------------------------------------------- http://127.0.0.1:81/perl/secure/training_site.css GET /perl/secure/training_site.css HTTP/1.1 Host: 127.0.0.1:81 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4 +) Gecko/20060508 Firefox/1.5.0.4 Accept: text/css,*/*;q=0.1 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://127.0.0.1:81/perl/secure/test.pl Cache-Control: max-age=0 HTTP/1.x 200 OK ----------------------------------------------------------

    HTM Header (For the HTM that correctly displays styles)

    http://127.0.0.1:81/perl/secure/test.htm GET /perl/secure/test.htm HTTP/1.1 Host: 127.0.0.1:81 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4 +) Gecko/20060508 Firefox/1.5.0.4 Accept: text/xml,application/xml,application/xhtml+xml,text/html;q=0.9 +,text/plain;q=0.8,image/png,*/*;q=0.5 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive HTTP/1.x 200 OK Transfer-Encoding: chunked Server: OptiPerl/Commercial Content-Type: text/html Connection: close --------------: --- ---------------------------------------------------------- http://127.0.0.1:81/perl/secure/training_site.css GET /perl/secure/training_site.css HTTP/1.1 Host: 127.0.0.1:81 User-Agent: Mozilla/5.0 (Windows; U; Windows NT 5.1; en-US; rv:1.8.0.4 +) Gecko/20060508 Firefox/1.5.0.4 Accept: text/css,*/*;q=0.1 Accept-Language: en-us,en;q=0.5 Accept-Encoding: gzip,deflate Accept-Charset: ISO-8859-1,utf-8;q=0.7,*;q=0.7 Keep-Alive: 300 Connection: keep-alive Referer: http://127.0.0.1:81/perl/secure/test.htm HTTP/1.x 200 OK Server: OptiPerl/Commercial Content-Type: text/css Connection: close Content-Length: 1950 ----------------------------------------------------------

    I've tried both IE and Firefox with indentical results.

    I compared the two headers using "Beyond Compare 2" and found that this portion exists in the header for the HTM file that displays correctly, but are missing from the actual perl script file's header:

    Server: OptiPerl/Commercial Content-Type: text/css Connection: close Content-Length: 1950
    I guess with these items missing the css file doesn't get called correctly. The question is how do I fix this? Is this a server configuration thing? Thanks for all the help, everyone. I'm seeking further enlightenment.
      Yes, I guess, it's an SSP (server side problem) which looks like a bug to me, but it can also be intended behaviour and configurable. I don't know the OptiPerl/Commercial server, you should contact the vendor if you don't find the solution in the server's docs.

      The different behaviour with long/short $body variable content seems to be a red herring though - your script runs fine on my apache. Length doesn't matter ;-)

      --shmem

      _($_=" "x(1<<5)."?\n".q/)Oo.  G\        /
                                    /\_/(q    /
      ----------------------------  \__(m.====.(_("always off the crowd"))."
      ");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

        Hi shmem, thanks for responding. I think I'm leaning more and more to the view that this is definately a server problem/bug.

        For some unknown reason (don't you love it?), the problem unexpectedly went away just as unexpectedly as it manifested itself.

        Perhaps it cleared up when I restarted the local server that comes with OptiPerl. If it was something that simple... *smacks forhead*

        Perhaps it was something different. I'm still not sure.

        I'm going to restart my computer and see if the problem is still there or if it has gone away--hopefully for good.

Re: Style Sheet Doesn't Display Styles
by JaredHess (Acolyte) on Jul 20, 2006 at 17:16 UTC

    After the system restart things seem to still be working correctly. *breathes a sigh of relief*

    *raises hands in praise* To all ye glorious Perl monks who did shine with the brilliance of the sun, and did ever aid to dispell the gloom upon my crestfallen soul, I praise ye your names! May the spirits of the ancients and their mighty wisdom continue to dwell in your hearts!

    *Bows deeply with steepled hands to chin and leaves the great hall with the soft rustling of robes*

      Perhaps it cleared up when I restarted the local server that comes with OptiPerl
      You may wish to check and see whether the "local" HTTPD defaults to caching Perl scripts in memory.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://562403]
Approved by ikegami
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2014-12-28 15:23 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (182 votes), past polls