Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Obtaining the output of a system call, not the return status

by Superlman (Pilgrim)
on Feb 09, 2002 at 21:50 UTC ( [id://144401]=perlquestion: print w/replies, xml ) Need Help??

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

I need a quick and dirty way to convert text to html. It turns out there's a nifty perl program already made, appropriately named txt2html. I've installed it and it works fine, but I'm having trouble integrating it into my script. Here's what I've been trying:

my $html = system("echo $plaintext | txt2html --extract");

However, $html is always 32512. Any ideas on how to fix this, or other ways to go about it?

Replies are listed 'Best First'.
Re: Obtaining the output of a system call, not the return status
by wog (Curate) on Feb 09, 2002 at 21:58 UTC
    You probably want to use backticks AKA qx:
    my $html = `echo $plaintext | txt2html --extract`; # -- or -- my $html = qx(echo $plaintext | txt2html --extract); # where the ()s can be any seperator you can use on qw(), etc.
    However, you might find it better to do this sort of thing with IPC::Open2, especially if $plaintext is going to contain stuff that you don't want to pass to the shell:
    use IPC::Open2; my($read_fh,$write_fh); my $pid = open2($read_fh,$write_fh,"txt2html","--extract"); print $write_fh $plaintext; close $write_fh; my $html = do { local $/; <$read_fh> }; # or you can use a while(<$read_fh>) { ... } loop. waitpid $pid, 0; # don't leave "zombies"
Re: Obtaining the output of a system call, not the return status
by gav^ (Curate) on Feb 09, 2002 at 22:13 UTC
    You could just use the module HTML::FromText which makes things simpler (and more portable!).
    use HTML::FromText; print text2html($text, urls => 1, paras => 1, headings => 1);

    gav^

Re: Obtaining the output of a system call, not the return status
by Zaxo (Archbishop) on Feb 09, 2002 at 21:57 UTC

    Backticks or qx will do that:

    my $html = qx(echo $plaintext | txt2html --extract);

    Update: (remove foot from mouth) ++japhy is correct. In detail: echo would be ok (it's a shell builtin), but $plaintext = quotemeta $plaintext; needs to be said first. The absolute path to txt2html should be used. If $plaintext comes from a file, input redirection to txt2html would be better than reading it first and echoing.

    After Compline,
    Zaxo

      Super unsafe. Not to mention, I'd think it's hard to extract HTML from a single line of text -- meaning, $plaintext is probably several lines long. It would need to be escaped and whatnot.

      _____________________________________________________
      Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
      s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Obtaining the output of a system call, not the return status
by japhy (Canon) on Feb 09, 2002 at 21:56 UTC
    system() is documented as returning the system return code (success/failure). And you can't echo tons of lines. It's not that easy.

    I'm silly. Disregard my code. wog showed me the error of my ways.

    Use pipes instead:

    # interact with txt2html pipe(READ, WRITE); open WRITE, "| txt2html --extract"; print WRITE $plaintext; close WRITE; { local $/; $html = <READ>; } close READ;
    Use IPC::Open2.

    _____________________________________________________
    Jeff[japhy]Pinyan: Perl, regex, and perl hacker.
    s++=END;++y(;-P)}y js++=;shajsj<++y(p-q)}?print:??;

Re: Obtaining the output of a system call, not the return status
by beebware (Pilgrim) on Feb 09, 2002 at 23:16 UTC
    Couldn't you either include the txt2html code in your script or just do a 'use path/to/txt2html;' and call it that way?

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (2)
As of 2024-04-25 20:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found