Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Perl and Shell (text conversion)

by Bryan882 (Novice)
on Jul 02, 2018 at 14:29 UTC ( [id://1217745]=perlquestion: print w/replies, xml ) Need Help??

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

Hello Monks, Recently I have been working on a way to convert RTF to text and have found an appropriate solution. However when I use perl -e to put this snippet of code into a shell script, it throws an error. This script will take its values from the command line of one shell script and pass it into another shell script where this perl code is sitting. Like I said it works fine in a small perl script but I am wondering why this error is happening when i try to use it within a shell script.
function fConvert { perl -e ' use strict; use warnings; use RTF::TEXT::Converter; my $result; my $object = new RTF::TEXT::Converter(output => \$result); if (@ARGV) { foreach my $filename (@ARGV) { $object->parse_stream($filename); print $result; $result = ''; } } ' }

Replies are listed 'Best First'.
Re: Perl and Shell (Delimiter)
by LanX (Saint) on Jul 02, 2018 at 14:49 UTC

    without trying it out, this looks plain wrong

     $result = '';

    because it breaks your evaled string delimiter

    perl -e ' ...

    maybe try

      $result = q();

    instead.

    q/STRING/ is an alternative syntax for '' , see perlop#Quote-and-Quote-like-Operators for details

    Cheers Rolf
    (addicted to the Perl Programming Language :)
    Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

    update

    PS: please note that bash has here-docs too, maybe a more flexible way to safely integrate literal Perl code.

Re: Perl and Shell (text conversion) (updated)
by haukex (Archbishop) on Jul 02, 2018 at 16:54 UTC

    Personally, I'd do it the other way around, that is, just write the whole thing in Perl... but ok, embedding Perl into a longer shell script is ok too, I guess... ;-)

    I'm assuming you're using bash, and you don't seem to need STDIN in your Perl script, so I'd probably use a here doc, like in the following. I've also made some tweaks, plus you didn't seem to be passing the Perl script any filenames. (Note: I tested without RTF::TEXT::Converter.)

    function fConvert { perl -wMstrict -- - $@ <<'ENDPERL' use RTF::TEXT::Converter; my $result = ''; my $object = RTF::TEXT::Converter->new( output => \$result ); foreach my $filename (@ARGV) { $object->parse_stream($filename); print $result; $result = ''; } ENDPERL }

    Update: Or, just make it a oneliner ;-) (the module just prints its output by default)

    perl -wMstrict -MRTF::TEXT::Converter -00ne 'RTF::TEXT::Converter->new->parse_string($_)'

    Update 2: I guess I should mention why I'd suggest a heredoc: when the delimiter is quoted (as in 'ENDPERL'), you don't have to worry about special characters within the heredoc.

Re: Perl and Shell (text conversion)
by Your Mother (Archbishop) on Jul 02, 2018 at 17:19 UTC

    On a tangent: RTF::TEXT::Converter is quite broken and has been abandoned for a long time; it will likely never be fixed; Unicode/UTF-8 incapable and has some surprise bugs like omitting lines that contain just a zero.

    I would recommend checking out Pandoc. I haven't used it but have been planning to get to it soon and convert a bunch of my own code if it's as good as I expect.

    Update: typo fixed.

      Kudos for mentioning Pandoc.

      But I'm wondering why it's necessary to use an intermediate Perl wrapper when starting a Haskell tool from Bash?

      Cheers Rolf
      (addicted to the Perl Programming Language :)
      Wikisyntax for the Monastery FootballPerl is like chess, only without the dice

        Surely not necessary. Just a nice normalizer, I suppose. Can't even build pandoc on my current work setup so... :(

Re: Perl and Shell (text conversion)
by jeffenstein (Hermit) on Jul 02, 2018 at 15:14 UTC

    It looks like LanX has the correct answer. After shell quoting, that line would be: $result = ; which surely isn't what you intended.

    Also, you check @ARGV in the embedded script, but don't pass any arguments to it. You should add "$@" after the last single quote to pass the arguments to the function. i.e.:

    function fConvert { perl -e ' ...<snip>... ' "$@" }
Re: Perl and Shell (text conversion)
by Bryan882 (Novice) on Jul 03, 2018 at 07:29 UTC
    Firstly, thank you to the contributors for their responses and insights, especially links to relevant docs!, it will definitely be useful in the future. You are indeed correct and I had missed passing any arguments to the function, which naturally wouldn't do anything. I had read that pandoc is a better/safer method and I think I will suggest this instead of the potentially unreliable conversion it is doing now. Thanks Monks!

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 19:28 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found