http://www.perlmonks.org?node_id=18986

Newcomers to the site seem to have trouble understanding and using the CODE tags particularly when posting to Seekers of Perl Wisdom. As a result they often don't get the quality of reply they deserve. Is it possible to have two text areas, one for normal text and one for the code fragment? The code one would have the tags wrapped around automatically.

Alternatively have a single area which has the tags wrapped by default, with a "Does not contain code" button for turning the wrapping off.

Replies are listed 'Best First'.
RE: Automatic CODE tags (kudra: potential problems)
by kudra (Vicar) on Jun 20, 2000 at 16:48 UTC
    The first wouldn't work very well if the person wanted to have two pieces of code. Something like this might be intended:
    I see this in a book:
    for (keys %hash) { print; }
    But when I do it like this, it doesn't work
    for (key %thisisnotintendedasaseriousquestion) { print; }
    What's wrong?
    It would be rendered something like (case second box):
    I see this in a book: But when I do it like this, it doesn't work
    Code:
    for (keys %hash) { print; } for (key %somename) { print; }

    I'm imagining that the 'Code:' is added automatically. Having code on by default would give something which is somewhat hard to read, for example (case all preformatted):
    I see this in a book: for (keys %hash) { print; } But it doesn't work in my example, please <a href="www.somedomain.com/dev/null>look at it here!</a>

    Yet I agree that something should be done... Perhaps information on how to display code and escape characters could be at the top of the page before the submission box on the Seekers of Perl Wisdom section only, in hopes that it will be read? (I don't think I'd like seeing it there, so don't consider me in favor of my own suggestion!)
RE: Automatic CODE tags
by jjhorner (Hermit) on Jun 20, 2000 at 16:50 UTC

    Another solution would be to look for the hash-bang line that would seem to denote the start of code, then collect it and everything up to and including the last semi-colon, and wrap in code tags. Not a perfect solution, and I'm not sure how feasible (what with everyone having different hash-bang lines and all), but a possible solution.

    We could just make everything in the text box preformatted with a PRE tag. That wouldn't be a perfect solution either, but still a possible solution.

    #!/usr/bin/perl
    
    use warnings;
    use strict;
    
    #testing pre and code
    

    Update:

    After seeing kudra's response, it shouldn't be that hard to run a regexp against everyone's post (non-greedy) to be able to split up code and text. Having two code blocks could be as easy as a good rexexp.

    J. J. Horner
    Linux, Perl, Apache, Stronghold, Unix
    jhorner@knoxlug.org http://www.knoxlug.org/
    
      Why not just put a big All code should be inside <code></code> tags! on the edit page? I know it's a bit naive to assume that everyone who comes here can read, but anyway... :)
        I think that their should be some help at the top of the postings area. You could even have it only pop up when is user is below a level. Cuz I am pretty sure most people once they get to level 2 or so know how to do it. Also they could put the <code> put code here </code> inside of the text area that we have to put data into.
        <TEXTAREA WRAP=VIRTUAL NAME="reply" ROWS=10 COLS=60> <CODE>put code here&lt;/CODE> </TEXTAREA>


        --BigJoe
        Maybe even put the code tags into the text area so that all they have to do is get the insertion point in the right place. I suppose it is all a matter of getting the balance right -easy for first timers but without annoying the more experienced users.

        I think there is an assumption that people are comfortable with this type of HTMLish structure that perhaps has never been challenged.
RE: Automatic CODE tags
by Corion (Patriarch) on Jun 20, 2000 at 18:52 UTC

    I think that a very simple code-detection could already be sufficient. If the post contains something, which remotely resembles Perl code, and it dosenīt contain a single <CODE> tag, then wrap the whole post in <CODE> tags.

    This is very crude, and should be toggled off in the preferences, but if you are smart enough to realize that you have user preferences, you already have learned enough about Perlmonks to know that you can use the TT><CODE> tags for your Perl code.

      I think that a very simple code-detection could already be sufficient. If the post contains something, which remotely resembles Perl code, and it dosenīt contain a single CODE tag, then wrap the whole post in CODE tags.

      A simple test would be something like:

      if(Code-catcher-not-disabled){ foreach(@line-of_post){ if m?\</CODE\>?i{ { last; } eval { $_ }; #Actually something to do with Safe module if not $@ { $codify=1; } } }
      (Requires clean up, and has many efficency improvements that can be made, but you get the idea. Basically if there is a line (or we could set a tolerance level...say, 4 lines?) of code that will eval, it's likely perl code. THis would also catch someone who remembered the CODE tag, but not the closing tag.
      Note: This should be done in a sandbox, such as the Safe module, but I haven't toyed with that enough to include it in this pseudocode.

      Another option is to use one of the Perl Parsers out there to check syntax, but that involves more overhead. eval already does all the checking, and properly buried in a Safe, it shouldn't affect the exterior code at all.

        Of course, there should be some more logic added, as most code that gets posted, won't compile. That's why it was posted in the first place. But then again, the analyzer should simply lean more towards code than towards text, because a monospaced text is far easier to read than vario-spaced and line-joined code.

RE: Automatic CODE tags
by Anonymous Monk on Jun 21, 2000 at 12:05 UTC

    I haven't been clear on the right use of monospacing tags here, either--
    for instance, why <CODE> is preferred to <PRE>.

    Tags like <KBD> and <CODE> are normally used for monospacing
    in-line. If you wrap a multi-line block of text in <CODE> tags, you expect
    the line breaks to disappear when the HTML file is displayed
    in the browser unless you also add <BR> tags at the end of each line.

    But wrap that same text in <PRE></PRE>, and the line breaks are
    handled fine without need for <BR> tags.

    I recall hearing a while ago here that the site turns <CODE> into <PRE>--
    but then wouldn't that be the wrong thing to do when the intent was
    to add monospaced text in-line?

      This by no means represents the official reason, it's simply my own thoughts on the reason behind the <code> tag.

      It looks as if the tag is handled in a special way (just as the square bracket links are). The fact that you can select how code is displayed in user settings seems to indicate this. Currently it looks as if the <code> tag is treated as a <pre> tag.

      One advantage of having the tag interpreted is that it's easy to change what it does either globally or by user. I imagine the term 'code' was chosen because the content is, in fact, code, so it ought to be easy to remember. Unfortunately it appears to be confusing as well. Using <pre> would also be confusing, because there's no guarantee <code> always is or will be directly converted to <pre> (<code> seems to be replaced with <pre><it> for me; if I turn off 'large code font' in my user settings, it replaces it with <pre><it><font size=-1>).

        <Code> also HTML-ize's any special characters (ampersands, angle-brackets, etc)
RE: Automatic CODE tags
by httptech (Chaplain) on Jun 21, 2000 at 22:11 UTC
    Why not just add extra, largish instructions for initiate-level users only?
      I suspect that people are so desperate for some sort of answer that they tend not to read anything, even if it was in foot high letters. Another thought occurred to me last night - are people expecting a reply through the e-mail address that they give when they register? Again the register user screen makes this clear but do people read it?

      In the "Debug this!!" post that triggered this thread I suspect that the original poster never read any of the replies - pehaps he didn't know that he had to or even how to.
        I'm thinking maybe the "Debug this!!" poster expected his post to show up on the main page of the site, and when he didn't see it there he kept posting. Another thing that could be explained for initiates, that their post will not appear on the main page unless it is moderated up. This could be put on the page after they submit the question.
RE: Automatic CODE tags
by t0mas (Priest) on Jun 20, 2000 at 16:53 UTC
    A new quest: Write a automatic-code-identifier-and-tagger.pm with subs that executes as default when someone (or just AM) posts at Seekers of Perl Wisdom.

    /brother t0mas
      Once this is working it should then be fairly trivial to detect the most common questions/faults and automatically generate a reply. At the same time a critique of the coding style could be performed and layout and performance improvements suggested. Sorry, ... got my silly head on this afternoon.