Perl isn't... FORTRAN. Still, it's often handy to be able to see (and talk about) line numbers in code listings. Editors and pastebins do this; why not PerlMonks?

It turns out that a slightly obscure feature lets us do just that. Follow these instructions and you will see line numbers alongside every block of code here. (This will not affect inline code.)

First navigate to Display Settings. We will make a few changes here.

In the Code Listing Section, be sure Code Wrapping Off is not checked.

For Code Prefix, enter:    <span class="line-number">&1;</span>&nbsp;

Line numbers will mess you up if you like to copy code right out of node text; instead, use the several [download] links provided. Be sure Don't show embedded d/l links is not checked and Show download link on code N lines long or more is set to a very small number, or left blank.

To style the new line-number class, we need to fix the stylesheet. We can paste the following into On-Site CSS Markup:

.codeblock { margin-left: 1em; background-color: #ddf; } .line-number { color: #779; background-color: #fdd; }

That's it; we're done. You may want to experiment with the color balance but that's... up to you.

Thanks

roboticus took pity on me whining in CB and pointed me to New code wrap options.

Corion made a good notice which unfortunately did not pan out.

Honorable mention goes to other six a.m. CBers who put up with the fumbling.

Feste: Misprison in the highest degree. Lady, cucullus non facit monachum. That's as much to say as, I wear not motley in my brain....

Replies are listed 'Best First'.
Re: Line Numbering in Code Listings
by cavac (Prior) on Dec 10, 2011 at 17:33 UTC

    Nice.

    Btw, to get rid of code wrapping, setting the Code Wrap Length to its maximum of 32766 should get rid of it in more or less all cases except maybe some Obfuscations (where line numbers make limited sense anyway).

    If we still hit the code wrapping limit in SoPW, we already know where the design issue in that code snippet is ;-)

    Don't use '#ff0000':
    use Acme::AutoColor; my $redcolor = RED();
    All colors subject to change without notice.
Re: Line Numbering in Code Listings
by Khen1950fx (Canon) on Dec 10, 2011 at 21:48 UTC
    Thanks for the info. Your example is great. I've tried it on occasion, but it always comes back to haunt me, so I don't fiddle around unless it's absolutely necessary:). There are times when I can see using line numbers. For example, when I'm editing a post and the line number would be helpful like this, looking at a charblock:
    #!/usr/bin/perl use strict; use warnings; use PerlIO::via::LineNumber; my $post = '/root/Desktop/myLines.log'; die unless open my $out, '>via(LineNumber)', $post; print $out <<'EOD'; { bidi => "L", block => "Basic Latin", category => "Lu", code => "0041", combining => 0, comment => "", decimal => "", decomposition => "", digit => "", lower => "0061", mirrored => "N", name => "LATIN CAPITAL LETTER A", numeric => "", script => "Latin", title => "", unicode10 => "" upper => "" } EOD print $out; close $out;
    With line numbers:
    1 { 2 bidi => "L", 3 block => "Basic Latin", 4 category => "Lu", 5 code => "0041", 6 combining => 0, 7 comment => "", 8 decimal => "", 9 decomposition => "", 10 digit => "", 11 lower => "0061", 12 mirrored => "N", 13 name => "LATIN CAPITAL LETTER A", 14 numeric => "", 15 script => "Latin", 16 title => "", 17 unicode10 => "" 18 upper => "" 19 }