Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > STDOUT beginner problem

Reply
Thread Tools

STDOUT beginner problem

 
 
mat.krawczyk@gmail.com
Guest
Posts: n/a
 
      11-26-2013
Hello,

I would like to write simple script for emails decoding. My problem is connected with input and output of an external program. I would like to use html2text converter:

open(HTML2TEXT, "| /usr/bin/html2text ") || die "html2text failed: $!\n";
$text = print HTML2TEXT $html;
close HTML2TEXT;
print $text;

but $text is empty and output is directed to STDOUT.

I will be grateful for any help..

Mateusz Krawczyk
 
Reply With Quote
 
 
 
 
Rainer Weikusat
Guest
Posts: n/a
 
      11-26-2013
http://www.velocityreviews.com/forums/(E-Mail Removed) writes:
> I would like to write simple script for emails decoding. My problem is connected with input and output of an external program. I would like to use html2text converter:
>
> open(HTML2TEXT, "| /usr/bin/html2text ") || die "html2text failed: $!\n";
> $text = print HTML2TEXT $html;
> close HTML2TEXT;
> print $text;
>
> but $text is empty and output is directed to STDOUT.


Output is to stdout because you didn't redirect it somewhere
else. Generally, the built-in 'pipe open' can't do what you want (write
data to some process and read its output back). IPC::Open2 can do that,
although using that is not as straight-forward as it seems (there's a
chance that both processes deadlock because both wait for data written
by the other). One way to deal with that is to use select and switch
between reading and writing as required. Another reasonably easy way
would be to use three processes, one which reads the output from the
external command, a 2nd which runs it and a 3rd which feeds input to it.

Example
-------
my ($in, $proc, $line, $rc);

$rc = open($proc, '-|');
if ($rc == 0) {
$rc = open($proc, '-|');
if ($rc == 0) {
#
# 3rd process: reads from input file, stdout connected
# to 2nd pipe
#
open($in, '<', '/var/log/syslog');
print $line while $line = <$in>;
exit(0);
}

#
# 2nd process: stdin redirected from 2nd pipe, stdout
# connected to 1st
#
open(STDIN, '<&', $proc);
exec('tr', '6', '^');
}

#
# original process: reads processed data from 1st pipe
#
print $line while $line = <$proc>;
 
Reply With Quote
 
 
 
 
gamo
Guest
Posts: n/a
 
      11-26-2013
El 26/11/13 18:38, (E-Mail Removed) escribió:
> Hello,
>
> I would like to write simple script for emails decoding. My problem is connected with input and output of


an external program. I would like to use html2text converter:
>
> open(HTML2TEXT, "| /usr/bin/html2text ") || die "html2text failed: $!\n";
> $text = print HTML2TEXT $html;
> close HTML2TEXT;
> print $text;
>
> but $text is empty and output is directed to STDOUT.
>
> I will be grateful for any help..
>
> Mateusz Krawczyk
>


Maybe simple backticks are what you are searching for

~$ cat test.backticks
#!/usr/bin/perl -W

use strict;

my $html = '<p>hi</p>';
my $text = `echo "$html" | /usr/bin/html2text`;
print $text, "\n";

~$ perl test.backticks
hi

~$ man perlop

(pay attention to the different ticks)

Good luck

 
Reply With Quote
 
gamo
Guest
Posts: n/a
 
      11-26-2013
El 26/11/13 23:21, Ben Morrow escribió:
> Careful with your quoting. It would probably be better to write the HTML
> to a file.


....probably, but the OP seems to not have problems with the input

> You can use qx// instead of backticks, and it's usually clearer.
>
> Ben
>


Then, he must use qx!! or some other separators

Thanks


 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      11-26-2013
Ben Morrow <(E-Mail Removed)> writes:
> Quoth gamo <(E-Mail Removed)>:


[...]

>> my $html = '<p>hi</p>';
>> my $text = `echo "$html" | /usr/bin/html2text`;

>
> Careful with your quoting. It would probably be better to write the HTML
> to a file.


Not necessary. When starting to make "Gee that looks *complicated*,
can't I sell him something else instead?" assumptions, the simple way to
do this would be to create a small shell script,

------
#!/bin/sh
printf '%s' "$1" | html2text
------

and use that like this:

------
my $html = '<html><body><em>$(echo 3)</em></body></html>';
open($h2t, '-|', '/tmp/h2t', $html);
print(<$h2t>);
------

(the reason for using printf is that echo may interpret \-escapes in its
argument).




 
Reply With Quote
 
gamo
Guest
Posts: n/a
 
      11-26-2013
El 26/11/13 23:55, Rainer Weikusat escribió:
> Ben Morrow <(E-Mail Removed)> writes:
>> Quoth gamo <(E-Mail Removed)>:

....
>> Careful with your quoting. It would probably be better to write the HTML
>> to a file.

>
> Not necessary. When starting to make "Gee that looks *complicated*,
> can't I sell him something else instead?" assumptions, the simple way to
> do this would be to create a small shell script,
>
> ------
> #!/bin/sh
> printf '%s' "$1" | html2text
> ------
>
> and use that like this:
>
> ------

....
> open($h2t, '-|', '/tmp/h2t', $html);
> print(<$h2t>);
> ------
>
> (the reason for using printf is that echo may interpret \-escapes in its
> argument).
>
>


Simplest is to read a file from html2text argument but if he wants to
use cat file | html2text or echo to, he could, because the
interpretation of escapes is disabled by default:

DESCRIPTION
Echo the STRING(s) to standard output.

-n do not output the trailing newline

-e enable interpretation of backslash escapes

-E disable interpretation of backslash escapes (default)




 
Reply With Quote
 
gamo
Guest
Posts: n/a
 
      11-27-2013
El 27/11/13 01:50, Ben Morrow escribió:
>> -E disable interpretation of backslash escapes (default)


> *My* echo(1), OTOH, recognises neither -e nor -E, and the manpage says:
>
> | The newline may also be suppressed by appending '\c' to the end of the
> | string, as is done by iBCS2 compatible systems. Note that the -n option
> | as well as the effect of '\c' are implementation-defined in IEEE Std
> | 1003.1-2001 ("POSIX.1") as amended by Cor. 1-2002. For portability, echo
> | should only be used if the first argument does not start with a hyphen
> | ('-') and does not contain any backslashes ('\'). If this is not suffi-
> | cient, printf(1) should be used.
>
> and also this:
>
> | Most shells provide a builtin echo command which tends to differ from
> | this utility in the treatment of options and backslashes. Consult the
> | builtin(1) manual page.
>
> so using echo to pass arbitrary text is not reliable.
>
> Ben
>



I'm afraid that is common to have 2 echo utilities avaible. One built-in
in the bash that does accept escapes and one in /bin/echo
who do not. It could be compared "help echo" with "man echo." My
response to the OP would be to substitute "echo" by "/bin/echo,"
as I remember it's said to do ever to enhance security when
invoquing commands.

Thanks


 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      11-27-2013
Ben Morrow <(E-Mail Removed)> writes:
> Quoth gamo <(E-Mail Removed)>:
>> El 26/11/13 23:55, Rainer Weikusat escribi:
>> > Ben Morrow <(E-Mail Removed)> writes:
>> >> Quoth gamo <(E-Mail Removed)>:

>> ...
>> >> Careful with your quoting. It would probably be better to write the HTML
>> >> to a file.
>> >
>> > Not necessary. When starting to make "Gee that looks *complicated*,
>> > can't I sell him something else instead?" assumptions, the simple way to
>> > do this would be to create a small shell script,
>> >
>> > ------
>> > #!/bin/sh
>> > printf '%s' "$1" | html2text
>> > ------
>> >
>> > and use that like this:
>> >
>> > ------

>> ...
>> > open($h2t, '-|', '/tmp/h2t', $html);
>> > print(<$h2t>);
>> > ------

>
> 'Oh, but there's no need to put that script in a file either...':
>
> open my $h2t, "-|", "/bin/sh", "-c",
> q/printf %s "$1" | html2text/, $html;
>
> ...and we end up with the sort of mess shell always turns into.
> Sometimes a temporary file is the cleanest and simplest solution.


In this particular case, the main complication is that html2text doesn't
support passing the text-to-be-processed literally as command-line
argument. And the simplest way to remedy that while avoiding issues with
'inappropriate data interpretation/ execution' is to create a shell
script which takes such an argument and passes it to html2text in the
appropriate way. This yields a new and possibly generally useful command
with more reasonable semantics. Actually, the replacement command could
be written in any programming language including Perl but for these
kinds of task, the shell is IMO the most appropriate tool.

Inline use of such a different programming language instead of creating
is new command is both messy and short-sighted.

 
Reply With Quote
 
Rainer Weikusat
Guest
Posts: n/a
 
      11-27-2013
gamo <(E-Mail Removed)> writes:
gamo <(E-Mail Removed)> writes:
> El 26/11/13 23:55, Rainer Weikusat escribió:
>> Ben Morrow <(E-Mail Removed)> writes:
>>> Quoth gamo <(E-Mail Removed)>:

> ...
>>> Careful with your quoting. It would probably be better to write the HTML
>>> to a file.

>>
>> Not necessary. When starting to make "Gee that looks *complicated*,
>> can't I sell him something else instead?" assumptions, the simple way to
>> do this would be to create a small shell script,
>>
>> ------
>> #!/bin/sh
>> printf '%s' "$1" | html2text
>> ------
>>
>> and use that like this:
>>
>> ------

> ...
>> open($h2t, '-|', '/tmp/h2t', $html);
>> print(<$h2t>);
>> ------
>>
>> (the reason for using printf is that echo may interpret \-escapes in its
>> argument).

>
> Simplest is to read a file from html2text argument but if he wants to
> use cat file | html2text or echo to, he could, because the
> interpretation of escapes is disabled by default:


That's not the problem with the backticks idea. For a
live-demonstration, create a file with the following content:

-----------
#!/usr/bin/perl
$output = `echo "$ARGV[0]" | html2text`;
print($output);
-----------

and execute that with '$(ls /)' as first argument. And the ls / could as
well have been cd; rm -rf *. And the OP wrote about processing e-mail
which is not exactly a trusted data source ...
 
Reply With Quote
 
gamo
Guest
Posts: n/a
 
      11-28-2013
El 27/11/13 16:54, Rainer Weikusat escribió:
>> open my $h2t, "-|", "/bin/sh", "-c",
>> > q/printf %s "$1" | html2text/, $html;
>> >
>> >...and we end up with the sort of mess shell always turns into.
>> >Sometimes a temporary file is the cleanest and simplest solution.

> In this particular case, the main complication is that html2text doesn't
> support passing the text-to-be-processed literally as command-line
> argument. And the simplest way to remedy that while avoiding issues with


At least my version of html2text supports input file as argument.
If that's a problem, anyway, it could be changed for "lynx -dump file,"
which is a more mature program from ISC, I think.


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
beginner Q: Kernel#puts, STDOUT, $stdout relation Andreas S Ruby 3 12-09-2006 12:39 AM
Problems redirecting STDOUT (NOT sys.stdout) to a pipe. Elad Python 0 03-19-2006 01:30 PM
copy stdout fails with permission denied when stdout is redirected brian.mabry.edwards@gmail.com Perl Misc 2 12-07-2005 10:49 PM
No Class at ALL!!! beginner/beginner question =?Utf-8?B?S3VydCBTY2hyb2VkZXI=?= ASP .Net 7 02-03-2005 02:47 PM
Tutorial for beginner/ Tutorial voor beginner Rensjuh C++ 7 09-02-2004 12:41 AM



Advertisments