Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Perl Script Engine

by davebarter (Acolyte)
on Jan 22, 2004 at 11:58 UTC ( [id://323160]=perlquestion: print w/replies, xml ) Need Help??

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

I have an application that is quite complex and I was thinking of developing a mini-scripting language for it.

It would be at a high level and each "function" would link to modules I have already written. the language would have a limited function set and basic flow control.

Eg.

RECEIVE data

IF data(response) = 'YES'

SENDEMAIL "Thank-you" to data(emailAddress)

The idea is that the operator can configure workflows without having to program at the perl level. I was wondering if a module existed to help with custom script language parsing and execution. Or if there was a resource I could use to help me develop one.

Replies are listed 'Best First'.
Re: Perl Script Engine
by rob_au (Abbot) on Jan 22, 2004 at 12:11 UTC
    For complex and recursive parsing in Perl, the Parse::RecDescent module by Damian Conway is that against which other such modules are measured. The learning curve for this module can be relatively steep, but I would recommend taking a look at the article "the man(1) of Descent" in Issue 12 of The Perl Journal and the Parse::RecDescent::FAQ.

     

    perl -le "print unpack'N', pack'B32', '00000000000000000000001010110001'"

      "The man(1) of descent" is also on CPAN here. That way the OP won't have to subscribe to TPJ.

      --
      Allolex

      rob_au points out that the article is available for free from the TPJ archives. I saw the login page and didn't look further.

Re: Perl Script Engine
by dragonchild (Archbishop) on Jan 22, 2004 at 12:11 UTC
    Try 319131 and its responses. There's a number of other threads, but that's the one I remember.

    ------
    We are the carpenters and bricklayers of the Information Age.

    Please remember that I'm crufty and crochety. All opinions are purely mine and all code is untested, unless otherwise specified.

Re: Perl Script Engine
by gjb (Vicar) on Jan 22, 2004 at 12:16 UTC

    You could have a look at Parse::RecDescent. It's a general purpose recursive descent parser generator I've used successfully for a number of problems and that is written by Damian Conway.

    Roughtly speaking, you specify the syntax of your language in an extended BNF like format and write semantic actions in Perl for the appropriate rules.

    Hope this helps, -gjb-

Re: Perl Script Engine
by Fletch (Bishop) on Jan 22, 2004 at 13:45 UTC

    And you might look into Template Toolkit which you could extend with plugins to do your processing and then just discard the output.

      I have done something similar using an custom XML based tag set that I devised. I can't really share the app with you due to restrictions where I work, but I have done enough to begin thinking about how one would put together a module that would facilitate mapping XML tags to perform the desired tasks. I have found there are 2 essential parts: 1) That the XML parser is event based like HTML::Parser - though I would suggest a pure perl solution since the time spend parsing the XML tags is microscopic compared to the time spend performing the mapped tasked. 2) Obviously, that the XML is parsed from the top down. If you are interested in working on such a project, I could help, and I could provide my experiences so far. Interested? My time is limited, but I think that it could be beneficial to the community.
Re: Perl Script Engine
by pg (Canon) on Jan 22, 2004 at 20:56 UTC
    I was wondering if a module existed to help with custom script language parsing and execution.

    I doubt that this is the right way to look at the issue you have. A better design or a better way to look at the problem, is to allow the user to configue the action. If the action is complex enough, XML could be a good idea.

    Your script simply parse the XML file, and call various functions you already implemented as instructed.

    This really should not resolve to the creation of some one-time only mini-language, when a general tool like XML is available.

      I doubt that this is the right way to look at the issue you have. A better design or a better way to look at the problem, is to allow the user to configue the action. If the action is complex enough, XML could be a good idea.

      I wouldn't be so sure myself.

      What exactly is wrong with a domain specific scripting language? Will it really be easier for a non-technical user to edit XML in all cases?

      Yes there are instances where static configuration using XML files makes sense. There are also many cases when it doesn't. As soon as you start getting close to a declarative or procedural language that needs to be written by normal people then I think a domain specific language is an excellent solution.

      For example, look at Ant - which I find a royal PITA for non-trivial systems. I'm sure it seemed a good idea at the time to configure builds using XML, but what you end up with is verbose hard to maintain configuration files as soon as you start doing something vaguely complex. I much prefer systems like Ruby's Rake and Perl's Module::Build that allow you to use a proper scripting language.

      Not many people write programs in XML. This is mostly a good thing :-)


      Update: Of course on the day I write this I hear that Ant is adding scripting features - one less thing to complain about I guess ;-)

Re: Perl Script Engine
by flyingmoose (Priest) on Jan 23, 2004 at 18:53 UTC
    Don't do it. Keep a loadable Perl module and keep the coding in baby Perl, and there will be few problems.

    I once tried, foolishly, to do exactly what you said for writing an automatic test system. It even had while loops and for loops! I spent over half my time writing the parsing system (it ended up being rather cool), but guess what? Everyone was still afraid of the meta-language I invented, and when I had to write test cases, I missed the ability to use real Perl. Might as well just left everything in Perl and it would have been tons easier to maintain.

    My other mistake was farming out some of the development to an intern, who tried to add OO-style functions to everything, and he really didn't grasp nested data structures. Between that and the ugly meta-language code he wrote, it was a total mess. And because it wasn't all Perl, it was hard for me to remember how to refactor that meta language code he wrote and clean it up.

    My advice is to keep things simple, provide a lot of well documented utility functions, and do this in baby-Perl. Baby-perl is just as readable as shell script, if not more so. The implementation of the the utility functions themselves can be as Perlish as you like. Non-Perl programs may need a scripting language, but Perl already has great use at doing that.

    Off-topic: anyone have experience embedding Lua in C programs? Did you like the results?

      The more I think about it, the more right you are. I think I'm just going to concentrate on writing clear clean maintainable perl. But I like the idea of using XML as the configuration format Thanks for all of the useful comment Dave

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others taking refuge in the Monastery: (6)
As of 2024-04-24 09:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found