How can I replace the use of a perl module with a call to an external program?

151 views Asked by At

Here's what I'm starting with

#$Id: MultiMarkdown.pm 4103 2009-03-02 20:41:50Z andrew $

package Template::Plugin::MultiMarkdown;

use strict;
use base qw (Template::Plugin::Filter);

use Text::MultiMarkdown;

our $VERSION = 0.03;

sub init {
    my $self = shift;
    $self->{_DYNAMIC} = 1;
    $self->install_filter($self->{_ARGS}->[0] || 'multimarkdown');
    return $self;
}

sub filter {
    my ($self, $text, $args, $config) = @_;
    my $m = Text::MultiMarkdown->new(%{$config || {}});
    return $m->markdown($text);
}

1;

I want to change the reference to the Perl moudule Text::MultiMarkdown to use an executable /usr/local/bin/multimarkdown

I think I need to change:

use Text::MultiMarkdown; 

To

use IPC::run3

and then the my $m line involves a call to run3, but then I'm lost.

I have found some mentions of how to write a filter on both the Template Toolkit mailing list, and on Perl Monks, but in both cases the answer assumes that I know something that I don't and it goes right over my head, hence the request here.

I asked a similar question earlier, but the answer given didn't seem to apply to using an external program.

2

There are 2 answers

0
Håkon Hægland On

Here is a simple example of a filter that calls an external program ( perl ) to replace all occurences of "Hello" with "Bye". It uses IPC::Run3:

package Template::Plugin::MultiMarkdown;
use strict;
use base qw (Template::Plugin::Filter);

use IPC::Run3;
our $VERSION = 0.03;

sub init {
    my $self = shift;
    $self->{_DYNAMIC} = 1;
    $self->install_filter($self->{_ARGS}->[0] || 'multimarkdown');
    return $self;
}

sub filter {
    my ($self, $text, $args, $config) = @_;
    my $out;
    run3 ['perl', '-pE', 's/Hello/Bye/g'], \$text, \$out; 
    return $out;
}
0
Borodin On

Your ideas and design are wrong. You mustn't extend the purview of any subclass beyond its parent. Overloading a method to use a system call is horrible.