Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked
 
PerlMonks  

Seeker of Perl Code Review

by bbfu (Curate)
on Mar 22, 2001 at 05:43 UTC ( [id://66216]=perlquestion: print w/replies, xml ) Need Help??

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

I've finally finished my syntax highlighting module. Now I'd like to get the community's review of what I've written.

I'm going to post links to the code here, instead of the code itself, for 3 reasons:

  1. It's rather large. Weighing in at just over 2600 lines (including POD), it's the biggest project I've yet completed.
  2. It's wide. I edit code in 120x48 window but most monk's <CODE> tags wrap at around 70 characters. Trust me, it looks horrible wrapped to 70 characters. :-)
  3. I wrote this nifty web page, using the module, that lets you view it (along with the script that generates the web page, of course). Plus it lets you enter in your own code to try out with the module, as well as define custom formatting (colors) and such, so it's fully self-demonstrating. Anyway, I'm proud of it.

I'd like to get people's thoughts on the code itself (please remember, though, that this is the first version; I haven't even optimized it!), my style, the concept, the documentation, as well as just general impressions. Suggestions for improvement and other comments will be appreciated.

I realize that it is kind of a lot of code, and that people are busy. Even if you only read the documentation, I would still like to hear your comments. Even general impressions after only skimming the code are OK.

I promise I won't get offended or upset by any comments. I'm open to criticism (though I prefer it to be constructive). If there's something you'd like to say privately to me, feel free to /msg me, or you can email me at join('@', 'darkness', 'yossman.net').

If the general impression is that I did a decent job of it, I plan to include it in my "portfolio" of code. If people think that it would be useful, I also plan on posting it to CPAN, possibly along with the couple of programs I wrote to use the module.

Okay, enough talk. The code is located at http://exnoctum.port5.com/cgi-bin/perl-highlighter.pl. I have had a few of my friends tell me that they have trouble accessing the site. I think that the server occasionally doesn't respond but should work if you just try it again. (It is a free account, and I guess you get what you pay for. Though it's nice that there are free sites that give Perl access.) If you consistently can't access the page, let me know and I will look into trying to set up a mirror.

Note to the editors: I had trouble deciding where to post this. I ended up choosing SOPW because it seemed most appropriate (to my mind, anyway). The editors are free to move it if I chose badly. Mea culpa.

Okay... Let the learning begin (er, continue)! :-)

bbfu
Seasons don't fear The Reaper.
Nor do the wind, the sun, and the rain.
We can be like they are.

Replies are listed 'Best First'.
Re: Seeker of Perl Code Review
by jeroenes (Priest) on Mar 22, 2001 at 12:46 UTC
    Well, I tried the following code:
    my $index = $#array; #Doesn't treat array as comment my $txt = "$index and something"; #Highlights $index! print OUT "$txt->test()"; #Doesn't highlight ->test() s/ (\d+ #test \w{1,3}) (\s) / $2 ? "yes" : $1 /ex; #Recognizes s///ex as the regex
    And the highlighted code looked OK. So you did a better job than emacs/jed (dunno about vim). Only thing is, it doesn't recognize the code within the regex, and also not the comments within the extended regex. But maybe you hit the ceiling here, as merlyn states that "only perl can parse perl code" (paraphrased).

    The highlighted syntax looks pretty, especially in color on white. Methinks your only competitor is a2ps, you can find it on Outside links.

    Nice job!

    Jeroen
    "We are not alone"(FZ)

      Ah, I'm glad someone's really putting it to the test. :) And yes, it does do better than VIM. :-p (You should see VIM choke on the relatively simple: qr ( one ( two ) three )x;!)

      Regular Expressions are one of the weak points of the module. It's even listed under KNOWN ISSUES or LIMITATIONS (or at the mirror) in the documentation. I may add support for regexp-special characters later, and maybe even set it up to handle the /e option. The big problem with the /e option is that it requires back-tracking (see my recent reply).

      I tried to show off, in the Test file, some of the tricky constructs that the formatter handled correctly. Especially that funky here-doc stuff at the bottom. :) Note that VIM doesn't even handle formats at all. All in all, I've tried to make it as true to Perl as I could (without actually parsing the code!). Considering I just shatter the string with a heavy regexp (or 10) and then walk through the pieces, I'm surpised I got it working this well! :D

      Thanks for the tip on a2ps! I originally started writing this module because I didn't know of anything that would highlight Perl code for presentation-or-printing. I wanted to be able to really show off my code on the web so I started working.

      I looked through the home page for a2ps and it looks nice. Very versatile as far as languages it handles, though I think that limiting it to PostScript output makes it not as good for my orignal purpose (of converting code to HTML). Do you know of a PS to HTML filter? I certainly never plan to extend my formatter to handle languages other than Perl (people use languages other than Perl???) so I think a2ps will always beat me out in that respect. :) I didn't install it to try it out so I don't know how well it handles some of the more esoteric constructs of Perl. It certianly looks powerful enough to do a good job!

      Well, thanks again for your reply, and let me know if you get a chance to delve into the depths of the module and can offer any suggestions for improvement!

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

        Converting PostScript to anything else is rather hard. To see why you should stop thinking of a PostScript file as a document but instead think of it as a programming language which is capable of giving a printing device instructions that will cause it to print out the document.

        Yes, if you send a PostScript printer the correct document rather than printing it will instead install a stealth backdoor that contacts someone and tells them that their latest victim is waiting. I forget whether the default password on HP printers was "OOOO" or "OOOOOO", but it is fairly simple to guess. And, as you can imagine, most people don't realize that their printer is a fairly capable reprogrammable computer, with an operating system (written in PostScript), full networking capabilities, etc.

        I will give it a better look, but when I have some time at hand. It might take a while.

        As for ps2html: Hardly. Postscript is more an end-language, something that you don't like to convert to text-like things. You'd better think like ps2pdf (exists), as pdf is a more or less 'accepted' web-standard.

        Later on I saw you remarks on that your script only works line-wise. That makes sense, and I don't think that you can except this kind of script to detect all perl-oddities.

        Cheers,

        Jeroen
        "We are not alone"(FZ)

      Just for the record, emacs in cperl-mode (by Ilya Zakharevich) got all of these except the comment in the x'd regex.

      p
(bbfu) (code mirror) Re: Seeker of Perl Code Review
by bbfu (Curate) on Mar 22, 2001 at 07:58 UTC
Re: Seeker of Perl Code Review
by kschwab (Vicar) on Mar 22, 2001 at 07:43 UTC
    I haven't delved into it enough to comment on the code itself, but the output is certainly well done.

    The "Black on White (Printable)" mode looks great, even when printed on my old HP LJ5MP !

    I'll be downloading and using it for docs. Thanks ! bbfu++

      I'm glad that you like it!

      Since you expressed interest, I've created a tarred-gzipped package for you to download containing all the files except Test that are viewable on the web page. Note that this is not an install. It is just the the three files (module, viewperl, and perl-highlighter.pl) from the web page. The (relative) directory structure for the module is set up in the file but you will probably want to copy it into your module directory on @INC.

      Anyway, the tar-ball can be found at http://exnoctum.port5.com/syntaxhighlightperl.tar.gz or at the mirror http://balder.prohosting.com/exnoctum/syntaxhighlightperl.tar.gz. Enjoy!

      Update:Ooops. I had typos in those links! Thanks to Kanji for catching that!

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

Re: Seeker of Perl Code Review
by diskcrash (Hermit) on Mar 22, 2001 at 12:39 UTC

    bbfu!

    What a ton of work!

    I browsed the sample files and looked at the doc pages. Took a quick look through the code.

    Comments

    The black on white was most readable to me, except a darker gray would be good for comments. (I'm a tad colorblind.)

    The doc page was very well formatted and thought out.

    The code was well commented and formatted.

    Is there any thought to a switch that would highlight bracket/paren pairs? Or flag unpaired "pairs".

    Overall, what a great tool!

    Diskcrash

      Thanks! To respond to your comments:

      1. The black on white was most readable ... darker gray ... for comments.

        I tried to make a few general Styles that would cover most of the range, but they were really only intended as examples. Did you try the Customize It (Style) button? You can easily alter one or two things from one of my pre-defined Styles, or go crazy and define your own. :-) And if you write your own implementation using the module, of course, you can insert any formatting you like.

      2. The doc page [and code] [were] well formatted [and commented]

        Thanks!

      3. Is there ... a switch that would highlight bracket/paren pairs? Or flag unpaired "pairs"[?]

        Currently, parentheses, brackets (square and curly), and semicolons are all considered 'Symbol's. You can only format them all together. :-( It would be trivial to modify the formatter to distinguish between paren/brackets and "other symbols" so perhaps I'll add it soon. The module doesn't actually parse the code, however, since we all know only perl can parse Perl. ;-) Rather, it works by "tokenizing" the code: breaking it into small pieces that can be recognized as "syntactically significant" and formatted properly. This is not a 100% solution but it seems to work well (enough). One big problem with this approach (or perhaps just my implementation of it) is that it is rather stream-oriented. In otherwords, the formatter can't back-track to the unmatched opening bracket when it realizes that it is unmatched. I'm hoping to eventually incorporate a back-tracking mechanism, as it will be usefull for many things that can't currently be done, but I'm having trouble figuring out how to do it. Hopefully someone here at the Monastery will chime in and help out! :-)

      Well, once again I would like to thank you and everyone else who has responded. And I hope that people will find this module usefull!

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

Re: Seeker of Perl Code Review
by t0j0 (Novice) on Mar 22, 2001 at 12:25 UTC
    i think program is nice except for certain thing..

    a) i thing it will be nicer if you could put the numbering all within one separate column(TD) rather than having it together with the code so that people can just highlight and copy code straight from the output. and not messing up with the line number

    b) i guess the font is quite big especially when you want to show large program. coz people dont really like to scroll too much..

    well.. i'm only giving my opinion towards your prog. btw, good job that you have done there..

      Thank you for your comments! I will definately look into separating the line numbers into <TD>'s. The reason I chose to go with the all-in-one originally was that it was easiest (psh! laziness!) and smallest (separating the line numbers will require me to print many more <PRE> tags where-as I have all of it in one large <PRE> tag now) but I realize now that people may indeed want to have a copy of the code (I do have a tar-ball of the code available; see one of my previous replies).

      As for the font, I set the default Style to the one I like best (I happen to use that scheme in VIM; though VIM doesn't handle several constructs properly that my module does) but there are several other pre-defined Styles available, as well as a page for creating your own (or customizing one of the pre-defined ones). You can even change the font! :-)

      Well, thanks again for taking the time to look it over and reply! Let me know if you get the chance to look at the module code. :-)

      bbfu
      Seasons don't fear The Reaper.
      Nor do the wind, the sun, and the rain.
      We can be like they are.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (6)
As of 2024-04-18 21:04 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found