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

Hi Lads,
I am simply trying to get a longer unquoted string into a variable. S.th. like this which doesn't work (I tried to force the idea from: print <<"EOF"< which doesnt quite do what I want).
$x=readstring(); sub readstring { $x <<"EOF"; here is my unquoted multiline string EOF return $x; }
Thanks a lot hasenbraten

Replies are listed 'Best First'.
Re: <<EOF syntax for reading into string
by eserte (Deacon) on Apr 19, 2004 at 17:28 UTC
    You missed a "=". Try $x = <<"EOF";
Re: <<EOF syntax for reading into string
by Zaxo (Archbishop) on Apr 19, 2004 at 17:59 UTC

    I notice that you have global $x appearing again in the code of readstring(). That will replace $x whenever readstring is called, whether or not you assign to it. Declaring the lexical my $x = <<EOF; in the first line of the sub will fix that.

    You can do without temporary variables entirely with the following oddity,

    sub readstring () { <<'EOF'; here is my unquoted multiline string EOF }
    The empty prototype is a hint to the compiler that readstring returns a constant value and can be evaluated during compilation. I single-quoted the heredoc end tag to emphasize that there is no variable interpolation to happen. ("unquoted" is only true in a typographic sense - heredocs can employ any of perl's flavors of quoting)

    After Compline,
    Zaxo

Re: <<EOF syntax for reading into string
by pbeckingham (Parson) on Apr 19, 2004 at 17:38 UTC

    That's called a "here document". Please note that you have other options for unquoted, multiline strings, namely the uninterpolated q and the interpolated qq.

    sub readstring { qq{ here is my unquoted multiline string }; }