Skip to content

Navigation Menu

Sign in
Sign up

Using indented HEREDOC with Rex tasks #1533

Discussion options

Using HEREDOC for adding a multi-line string to the configuration file with append_if_no_such_line command:

task 'loader', group => 'testing', sub {
	my $file = '/boot/loader.conf';
	append_if_no_such_line $file, <<~ "LOADER";
	kernel="kernel"
	bootfile="/boot/kernel/kernel"
 LOADER
};

Here I'm passing HEREDOC as the function's second argument.

Task fails with:

[2022年06月21日 19:08:26] ERROR - Compile time errors:
[2022年06月21日 19:08:26] ERROR - Indentation on line 1 of here-doc doesn't match delimiter at __Rexfile__.pm line 403.
[2022年06月21日 19:08:26] ERROR - Compilation failed in require at /home/regular/perl5/perlbrew/perls/perl-5.36.0/lib/site_perl/5.36.0/Rex/CLI.pm line 759.

Note the <<~ not sure if Rex will support indented HEREDOC's in Perl.

The construct works in Perl scratch file.

Do I need to create an issue about it?

You must be logged in to vote

HEREDOCs should work in general. Rex code is just Perl code, there's no distinction.

The Compile time errors message suggests that Rex receives invalid Perl code somehow. When loading the Rexfile upon rex execution, it is treated as a perl module named __Rexfile__. This helps avoiding to execute invalid code, by detecting them early (and offloads the whole check to standard perl).

The problem here is that the ending delimiter LOADER is indented with 8 spaces, while the previous lines are indented with single tabs. I could reproduce the initial error, but when I made the HEREDOC indentation correct, it worked for me as expected.


That being said, in general I would not recommend using the a...

Replies: 2 comments 1 reply

Comment options

HEREDOCs should work in general. Rex code is just Perl code, there's no distinction.

The Compile time errors message suggests that Rex receives invalid Perl code somehow. When loading the Rexfile upon rex execution, it is treated as a perl module named __Rexfile__. This helps avoiding to execute invalid code, by detecting them early (and offloads the whole check to standard perl).

The problem here is that the ending delimiter LOADER is indented with 8 spaces, while the previous lines are indented with single tabs. I could reproduce the initial error, but when I made the HEREDOC indentation correct, it worked for me as expected.


That being said, in general I would not recommend using the append_if_no_such_line() command to manage multiple lines like that. Its purpose is to operate on single lines, so it's not an optimal choice to manage multiple lines. As the docs state:

If no regexp is supplied, the line is appended unless there is already an identical line in $file.

Checking the presence of a multiline input while operating on a line-by-line basis would certainly not work as expected. It would fail to detect whether the content is already there or not, and just append it anyway each time this task is executed.

To append two lines, call append_if_no_such_line twice:

append_if_no_such_line $file, q(kernel="kernel");
append_if_no_such_line $file, q(bootfile="/boot/kernel/kernel");

Alternatively, for best experience, I would recommend using file() (with content => template(), perhaps) to manage file content with a single operation.

You must be logged in to vote
1 reply
Comment options

will correct, thanks for the recommendation

Answer selected by rwp0
Comment options

I also didn't know about __Rexfile__.pm being the Rexfile (it's very useful to know); now I'll easily check line 403 for errors which my eyes were looking for but were failing to find since now 🙂

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
2 participants

AltStyle によって変換されたページ (->オリジナル) /