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

In Shell you can run a system command as
$system_command <<EOF EOF
And have it pass standard in.

How do you do the same in Perl?

update (broquaint): added formatting

Title edit by tye

Replies are listed 'Best First'.
Re: Shell v Perl
by jasonk (Parson) on Apr 10, 2003 at 16:47 UTC

    You can do the same 'here document' style thing in perl...

    print <<"EOF"; EOF # to pass the here document as stdin to some system # command you could do this: open(CMD,"| command"); print CMD <<"END"; stuff.... END close(CMD);

    We're not surrounded, we're in a target-rich environment!
      It's worthwhile to note that in your example, the quotes around EOF are optional. Also, that there can be no whitespace seperating the << from the ending token. It's very easy to accidentally get that wrong, e.g.
      print << FOO; # WRONG!
      print <<FOO; # right
      
        You can doesn't mean you should. Perl6 will do away with that, thankfully.

        Makeshifts last the longest.

Re: Shell v Perl, here-doc as stdin
by dga (Hermit) on Apr 11, 2003 at 16:03 UTC

    I am not sure what you are asking exactly but here are a few possibilities.

    As mentioned the here-document syntax is supported inside perl programs for data.

    If you are wanting to send some input to 'system_command' then this could look really similar to a shell.

    open FH, "|system_command"; print FH <<EOF EOF

    The | in front of the command tells perl to send anything printed onto that filehandle along to the program. Also if you have a newer perl you can use the 3 element open and lexical filehandles so the open could look more like:

    open my $fh, "|-", "system_command";

    Of course, in perl, TIMTOWTDI, so you can also have a variable with content to be passed along.

    open FH, "|system_command"; my $stuff="some things to pass along to system command\n"; print FH $stuff;

    A perl script can be passed input via a here-doc from the shell just like any other command can recieve this data.

    I hope one of these addresses the question that you had.

Re: Shell v Perl
by Coplan (Pilgrim) on Apr 10, 2003 at 16:47 UTC
    Sorry, i seem to have misunderstood the question.

    But yeah, jasonk is right. But if you want to print until END, like in his first example, I have one caveat: Make sure there is a definate empty line with nothing on it before the END and that the E is the VERY first character on the line.. Sometimes it'll act wierd and not find it as it might have a space or what-not before it.

    --Coplan

      If you have an empty line before the END, that empty line will get printed, which may not be what you want. You are right that it must match the first tag exactly though, if you say print <<"END";, the ending line must have ONLY 'END' in it, if you have extra spaces (even ones you may not be able to see at the end of the line), it won't work. This advice is doubly serious when you write things like this:

      sub print_stuff { print <<"END"; some stuff some more stuff END }

      This example doesn't work because your starting tag is "END" but your ending tag is " END". Leaving a blank line before the END isn't something I'm planning to start doing though, if you are using the here-docs right, you don't need it, if you need to always leave a blank line, then you are probably doing something wrong.


      We're not surrounded, we're in a target-rich environment!
        That brings up a good question. Would you be able to use regexp in a situation like that?

        $buffer = <<"/\s*END/";

        I don't have perl here at work, so I can't test it.

        --Coplan