From a03794e5a74f45233831eadbd84c60838fdc0d0a Mon Sep 17 00:00:00 2001 From: Steve Hancock Date: Sat, 19 Mar 2022 12:20:47 -0700 Subject: [PATCH] treat an obj with print method as a file in -eos logic --- docs/eos_flag.md | 13 ++++++------ lib/Perl/Tidy.pm | 52 ++++++++++++++++++++++++++++++++++++++---------- 2 files changed, 48 insertions(+), 17 deletions(-) diff --git a/docs/eos_flag.md b/docs/eos_flag.md index d32396ac..2e38e0af 100644 --- a/docs/eos_flag.md +++ b/docs/eos_flag.md @@ -140,10 +140,13 @@ it is processed by perltidy at three well-defined points: - at the intermediate stage as it is processed - when it is leaves to its destination -Since 'C' mode only has meaning within Perl scripts, a rule is that -outside of the realm of Perl the text must exist in 'B' mode. So the source -can only be in 'C' mode if it arrives by a call from another Perl program, and -the destination can only be in 'C' mode if the destination is a Perl program. +Since 'C' mode only has meaning within Perl scripts, a rule is that outside of +the realm of Perl the text must exist in 'B' mode. So the source can only be +in 'C' mode if it arrives by a call from another Perl program, and the +destination can only be in 'C' mode if the destination is a Perl program. If +the destination is a file, or object with a print method, then it will be +assumed to be ending its existance as a Perl string and will be placed in an +end state which is 'B' mode. Let us make a list of all possible sets of modes to be sure that all cases are covered. If each of the three states could be in 'B' or 'C' mode then we @@ -181,5 +184,3 @@ Referring back to the full table, note that case 7, the C->C->B route, is an unusual but possible situation involving a source string being sent directly to a file. It is the only situation in which perltidy does an encoding without having done a corresponding previous decoding. - - diff --git a/lib/Perl/Tidy.pm b/lib/Perl/Tidy.pm index 8eb5d074..01edc746 100644 --- a/lib/Perl/Tidy.pm +++ b/lib/Perl/Tidy.pm @@ -196,7 +196,7 @@ sub streamhandle { $New = sub { undef }; confess <{'encode-output-strings'} && $decoded_input_as; + } + + # An object with a print method will use file encoding rules + elsif ( $ref_destination_stream->can('print') ) { + $encode_destination_buffer = $is_encoded_data; + } + else { + confess <new( @@ -1796,7 +1814,7 @@ EOM # source, it encodes before returning. $rstatus->{'output_encoded_as'} = ''; - if ( $rOpts->{'encode-output-strings'} && $decoded_input_as ) { + if ($encode_destination_buffer) { my $encoded_buffer; eval { $encoded_buffer = @@ -1815,13 +1833,25 @@ EOM } } - # Final string storage + # Send data for SCALAR, ARRAY & OBJ refs to its final destination if ( ref($destination_stream) eq 'SCALAR' ) { ${$destination_stream} = $destination_buffer; } else { my @lines = split /^/, $destination_buffer; - @{$destination_stream} = @lines; + if ( ref($destination_stream) eq 'ARRAY' ) { + @{$destination_stream} = @lines; + } + + # destination is object with print method + else { + foreach (@lines) { + $destination_stream->print($_); + } + if ( $ref_destination_stream->can('close') ) { + $destination_stream->close(); + } + } } } else { -- 2.39.5