You are here: Foswiki>Tasks Web>Item8422 (03 Nov 2015, GeorgeClark)Edit Attach

Item8422: HERE documents as macro parameters

pencil
Priority: Enhancement
Current State: No Action Required
Released In: n/a
Target Release: minor
Applies To: Engine
Component:
Branches:
Reported By: MichaelTempest
Waiting For:
Last Change By: GeorgeClark
Implement HereDocumentSyntaxForMacros

-- MichaelTempest - 25 Jan 2010

getting a failure in unit test for PerlPlugin (just written); testcase is:

sub test_hereDoc {
    my $this = shift;
    my $t = <<'BEDRAGONS';
%PERL{<<HERE}%
my %x = ( a=>'%TOPIC%' );
$x{a} =~ s/%/x/g;
"$x{a}\n";
HERE
X
BEDRAGONS
    $t = Foswiki::Func::expandCommonVariables($t);
    $this->assert_equals("xTOPICxX", $t);
}
I get:
Expected:'xTOPICxX'
 But got:'xTOPICx

X
'

-- CrawfordCurrie - 15 Feb 2010

The expected result for the unit test is incorrect - it should be "xTOPICx\nX\n" because the %PERL call appends a newline to its result ( "$x{a}\n"; does this) and because perl here-documents including a traling newline, so there is a newline after the X.

There is still one newline too many in the actual result, though. I'll investigate.

-- MichaelTempest - 15 Feb 2010

That example should be roughly equivalent to the following:
sub test_hereDoc {
    my $this = shift;
    my $t = <<'BEDRAGONS';
%PERL{"my %x = ( a=>'xTOPICx' ); qq/$x{a}\n/;"}%
X
BEDRAGONS
    $t = Foswiki::Func::expandCommonVariables($t);
    $this->assert_equals("xTOPICxX", $t);
}

I changed the perl bit slightly because %TOPIC% would be expanded before the %PERL handler is called. However, the point of this change is so that it is clear that there is a newline between }% and X - this is the newline that I could not account for earlier.

I think the here-document processing is behaving correctly.

-- MichaelTempest - 15 Feb 2010

The placing for the %<nop>TOPIC% parameter expansion is deliberate. %PERL code blocks do not support expanded macros. But there's still an extra newline, isn't there? As it says in HereDocumentSyntaxForMacros,
  • The newlines before and after the closing string will be ignored.
so surely the newline between x and X is extra?

-- CrawfordCurrie - 16 Feb 2010

But the X is not on the same line as the %PERL.

%PERL{<<HERE}%Z
my %x = ( a=>'%TOPIC%' );
$x{a} =~ s/%/x/g;
"$x{a}\n";
HERE
X
should expand to
xTOPICx
Z
X

The newline between x and Z comes from the perl. The newline between Z and X is the newline that follows after Z.

-- MichaelTempest - 16 Feb 2010

I agree about the newline from the perl (and I removed it in the test) but I refer you back to HereDocumentSyntaxForMacros:
  • The newline at the end of any line containing a here-document (<<HERE) will be ignored. The value starts after this newline.
Thus the newline after the Z should be ignored, surely? by these two rules,
%PERL{<<HERE}%Z
my %x = ( a=>'%TOPIC%' );
$x{a} =~ s/%/x/g;
"$x{a}";
HERE
X
should expand to
ZxTOPICxX
should it not? Similarly if there are multiple heredocs on the same line:
A%IF{"cond" then=<<ELSE else=<<ENDIF}%B
will generate
ELSE
spurious newlines
ENDIF
C
if "cond" is true, I'd expect that to expand to:
ABwill generateC
and if "cond" is false,
ABspurious newlinesC

-- CrawfordCurrie - 16 Feb 2010

I re-read HereDocumentSyntaxForMacros and I accept your point that there is one newline too many.

I don't accept that the macro result should appear after text on the same line. Your "If it takes three men to dig a hole" example says otherwise.

-- MichaelTempest - 16 Feb 2010

The proposal was not accepted, so I am reverting this change.

-- MichaelTempest - 23 Feb 2010

Closing task.

-- MichaelTempest - 19 Apr 2010
 
Topic revision: r28 - 03 Nov 2015, GeorgeClark
The copyright of the content on this website is held by the contributing authors, except where stated elsewhere. See Copyright Statement. Creative Commons License    Legal Imprint    Privacy Policy