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

This node falls below the community's threshold of quality. You may see it by logging in.

Replies are listed 'Best First'.
Re: $foo = << "END"
by marto (Cardinal) on Mar 05, 2012 at 13:44 UTC

    "Hi Everybody, Does anyone know what the following code do."

    What did it do when you tried it? I know it'll throw an error.

    "I would be very thankful if you could suggest me an alternative for the same."

    You want an alternative for something but you don't understand what it's supposed to do? Why not find out what it is and what it does first. It's called a Here_document. Consider the following example:

    #!/usr/bin/perl use strict; use warnings; my $foo = << "END"; Blah blah blah more blah END print $foo;

    Output:

    Blah blah blah more blah
      Thank you morto. I have updated my thread. Please check if that could help.

        I previously advised you to read and understand How do I post a question effectively? which mentions updating your posts. Generally it's very helpful to add a line to point out what has been updated, e.g.

        My first question goes here....

        Update: I've thought of something else

        Incidentally the code to post this is as follows:

        <p>My first question goes here....</p> <p><b>Update</b>: I've thought of something else</p>

        Again, going back to advice given in a previous thread, you need to ask questions properly. Tell people what you're actually trying to do.

        It seems you're trying to build up a SQL statement based on a template query and some values stored in variables. I'd advise against using here docs to build up SQL queries based on appending values to strings. See Placeholders and bind variables and SQL_injection.

Re: $foo = << "END"
by aaron_baugher (Curate) on Mar 05, 2012 at 13:40 UTC

    It throws an error, because that's an incomplete statement. What you have there is most of the first line of a heredoc, which when completed looks like this:

    my $foo = << "END"; This is some text that will be placed in the scalar \$foo. Variables like $this and $that will be interpolated. END print $foo;

    A couple of gotchas on using heredocs: note that the opening line has a closing semicolon and the closing one does not. (I think PHP's heredoc works the other way around.) Also, the closing delimiter word must not have any whitespace on the line before it, unless you go to some trouble to quote the same whitespace as part of the opening delimiter.

    Aaron B.
    My Woefully Neglected Blog, where I occasionally mention Perl.

      Ya thats right but we could have directly written as
      my $foo =" This is some text that will be placed in the scalar \$foo. Variables like $this and $that will be interpolated." print $foo;
      Does using  << "END" have any specific advantage?
        You can include double quotes without escaping.

        Except that your example is not the same. Yours has a leading space that mine doesn't have, and mine has a trailing newline that yours doesn't. Those are things that can be more easily spotted with a heredoc.

        On the other hand, some things can be tricky to do with a heredoc, like ending a multi-line string without a newline. So each method has its own best uses.

        Aaron B.
        My Woefully Neglected Blog, where I occasionally mention Perl.