On 09/05/2014 21:41, Harry Putnam wrote:
I guess I need a mnemonic device to trick myself into remembering
which way the pipe with dash symbol goes.

   And I suppose I should apologize for the cry baby rant in
    advance:

Even now I'm flopping around trying to remember...

I've written scripts involving sending mail with sendmail from perl.
Or capturing the output of rsync etc.

I used a piped open like this:

   open my $ch, '|-', "$sendm" or die
                      "Can't open $sendm: $!";

   while (<$sch>){
     print $ch bleh;
     print $ch blah;
  }

Or perhaps it was like this:

   open my $ch, '-|', "$sendm" or die
    [...]

It may have been either way.  I don't have it handy.  But one way,
string $bleh and $blah get sent as mail.

My question is not so much which was right for that usage (although
that would be helpful too), but how to know at a moments thought, how
to setup the pipe for that usage.

One way might inject mail into sendmail with a while loop, the other
might allow one to track (or search etc.) the output of some command
in a while loop.

One way fails and can cause some confusion in tracking the
problem.

So the need here (beyond importing someone elses' brain) is how
to remember which way.

There must be something that would make even an intellectually
challenged sort like me ... remember.

Yes, yes, I know I can test and get it right.... which I've done
repeatedly. Here, I'm looking for a way to fix it in my mind.

As soon as I think I've got it right by thinking:

   ok, left side is into the command
      right side is out.

Hi Harry

Information flow is left-to-right, the same as when you use pipes to
chain a string of commands on the terminal.

I sympathise, but if you remember the history - first of all that larry
Wall wrote the language from the point of view of a linguist - and
secondly that

    open my $ch, '| command p1 p2'

means to open a pipe to `command p1 p2`. This format happens to use the
shell to do its stuff.

This was improved in Perl 5 version 6-ish to make this work with the
three-parameter form of `open`, like this

    open my $ch, '|-', 'command', 'p1', 'p2'

So the hyphen, or dash, is an *it* pronoun like `$_`, and this syntax
opens a pipe to *it*, where *it* is the given command and parameters.

The same applies to using a mode of `-|`, where the *it* is still the
command and parameters, but it is on the left of the pipe, so the Perl
program is opening a pipe *from* the command.

I hope that helps.

Rob



---
This email is free from viruses and malware because avast! Antivirus protection 
is active.
http://www.avast.com


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to