| 1 | package App::CSVUtils::csv_fill_template; | ||||||||||
| 2 | |||||||||||
| 3 | 1 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 1 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 1 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | |||||||||||
| 7 | 1 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||
| 8 | 1 | our $DATE = '2026-07-09'; # DATE | |||||||||
| 9 | 1 | our $DIST = 'App-CSVUtils'; # DIST | |||||||||
| 10 | 1 | our $VERSION = '1.038'; # VERSION | |||||||||
| 11 | |||||||||||
| 12 | 1 | ▶ | use App::CSVUtils qw(gen_csv_util); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 13 | |||||||||||
| 14 | gen_csv_util( | ||||||||||
| 15 | name => 'csv_fill_template', | ||||||||||
| 16 | summary => 'Substitute template values in a text file with fields from CSV rows', | ||||||||||
| 17 | description => <<'MARKDOWN', | ||||||||||
| 18 | |||||||||||
| 19 | Templates are text that contain `[[NAME]]` field placeholders. The field | ||||||||||
| 20 | placeholders will be replaced by values from the CSV file. This is a simple | ||||||||||
| 21 | alternative to mail-merge. (I first wrote this utility because LibreOffice | ||||||||||
| 22 | Writer, as always, has all the annoying bugs; that particular time, one that | ||||||||||
| 23 | prevented mail merge from working.) | ||||||||||
| 24 | |||||||||||
| 25 | Example: | ||||||||||
| 26 | |||||||||||
| 27 | % cat madlib.txt | ||||||||||
| 28 | Today I went to the park. I saw a(n) [[adjective1]] [[noun1]] running | ||||||||||
| 29 | towards me. It looked hungry, really hungry. Horrified and terrified, I took | ||||||||||
| 30 | a(n) [[adjective2]] [[noun2]] and waved the thing [[adverb1]] towards it. | ||||||||||
| 31 | [[adverb2]], when it arrived at my feet, it [[verb1]] and [[verb2]] me | ||||||||||
| 32 | instead. I was relieved, the [[noun1]] was a friendly creature after all. | ||||||||||
| 33 | After we [[verb3]] for a little while, I went home with a(n) [[noun3]] on my | ||||||||||
| 34 | face. That was an unforgettable day indeed. | ||||||||||
| 35 | |||||||||||
| 36 | % cat values.csv | ||||||||||
| 37 | adjective1,adjective2,adjective3,noun1,noun2,noun3,verb1,verb2,verb3,adverb1,adverb2 | ||||||||||
| 38 | slow,gigantic,sticky,smartphone,six-wheeler truck,lollipop,piece of tissue,threw,kissed,stared,angrily,hesitantly | ||||||||||
| 39 | sweet,delicious,red,pelican,bottle of parfume,desk,exercised,jumped,slept,confidently,passively | ||||||||||
| 40 | |||||||||||
| 41 | % csv-fill-template values.csv madlib.txt | ||||||||||
| 42 | Today I went to the park. I saw a(n) slow six-wheeler truck running | ||||||||||
| 43 | towards me. It looked hungry, really hungry. Horrified and terrified, I took | ||||||||||
| 44 | a(n) gigantic lollipop and waved the thing angrily towards it. | ||||||||||
| 45 | hesitantly, when it arrived at my feet, it threw and kissed me | ||||||||||
| 46 | instead. I was relieved, the six-wheeler truck was a friendly creature after all. | ||||||||||
| 47 | After we stared for a little while, I went home with a(n) piece of tissue on my | ||||||||||
| 48 | face. That was an unforgettable day indeed. | ||||||||||
| 49 | |||||||||||
| 50 | --- | ||||||||||
| 51 | Today I went to the park. I saw a(n) sweet pelican running | ||||||||||
| 52 | towards me. It looked hungry, really hungry. Horrified and terrified, I took | ||||||||||
| 53 | a(n) delicious bottle of parfume and waved the thing confidently towards it. | ||||||||||
| 54 | passively, when it arrived at my feet, it exercised and jumped me | ||||||||||
| 55 | instead. I was relieved, the pelican was a friendly creature after all. | ||||||||||
| 56 | After we slept for a little while, I went home with a(n) desk on my | ||||||||||
| 57 | face. That was an unforgettable day indeed. | ||||||||||
| 58 | |||||||||||
| 59 | MARKDOWN | ||||||||||
| 60 | |||||||||||
| 61 | add_args => { | ||||||||||
| 62 | template_filename => { | ||||||||||
| 63 | schema => 'filename*', | ||||||||||
| 64 | req => 1, | ||||||||||
| 65 | pos => 1, | ||||||||||
| 66 | }, | ||||||||||
| 67 | }, | ||||||||||
| 68 | tags => ['category:templating'], | ||||||||||
| 69 | |||||||||||
| 70 | examples => [ | ||||||||||
| 71 | ], | ||||||||||
| 72 | |||||||||||
| 73 | writes_csv => 0, | ||||||||||
| 74 | |||||||||||
| 75 | 0 | ▶ | on_begin => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 76 | 0 | my $r = shift; | |||||||||
| 77 | 0 | $r->{wants_input_row_as_hashref}++; | |||||||||
| 78 | |||||||||||
| 79 | 0 | require File::Slurper::Dash; | |||||||||
| 80 | |||||||||||
| 81 | 0 | my $template = File::Slurper::Dash::read_text($r->{util_args}{template_filename}); | |||||||||
| 82 | |||||||||||
| 83 | # this is the key we add to the stash | ||||||||||
| 84 | 0 | $r->{template} = $template; | |||||||||
| 85 | 0 | $r->{filled_template} = ''; | |||||||||
| 86 | }, | ||||||||||
| 87 | |||||||||||
| 88 | 0 | ▶ | on_input_data_row => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=3 SCAR=24.8
| |||||||||||
| 89 | 0 | my $r = shift; | |||||||||
| 90 | |||||||||||
| 91 | 0 | my $text = $r->{template}; | |||||||||
| 92 | 0 | ▶ | $text =~ s/\[\[(.+?)\]\]/defined $r->{input_row_as_hashref}{$1} ? $r->{input_row_as_hashref}{$1} : "[[UNDEFINED:$1]]"/eg; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 93 | 0 | ▶ | $r->{filled_templates} .= (length $r->{filled_template} ? "\n---\n" : "") . $text; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 94 | }, | ||||||||||
| 95 | |||||||||||
| 96 | writes_csv => 0, | ||||||||||
| 97 | |||||||||||
| 98 | 0 | ▶ | on_end => sub { | ||||||||
|
Subroutine
__ANON__: not called CC=1 SCAR=6.9
| |||||||||||
| 99 | 0 | my $r = shift; | |||||||||
| 100 | 0 | $r->{result} = [200, "OK", $r->{filled_templates}]; | |||||||||
| 101 | }, | ||||||||||
| 102 | 1 | ); | |||||||||
| 103 | |||||||||||
| 104 | 1 | 1; | |||||||||
| 105 | # ABSTRACT: Substitute template values in a text file with fields from CSV rows | ||||||||||
| 106 | |||||||||||
| 107 | __END__ | ||||||||||