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

I'm trying to help someone with a script which uses a library called txt2html -- see http://www.aigeek.com/txt2html/ for details. (Disclaimer: I am not able to use the canonical CPAN module for this. I know that's probably the right thing to do but I'd like to understand what's going on with this code more than I'd like to get it right, if you see what I mean.)

Despite the existence of this website, I've been unable to get his cgi-input example ( http://www.aigeek.com/txt2html/txt2html.cgi-source ) to work.

But more to the point, I don't really understand this way of doing Perl.

His library consists of many many subs, which are called by "&main()" at the bottom.

The cgi version of using this library is,

open( T, "|/path/to/lib -l /path/to/.txt2html.dict" ); print T $text;
where the "-l" is a switch and the other file is a dictionary.

I just don't understand how this "pipe" method works. That's supposed to print out to screen the contents of the variable $text, as munged by his program?

Also, any ideas as to why I can't make it work on my server? It doesn't die when I put an '|| die "$!"' after the "open" line, it just prints nothing to screen. And there is something in the $text variable, I checked.

So my questions are,


--
($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;

Replies are listed 'Best First'.
(jeffa) Re: txt2html
by jeffa (Bishop) on Jul 16, 2002 at 02:59 UTC
    Time after time i hear "i can't use a CPAN module", but never do i hear why one can't use a CPAN module. But i will appease you nonetheless.

    First, you are correct, that is not a module or a library. It is a Perl script, which IMHO is a horrible way to solve this problem. Better would be to make a library, but then again, since you can't use CPAN module, you surely couldn't use a library. ;)

    Piping works by attaching the output of one process to the input of another process, or vice versa. So, in this example, you fork and exec another process to invoke another Perl script. This is overkill. A better way is to use a library or module that you use or require subroutines into your CGI script's namespace.

    Why doesn't it work? Well, if you are using this line verbatim:

    open( T, "|/path/to/lib -l /path/to/.txt2html.dict" );
    that will never work, because /path/to/lib is a directory. Surely you mean something like:
    open( T, "|/path/to/txt2html.pl -l /path/to/.txt2html.dict" );
    because you are going to execute the script in question, not import it into your CGI script's namespace. What can you check? Run everything in quotes (except the leading pipe, of course) from the open in a shell first. This is a sane way to ensure that a) the script to execute is executable (chmod 755) and b) the dictionary file is readable (chmod 644).

    I got the example to work. I copied (as root) txt2html.pl to /usr/bin/txt2html and copied txt2html.dict to my public_html directory for user 'jeffa'. I modified the CGI script to actually ask the user for some text to mark up:

    use strict; use CGI qw(:standard); print header; my $text; if ($text = param('text')) { open(T, "|/usr/bin/txt2html -l /home/jeffa/public_html/txt2html.dic +t"); print T $text; } else { print start_html, start_form, textfield('text'), submit, end_form, end_html, ; }
    And it worked fine. But, i still recommend you use HTML::FromText or HTML::TextToHTML. If you can download and install a solution that forks and execs to another script, you can download and install a CPAN module. If you feel that you can't install modules due to permissions, correct this misinformation by reading this excellent Guide to Installing Modules.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      never do i hear why one can't use a CPAN module.

      In this case it's not me, it's my friend. They want a quick solution to their problem, and their problem is cause by this particular thing, the library, which is embedded in their code. I could advise them to do it another way, but they really wouldn't.

      I promise to install CPAN modules where possible.

      OK your advice was very greatefully received:

      Surely you mean something like:

      open( T, "|/path/to/txt2html.pl -l /path/to/.txt2html.dict" );

      Yes, thanks for that. And thanks for your code. I uploaded it and ran it, and it worked.

      All I really want to do is tweak the regexes inside this script so that it doesn't attempt to find list items and convert them, and leave it in place in the rather awful structure it's in. So I wanted to install/use it so that I could test my regexes.
      --

      ($_='jjjuuusssttt annootthheer pppeeerrrlll haaaccckkeer')=~y/a-z//s;print;