@@ -2875,85 +2875,151 @@ must use an C<eval()>:
2875
2875
2876
2876
eval "tr/$oldlist/$newlist/, 1" or die $@;
2877
2877
2878
- =item C<< <<I<EOF> >>
2879
- X<here-doc> X<heredoc> X<here-document> X<<< << >>>
2878
+ =back
2879
+
2880
+ =head3 Here-docs
2881
+ X<here-doc> X<here-docs> X<heredoc> X<here-document> X<<< << >>>
2882
+
2883
+ =over 4
2884
+
2885
+ =item C<< <<I<EOT> >>
2886
+
2887
+ =item C<< <<~I<EOT> >>
2880
2888
2881
2889
A line-oriented form of quoting is based on the shell "here-document"
2882
2890
syntax. Following a C<< << >> you specify a string to terminate
2883
2891
the quoted material, and all lines following the current line down to
2884
2892
the terminating string are the value of the item.
2885
2893
2886
- Prefixing the terminating string with a C<~> specifies that you
2887
- want to use L</Indented Here-docs> (see below).
2894
+ An example is
2888
2895
2889
- The terminating string may be either an identifier (a word), or some
2890
- quoted text. An unquoted identifier works like double quotes.
2891
- There may not be a space between the C<< << >> and the identifier,
2892
- unless the identifier is explicitly quoted. The terminating string
2893
- must appear by itself (unquoted and with no surrounding whitespace)
2894
- on the terminating line.
2896
+ my $endng = <<WHIMPER;
2897
+ This is the way the text ends.
2898
+ This is the way the text ends.
2899
+ Not with a bang, but with a
2900
+ WHIMPER
2895
2901
2896
- If the terminating string is quoted, the type of quotes used determine
2897
- the treatment of the text.
2902
+ In this case, the terminator is an identifier, the word "WHIMPER". Most
2903
+ usually, people capitalize the identifier, just so it stands out, but
2904
+ this is just a convention that isn't necessary.
2898
2905
2899
- =over 4
2906
+ The terminator may be enclosed in quotes, as detailed below, but without
2907
+ them, the text of the here-doc acts exactly as if it were double-quoted.
2900
2908
2901
- =item Double Quotes
2909
+ my $person = 'John';
2902
2910
2903
- Double quotes indicate that the text will be interpolated using exactly
2904
- the same rules as normal double quoted strings.
2911
+ print uc <<EOT;
2912
+ Hello, $person!
2913
+ And the text goes on.
2914
+ EOT
2905
2915
2906
- print <<EOF;
2907
- The price is $Price.
2908
- EOF
2916
+ This yields:
2909
2917
2910
- print << "EOF"; # same as above
2911
- The price is $Price.
2912
- EOF
2918
+ HELLO, JOHN!
2919
+ AND THE TEXT GOES ON.
2913
2920
2921
+ The parentheses in the C<uc> function call don't have to be omitted:
2914
2922
2915
- =item Single Quotes
2923
+ print uc(<<EOT);
2924
+ Hello, $person!
2925
+ And the text goes on.
2926
+ EOT
2916
2927
2917
- Single quotes indicate the text is to be treated literally with no
2918
- interpolation of its content. This is similar to single quoted
2919
- strings except that backslashes have no special meaning, with C<\\>
2920
- being treated as two backslashes and not one as they would in every
2921
- other quoting construct.
2928
+ HELLO, JOHN!
2929
+ AND THE TEXT GOES ON.
2922
2930
2923
- Just as in the shell, a backslashed bareword following the C<<< << >>>
2924
- means the same thing as a single-quoted string does:
2931
+ And you can intermix a here-document with other things:
2925
2932
2926
- $cost = <<'VISTA'; # hasta la ...
2927
- That'll be $10 please, ma'am.
2928
- VISTA
2933
+ print <<EOT, "Followed by the next argument\n";
2934
+ Hello, $person!
2935
+ And the text goes on.
2936
+ EOT
2929
2937
2930
- $cost = <<\VISTA; # Same thing !
2931
- That'll be $10 please, ma'am .
2932
- VISTA
2938
+ Hello, John !
2939
+ And the text goes on .
2940
+ Followed by the next argument
2933
2941
2934
- This is the only form of quoting in perl where there is no need
2935
- to worry about escaping content, something that code generators
2936
- can and do make good use of.
2942
+ And you can have multiple here-documents:
2937
2943
2938
- =item Backticks
2944
+ print <<EOT1, <<EOT2;
2945
+ Hello, $person!
2946
+ And the text goes on.
2947
+ EOT1
2948
+ Followed by the next argument
2949
+ EOT2
2939
2950
2940
- The content of the here doc is treated just as it would be if the
2941
- string were embedded in backticks. Thus the content is interpolated
2942
- as though it were double quoted and then executed via the shell, with
2943
- the results of the execution returned.
2951
+ Hello, John!
2952
+ And the text goes on.
2953
+ Followed by the next argument
2944
2954
2945
- print << `EOC`; # execute command and get results
2946
- echo hi there
2947
- EOC
2955
+ The terminator doesn't have to be a single word; it may also be some
2956
+ quoted text:
2948
2957
2949
- =back
2958
+ my $pagliaci = << "La Commedia e finita!";
2959
+ A troupe comes to town to perform a play, a comedy. The lead actress
2960
+ and lead actor are in an unhappy marriage. On stage, he stabs her
2961
+ for real; then he stabs her lover who has rushed from the audience to
2962
+ defend her. Both die.
2963
+ La Commedia e finita!
2950
2964
2951
- =over 4
2965
+ There may not be a space between the C<< << >> and the identifier unless
2966
+ the terminator is quoted, as demonstrated in the example just above.
2967
+ Quoting rules for the terminator are unrelated to Perl's quoting rules.
2968
+ Only C<"">, C<''>, and C<``> can be used to quote it, NOT C<q()>,
2969
+ C<qq()>, and the like. The only interpolation is for backslashing the
2970
+ quoting character:
2971
+
2972
+ print << "abc\"def";
2973
+ testing...
2974
+ abc"def
2975
+
2976
+ The terminating string must appear by itself (unquoted and with no
2977
+ surrounding whitespace) on the terminating line. Also, it cannot span
2978
+ multiple lines. The general rule is that the identifier must be a
2979
+ string literal. Stick with that, and you should be safe.
2980
+
2981
+ Don't forget that you have to put a semicolon on the end to finish the
2982
+ statement, as Perl doesn't know you're not going to try to do this:
2983
+
2984
+ print <<ABC
2985
+ 10
2986
+ ABC
2987
+ + 20;
2988
+
2989
+ which prints C<30> and no line terminator.
2990
+
2991
+ If you want your here-doc to not have a line terminator on the final
2992
+ line, use C<chomp()>.
2993
+
2994
+ chomp($string = <<'END');
2995
+ This is the first line.
2996
+ This second line won't end in a \n.
2997
+ END
2998
+
2999
+ If you use a here-doc within a delimited construct, such as in C<s///eg>,
3000
+ the quoted material must still come on the line following the
3001
+ C<<< <<TERMINATOR >>> marker, which means it may be inside the delimited
3002
+ construct:
3003
+
3004
+ s/this/<<E . 'that'
3005
+ the other
3006
+ E
3007
+ . 'more '/eg;
2952
3008
2953
- =item Indented Here-docs
3009
+ (It works this way as of Perl 5.18. Historically, it was inconsistent, and
3010
+ you would have to write
3011
+
3012
+ s/this/<<E . 'that'
3013
+ . 'more '/eg;
3014
+ the other
3015
+ E
2954
3016
2955
- The here-doc modifier C<~> allows you to indent your here-docs to make
2956
- the code more readable:
3017
+ outside of string evals.)
3018
+
3019
+ A problem with the Here-doc syntax given so far is that it must be at the
3020
+ left margin of your program, messing up the indentation. Starting in
3021
+ Perl v5.26, the tilde C<~> modifier allows you to indent your here-docs
3022
+ to make the code more readable.
2957
3023
2958
3024
if ($some_var) {
2959
3025
print <<~EOF;
@@ -2983,97 +3049,90 @@ remain tabs and are not expanded.
2983
3049
Additional beginning whitespace (beyond what preceded the
2984
3050
delimiter) will be preserved:
2985
3051
2986
- print <<~EOF;
2987
- This text is not indented
2988
- This text is indented with two spaces
2989
- This text is indented with two tabs
2990
- EOF
3052
+ print <<~EOF;
3053
+ This text is not indented
3054
+ This text is indented with two spaces
3055
+ This line is indented with two tabs, though those may
3056
+ have been converted to spaces by various filters by
3057
+ the time you read this.
3058
+ EOF
2991
3059
2992
- Finally, the modifier may be used with all of the forms
2993
- mentioned above:
3060
+ =back
2994
3061
2995
- <<~\EOF;
2996
- <<~'EOF'
2997
- <<~"EOF"
2998
- <<~`EOF`
3062
+ =head4 Quoting the delimiter
2999
3063
3000
- And whitespace may be used between the C<~> and quoted delimiters:
3064
+ As mentioned above, the terminating string may be quoted. There are
3065
+ three types of quoting possible. The type used determines the treatment
3066
+ of the text.
3001
3067
3002
- <<~ 'EOF'; # ... "EOF", `EOF`
3068
+ =over 4
3003
3069
3004
- =back
3070
+ =item Double Quotes
3005
3071
3006
- It is possible to stack multiple here-docs in a row:
3072
+ Double quotes surrounding the terminating word or string behave as if
3073
+ no quotes were there, namely the text will be interpolated using exactly
3074
+ the same rules as normal double quoted strings, as in all the examples
3075
+ above. So
3007
3076
3008
- print <<"foo", <<"bar"; # you can stack them
3009
- I said foo.
3010
- foo
3011
- I said bar.
3012
- bar
3077
+ my $person = 'John';
3013
3078
3014
- myfunc(<< "THIS", 23, <<'THAT');
3015
- Here's a line
3016
- or two.
3017
- THIS
3018
- and here's another.
3019
- THAT
3079
+ print uc << "EOT";
3080
+ Hello, $person!
3081
+ And the text goes on.
3082
+ EOT
3020
3083
3021
- Just don't forget that you have to put a semicolon on the end
3022
- to finish the statement, as Perl doesn't know you're not going to
3023
- try to do this:
3084
+ yields:
3024
3085
3025
- print <<ABC
3026
- 179231
3027
- ABC
3028
- + 20;
3086
+ HELLO, JOHN!
3087
+ AND THE TEXT GOES ON.
3029
3088
3030
- If you want to remove the line terminator from your here-docs,
3031
- use C<chomp()>.
3089
+ which is the same result as without quotes.
3032
3090
3033
- chomp($string = <<'END');
3034
- This is a string.
3035
- END
3091
+ =item Single Quotes
3036
3092
3037
- If you want your here-docs to be indented with the rest of the code,
3038
- use the C<<< <<~FOO >>> construct described under L</Indented Here-docs>:
3093
+ If instead, single quotes are used, the text is treated literally, with
3094
+ no interpolation of its content.
3039
3095
3040
- $quote = <<~'FINIS';
3041
- The Road goes ever on and on,
3042
- down from the door where it began.
3043
- FINIS
3096
+ my $person = 'John';
3097
+ print uc <<'EOT';
3098
+ Hello, $person!
3099
+ And the text goes on.
3100
+ EOT
3044
3101
3045
- If you use a here-doc within a delimited construct, such as in C<s///eg>,
3046
- the quoted material must still come on the line following the
3047
- C<<< <<FOO >>> marker, which means it may be inside the delimited
3048
- construct:
3102
+ HELLO, $PERSON!
3103
+ AND THE TEXT GOES ON.
3049
3104
3050
- s/this/<<E . 'that'
3051
- the other
3052
- E
3053
- . 'more '/eg;
3105
+ The difference between a single-quoted here-doc and a single-quoted
3106
+ string is that backslashes have no special meaning in a here-doc, with
3107
+ C<\\> being treated as two backslashes and not one as they would in
3108
+ every other quoting construct.
3054
3109
3055
- It works this way as of Perl 5.18. Historically, it was inconsistent, and
3056
- you would have to write
3110
+ Just as in the shell, a backslashed bareword following the C<<< << >>>
3111
+ means the same thing as a single-quoted string does:
3057
3112
3058
- s/this/<<E . 'that'
3059
- . 'more '/eg;
3060
- the other
3061
- E
3113
+ $cost = <<'VISTA'; # hasta la ...
3114
+ That'll be $10 please, ma'am.
3115
+ VISTA
3062
3116
3063
- outside of string evals.
3117
+ $cost = <<\VISTA; # Same thing!
3118
+ That'll be $10 please, ma'am.
3119
+ VISTA
3064
3120
3065
- Additionally, quoting rules for the end-of-string identifier are
3066
- unrelated to Perl's quoting rules. C<q()>, C<qq()>, and the like are not
3067
- supported in place of C<''> and C<"">, and the only interpolation is for
3068
- backslashing the quoting character:
3121
+ These two forms are the only ways of quoting in Perl where there is no
3122
+ need to worry about escaping content, something that code generators can
3123
+ and do make good use of.
3069
3124
3070
- print << "abc\"def";
3071
- testing...
3072
- abc"def
3125
+ =item Backticks
3073
3126
3074
- Finally, quoted strings cannot span multiple lines. The general rule is
3075
- that the identifier must be a string literal. Stick with that, and you
3076
- should be safe.
3127
+ Finally, if instead backticks are used to quote the terminating string,
3128
+ the content of the here doc is treated just as it would be if it were a
3129
+ string embedded in backticks. Thus the content is interpolated as
3130
+ though it were double quoted and then executed via the shell, with the
3131
+ results of the execution returned.
3132
+
3133
+ print << `EOC`; # execute command and get results
3134
+ echo hi there
3135
+ EOC
3077
3136
3078
3137
=back
3079
3138
@@ -3892,6 +3951,10 @@ only to prevent breaking any pre-existing links to it from outside.
3892
3951
3893
3952
This section has been replaced by L</Simpler Quote-Like Operators>
3894
3953
3954
+ =head2 Indented Here-docs
3955
+
3956
+ This section has been merged into by L</Here-docs>
3957
+
3895
3958
=head1 APPENDIX
3896
3959
3897
3960
=head2 List of Extra Paired Delimiters
0 commit comments