in reply to Newbie: Pipe/STDIN Clarification

You don't need to touch STDOUT actually, and you can simplify the file read:
my $stringified = ''; { #Go into local slurp mode .. local $/=undef; open FH, "< ./demo.mail"; $stringified = <FH>; close FH; open (CMD_OUT, "/usr/lib/dovecot/deliver -f +test\@somedomain.com -d test\@mailtask.dom $stringified"|); my $test = <CMD_OUT>; print $test; close CMD_OUT; }


The 'local $/=undef' bit turns off the normal record separator (newline by default). So when the filehandle is read the data is slurped in as one record. The point of enclosing in {}'s is to localise the effect - outside the {}'s the default separator will be used.

You could also use the back-tick notation to run the shell command:
my $test = `/usr/lib/dovecot/deliver -f +test\@somedomain.com -d test\@mailtask.dom $stringified`;
There are other ways of course. This is perl after all.

Replies are listed 'Best First'.
Re^2: Newbie: Pipe/STDIN Clarification
by graff (Chancellor) on Nov 05, 2011 at 13:57 UTC
    Two problems: (1) your "local slurp" isn't local enough, and (2) I think you misread the OP's intention -- mpapet wants to write to the "deliver" process, not read from it. (And BTW, you should indent, and be careful about how line-wrapping in code blocks is handled here at PM.)
    my $stringified; { local $/; # defaults to undef open my $fh, '<', './demo.mail'; # 3-arg open is nice $stringified = <$fh>; } # input's done, end-of-block closes the file and reverts to normal i/o my @cmd = qw( /usr/lib/dovecot/deliver -f test@somedomain.com -d test@mailtask.dom ); open( CMD_OUT, '|-', @cmd ) or die $!; print CMD_OUT $stringified; close CMD_OUT;
    (Updated to use lexically-scoped file handle in the localized input block -- that's what will cause the file to be closed on leaving the block.)
      Ah - but as you say I misread the OP's intention. The localised slurp also encompassed the (incorrectly) assumed read from the command - which I assumed would come with line feeds.

      Thanks for the tip on indents.
Re^2: Newbie: Pipe/STDIN Clarification
by mpapet (Novice) on Nov 05, 2011 at 13:54 UTC
    Thanks for both replies.

    If I try to pass the contents of the variable, it errors out. "Fatal: Unknown argument: From" That's the first line of my demo.mail file. I think it's safe to assume it is reading the string as a file name only.

    That means one more step of writing the email out to disk using the lda program as-is.

      It's unfortunate that the first two replies to your OP (from Anonymous Monk and mrstlee) were wrong, and/or didn't quite get your question. Of course there's no guarantee that my replies are correct, either...