Being fairly new to Perl, I am constantly searching for quick references, tutorials, books etc. which might make the learning easier.

One of the best methods I've found is to "decode" existing Perl code to see what the programmer is trying to accomplish.

I was recently thumbing through "Perl and CGI For The World Wide Web" by Elizabeth Castro, and while a little too elementary for me, I found her teaching technique extremely easy to read through. And it got me thinking.

Since Perl was originally designed to manipulate text, how easy would it be to design a program that takes a Perl construct and returns an english explaination of what it is trying to accomplish or doing? Something like you type the following into a form:

@classes = split(/,/, $class);

and the program returns an explanation such as:

"@classes" is an array being built from splitting the scalar variable "$class" into individual items separated by the argument between the "//", or in this case a "'".

Has anyone seen anything like this? Or is it even practical? Seems like it would be a great resource for beginners like me when trying to learn.

I have used the MAN pages and perldocs, but it is easier for me to learn when seeing application in different instances.

Any thoughts?

Replies are listed 'Best First'.
Re (tilly) 1: Idea for Perl Learning Tool
by tilly (Archbishop) on Apr 29, 2001 at 06:01 UTC
    It is essentially impossible for reasons listed at On Parsing Perl. However tools like B::Deparse are able to do a "95%" job of parsing Perl, and you might be able to build a useful tool on top of that.

    Also Perl 6 is supposed to have a saner syntax when it comes...

      The reasons in that thread are talking about having something other than perl itself parse Perl scripts. B::Deparse doesn't parse Perl, it dumps the results of what perl itself parsed.

      The only reason that B::Deparse is missing 5% (or less) is because the module is still young. Some of the bigger missing pieces were just put in place (from what I read) so I suspect that the latest version (not generally available) can successfully "deparse" quite a few working scripts such that the results are functionally identical.

              - tye (but my friends call me "Tye")
Re: Idea for Perl Learning Tool
by dze27 (Pilgrim) on Apr 28, 2001 at 22:51 UTC

    I don't know of any existing tools like this.

    However, there is a really cool toolkit for doing regular expressions in the new ActiveState Komodo interface. You can type in a regexp and some text to match. It highlights what is currently being matched and explains what is going on in English. You can advance through each part of it and it shows what is getting stored in each of the variables $1, $2 etc.

    This might at least help with one aspect of perl although a general tool might be a great learning aid as you mention.

      For RegEx explanation, japhy does a great job with that in his module.

      He who asks will be a fool for five minutes, but he who doesn't ask will remain a fool for life.

      Chady |
      Says dze27:

      <blocckquote> there is a really cool toolkit for doing regular expressions in the new ActiveState Komodo interface. Thanks! I am glad you liked that.

      Mark Dominus
      Perl Paraphernalia

        Absolutely. The way you get the immediate feedback on what is being matched is extremely helpful. Thank *you*!

        I don't know if this is the place for it, but I was thinking it would be even better (feeping creaturism, here we go) if you could have a list of strings to be matched. So instead of entering just one, you could enter N strings, maybe 1 per line, and watch the regexp match them at the same time.

Re: Idea for Perl Learning Tool
by larsen (Parson) on Apr 29, 2001 at 03:34 UTC
    I think that what you're talking about could be useful during the very first steps of learning a new language. But soon, you will be able to cope with larger chunks of code, perceiving patterns and idiomatic uses of Perl. A software that understands the meaning of even 4-5 lines of code could be a huge effort: understanding semantics is complex in artificial languages such as in natural languages.

    By the way, Perl has many syntax constructs that are difficult to read even for experienced programmers: for example, regular expressions. In this case, tools like this are very useful :)

Re: Idea for Perl Learning Tool
by gregor42 (Parson) on Apr 30, 2001 at 17:52 UTC

    One of the best methods I've found is to "decode" existing Perl code to see what the programmer is trying to accomplish.

    If you have learned to appreciate learning in this manner then you might be very interested in mining through the Obfuscated Code section. It's a great motivator to learn all sorts of things about PERL that are normally overlooked, and it gets your brain thinking in new ways about PERL.

    Wait! This isn't a Parachute, this is a Backpack!