in reply to Shell v Perl, here-doc as stdin

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

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

    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

        Short answer: No:

        #!/usr/bin/env perl eval { sub test { my $buffer =<<"/\s*END/"; Eins Zwei END return $buffer; } print test(); }; if($@) { print $@; } print "\ndone\n"; __END__ Can't find string terminator "/\s*END/" anywhere before EOF at test.pl + line 3.

        Longer answer in perlfaq4 as answer to the question "Why don't my <<HERE documents work?".

        regards,
        tomte


        Hlade's Law:

        If you have a difficult task, give it to a lazy person --
        they will find an easier way to do it.


        Corrcted the code to look like zthe code I actually used ;)