Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

Replacing perl module with external program

by sgbotsford (Initiate)
on Jan 29, 2014 at 19:13 UTC ( [id://1072563]=perlquestion: print w/replies, xml ) Need Help??

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

I use Template Toolkit 2 version 2.25 in combination with Multimarkdown with the following bits from cpan. Documentation for TT2 is http://www.template-toolkit.org/docs/

perl5/site_perl/5.12.4/Template/Plugin/MultiMarkdown.pm perl5/site_perl/5.12.4/Text/MultiMarkdown.pm perl5/site_perl/5.12.4/Text/Markdown.pm

Template Toolkit in essence implements a new interpreted language.

In actual use there are two calls:

... other use statements [% USE MultiMarkdown %] ... more template [% FILTER multimarkdown %] markdown stuff goes here [% END %]

MultiMarkdown has had 3 major releases, and I think aleph-null minor ones. The current release is a C compiled PEG grammar version 3.5.

I would like to replace the filtering presently happening in Text::MultiMarkdown with input and output to /usr/local/bin/multimarkdown, but I haven't a clue how to go about this.

I looked in the categorized examples and on CPAN. So far I've found references to open2 and open3, to 284 references on CPAN that ahve IPC:: as part of their name.

It would seem to me that there is much re-inventing of this wheel, and this lowly novice is so unskilled that he not only does not know which is the better wheel, but does not even know how to attach the wheel to his oxcart.

Please, learned masters, attempt this one's enlightenment.

Replies are listed 'Best First'.
Re: Replacing perl module with external program
by Your Mother (Archbishop) on Jan 30, 2014 at 03:42 UTC

    This appears to be a great idea and is already done correctly in Text::MultiMarkdown::XS.

    Punchline: much faster

    Rate perl xs perl 386/s -- -67% xs 1179/s 206% --

    Test code

    use strictures; use Template; use Benchmark "cmpthese"; my $tt2 = Template->new(); my $template = do { local $/; <DATA> }; cmpthese 10_000, { xs => sub { $tt2->process(\qq{[% USE MultiMarkdown(implementation => "XS") + -%] $template}, {}, \my $toss) or die $tt2->error; }, perl => sub { $tt2->process(\qq{[% USE MultiMarkdown(implementation => "PP") + -%] $template}, {}, \my $toss) or die $tt2->error; } }; __DATA__ [% FILTER multimarkdown %] An h1 header ============ Use 3 dashes---for an em-dash. Use 2 dashes for ranges (ex. "it's all in chapters 12--14"). Three dots ... will be converted to an ellipsis. Bulleted list-- * this one * that one * the other one > Block quotes are written like so. Paragraphs are separated by a blank line. Obviously. [% END %]

    Caveats, help till it normalizes

    Text::MultiMarkdown::XS is still experimental and has no regular releases so it can't be installed with cpanm, even with the -dev flag. It also has a problem (for me) with the Makefile.PL (no printf for file handle) which I fixed by manualling adding use IO::Handle and installing with (update, took out note about pre-installing multimarkdown per anonymonk)–

    wget http://search.cpan.org/CPAN/authors/id/A/AN/ANDREWF/Text-MultiMa +rkdown-XS-0.001_03.tar.gz gnutar xpfvz Text-MultiMarkdown-XS-0.001_03.tar.gz cd Text-MultiMarkdown-XS-0.001_03 # add "use IO::Handle;" to Makefile.PL make Makefile.PL make make test make install # Might need "sudo make install"

    Eventually, for future readers, this, or your favorite alternative, is all it should take–

    cpanm Text::MultiMarkdown::XS

       cpanm ANDREWF/Text-MultiMarkdown-XS-0.001_03.tar.gz ought to install it :)

      But FWIW, it is an old version , "4.1.1" versus "4.5.1"

      Also FWIW, on win32 the tests fail with Free to wrong pool ...

        Oh, I thought it was relying on installed lib/binary. Thanks for the update!

Re: Replacing perl module with external program (is dumb)
by Anonymous Monk on Jan 29, 2014 at 21:28 UTC
    Well, first things first, locate the docs for this "markdown" program, this is the beginning

      Thank you for your helpful suggestion to this unworthy novice. Yes, I am dumb. I humbly apologise for not having yet attained enlightenment such as your Exalted Greatness has.

      I have spent a day reading and rereading the TT2 documentation. It may as well be in Egyption Hieroglyphics. I have also gone into the source. I don't understand the source.

      While my question uses a specific example, the question is a more general one. Here is a module that is used as a filter. A data stream comes into it, is modified, then returned to the program for further operations. Is there a general guide on how to replace such a filter, done by a module, with a module that uses an external program?

        In addition to AM's suggestion above, I would look at the possibility or need of supporting multiple versions. Perhaps replace the original module with a find-and-load module, and move the original one to Module::Name::VersionX, where VersionX is the support code for a single, standardized perl API.

        --MidLifeXis

        ... Is there a general guide on how to replace such a filter, done by a module, with a module that uses an external program?

        Its easy to create a new Template::Plugin::Filter, you simply copy/paste the existing Template::Plugin::MultiMarkdown, rename it (say Template::Plugin::MultiMarkdownBin ), and replace the module-calling-part with something that calls the external program.

        To know how to call this external program, the first step is locating the documentation for this external program to learn how to use it.

        Then use something as discussed in perlipc (open2,open3, qx ...

        You can use Capture::Tiny, IPC::Run3 ....

        But you start with the documentation of this external program...

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (5)
As of 2024-03-29 02:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found