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

HTML coded for in PERL

by MonkPaul (Friar)
on May 18, 2005 at 16:48 UTC ( [id://458288]=perlquestion: print w/replies, xml ) Need Help??

MonkPaul has asked for the wisdom of the Perl Monks concerning the following question:

Hello.

I am writing a bit HTMl in perl but when i come to format the table so it doesnt go off the end of the screen IT DOESN'T WORK. The problem lies with the sequence or fragments, labelled below as @cut and $annotate. Its too long and goes off the edge of the screen.

Does any body see a reason why.

I have tried using <TABLE WIDTH = 100%>", "<TABLE WIDTH = "100%">", "< +TD WIDTH = "100%">", "<WBR>", and a few others but none of them work. sub show_Enzyme_Output() { my ( $counter, $recSite, $element,$anotate,@fragments) = @_; print("<TABLE BORDER=2 WIDTH=100%>"); print("<TR><TD WIDTH=100%><B>Enzyme number $counter</B></TR></TD>" +); print("<TR><TD WIDTH=100%>"); print("The cleavage site is <B>$recSite</B> for enzyme $element de +noted by \" ^ \"<BR></TD></TR>"); print("<TR><TD WIDTH=100%>The annotated sequence is given below al +ong with the digested sequence<BR>"); print("<B>Annotated:</B>$anotate<BR>"); print("<B>Digested:</B>@cut</TD></TR>"); print("</TABLE><BR><BR>"); }
Thanks.

Replies are listed 'Best First'.
Re: HTML coded for in PERL
by jeffa (Bishop) on May 18, 2005 at 17:05 UTC

    Indeed, this is not really a Perl problem, but please do yourself a favor and do not use multiple print's when you only need one. And use the qq operator so you don't have to escape the quotes you should be using:

    print qq{ <table border="2" width="100%"> <tr> <td width="100%"><b>Enzyme number $counter</b></td> </tr> <tr> <td width="100%"> The cleavage site is <b>$recSite</b> for enzyme $element denoted by ' ^ ' </td> </tr> <tr> <td width="100%"> The annotated sequence is given below along with the digested sequence <ul> <li><b>Annotated:</b> $anotate</li> <li><b>Digested:</b> @cut</li> </ul> </td> </tr> </table> };

    Also consider using HTML::Template.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
Re: HTML coded for in PERL
by ww (Archbishop) on May 18, 2005 at 17:07 UTC
    I don't see (though wiser heads may) any show-stoppers in your perl, but the html has some problems (though the whole issue of how a table is rendered tends to be browser-dependant).

    Do use:
        <table border="2" width="100%" ....>
    ... eg, quote all attribute-values and no spaces around the equal-sign, may help

    Line 8 has closures of <tr> and <td> reversed:
        </TR></TD>
    which won't do you any favors either.

    and <br><br> won't work as you expect, either...<br>, \n, \t, etc ALL are -- in html's mind -- whitespace, and browsers are supposed to (w3c) render ONLY one space, horizontal or vertical, when they read a (non-char_entity) whitespace. One workaround: Use char_entity whitespace &nbsp; to separate 'em.

    Misc non-showstopper notes: use lc for all your html tags, attributes, etc. Not required in html 4.01, but.... will make shifting to XML, etc. easier. And if you're not fully comfortable with writing compliant html, maybe write the .html separately, submit it to the w3c validator, and when it's clear, use it as a here_doc.

    Update: Took so long to clean this up that it's virtually redundant. ++, Monks above!
Re: HTML coded for in PERL
by davidrw (Prior) on May 18, 2005 at 16:59 UTC
    This line has the </TR> and </TD> flipped, which could be making your browser unhappy:
    print("<TR><TD WIDTH=100%><B>Enzyme number $counter</B></TR></TD>");
    Besides that, what are sample values for the variables ($counter, $recSite, $anotate, @cut)?

      Good catch. Using a here doc (<<EOT) rather than multiple print statements can make it easer to see what you're actually going to output rather than adding extra visual noise. Also consider using a templating module (Template or the like) and separate the presentation and markup from the code.

      Update: Or qq{} as jeffa mentions below is also a good way to do it. And also not a Perl problem, but you really should have quotes around the attribute values (width="100%")</XHTML-Pedant>

      Update Update: And to be a real stickler you should be using CSS rather than width attributes and tables . . .

      Yes Sorry.

      The $counter is as it sounds a counter, whcih is taken from an external sub foreach loop.
      The $recSite is a string variable showing the recognition site for an enzyme cleavage location i.e G^ATATGAG.
      The $anotate is another string that shows the binding site of an enzyme in a DNA sequence i.e AGTATACGTTCAACGCA
      The @cut is an array of fragments cut by the enzyme at the $recSite position, highlighted by the $anotate sequence.

        Right, but what are the exact values (especially for @cut)? Can you provide the actual generated HTML that's too wide? I suspect (as some of the other responders have, too) that @cut has some big long chunk of whitespace-less text in it.
Re: HTML coded for in PERL
by polettix (Vicar) on May 18, 2005 at 17:07 UTC
    I fear that if there aren't sufficient spaces inside both $anotate and the elements in @cut you're stuck to a table that goes beyond the browser's border. You could implement some hand-made wrapping mechanism, like the one here in PM that cuts too long lines in code fragments into separate lines.

    BTW, you probably have a typo in your $anotate variable (you refer to $annotate in the post), and you also swapped the closing tags in the first line of the table ("</TR></TD>" instead of "</TD></TR>"); this would not happen if you used CGI :)

    Update: removed reference to the typo, the real variable name in OP's code is $anotate.

    Flavio (perl -e 'print(scalar(reverse("\nti.xittelop\@oivalf")))')

    Don't fool yourself.
Re: HTML coded for in PERL
by Transient (Hermit) on May 18, 2005 at 16:58 UTC
    So your problem is primarily HTML, then, if I am reading this correctly?

    You may get better/more accurate help at an HTML site.

    Beyond that, post (in readmore and code tags), the output HTML and some may be able to help.

    One thing you might want to try to do is use HERE documents, or single quoted print strings to avoid interpolation where it's unwanted.

    e.g.:
    print q{ <table width="100%"> <tr><td>blah blah blah</td></tr> <tr><td> ); print $my_value; print q{</td></tr> </table> };
      I was just wondering if the @cut array was causing the problem. If it is i think i will just leave it as i think i may have to write a function to include a line break in the perl code.
        #!/usr/bin/perl -w my @cut = qw( this is a bunch of entries in an array that I want to se +e what it outputs as if I just print the array as a whole ok? ); print("<B>Digested:</B>@cut</TD></TR>"); __END__ <B>Digested:</B>this is a bunch of entries in an array that I want to +see what it outputs as if I just print the array as a whole ok?</TD>< +/TR>
        Looks like it shouldn't be a problem, but, again, it might help to see the HTML output instead.
Re: HTML coded for in PERL
by wolfi (Scribe) on May 18, 2005 at 20:03 UTC

    my usage of perl is almost entirely for web-development, so i have a lot of experience w/compatability issues etc. - but to sum up...
    ~yes, you should always nest tags the 'correct way' -> <TABLE><TR><TD></TD></TR></TABLE>, but unless you're using something like XML (which is particularly adamant on this point) or really jumbled up the tags - usually the browsers will still render it as expected.
    ~quoting strings is good, but in my experience, only necessary, when working with something other than pixels. (The percentages "100%" should be quoted.)

    BUT... when it comes down to answering your question, those things are irrelevant. AFAIK, browsers will not chop up your long strings - unless they reach some arbitrary limit, that the browser sets for max. widths of pages - which could be thousands of characters. This problem is quite common w/people posting huge links and messing up your nicely designed webpages. :-P

    What i do is what frodo72 touched on. I first dice large strings into arrays. (Which we don't have to do here, since you already have the data in @cut). Then, i'd do something like this...

    # please note: i always miss some punctuation mark or some other overs +ight # so this may/may not work, but you get the idea... print "<TR><TD WIDTH='100%'>The annotated sequence is given below alon +g with the digested sequence<BR>"; print "<B>Annotated:</B>$anotate<BR>"; print "<B>Digested:</B>"; my $printcounter = 1; foreach $_(@cut){ print "$_"; $printcounter++; # you can put whatever number you want in here and substitute <WBR +> for <BR> # if you like (maybe use $printcounter of 4 with <WBR> or somethin +g) if ($printcounter==16){ print "<BR>"; $printcounter=1;}} print "</TD></TR>";
    If you don't explicity put in the optional break <WBR> or definitive break <BR>, browsers usually will not take it upon themselves to do it for you.
Re: HTML coded for in PERL
by jhourcle (Prior) on May 19, 2005 at 00:28 UTC

    To keep this slightly on topic, I'll refer you back to an earlier post about debugging generated HTML.

    However, in this case, it's also important to recognize exactly what HTML is, and isn't intended to do.

    HTML is a way of placing extra information in a text file, to describe what the content is, in a way that can be easily interpreted by programs. You can place suggestions in the file about how you wish for the page to be displayed, but they are not strict rules.

    If the browser can't keep the table within the confines of the screen, there is no requirement that it do so. It may be displayed on a cell phone, or use a large font, or have too much text, or for whatever reason, just not easily fit.

    I personally see no reason for having the table in the first place -- it's one cell wide. It's not tabular data. If all you wanted were borders around items, you can achieve the same effect using CSS.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (2)
As of 2024-04-20 03:12 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found