in reply to Re^2: Print array from here-doc
in thread Print array from here-doc

One trick to do that is the "babycart operator" @{[]}:
my $t = "\t"; my $msg = <<"END_OF_BODY"; The number of SKUs that have been flagged as DeleteMe is greater than +the threshold of $threshold and thus Manager approval is required. Here is the list of SKUs and their counts: PLANNERCODE${t}DEALER${t}COUNT ================================= @{[ join("\n", map {join("\t",@$_)} @output_array) ]} END_OF_BODY

The here-doc construct <<"END_OF_BODY"interpolates as within double quotes. The @{ } thingy dereferences (even between double quotes) an anonymous array, which is - inside that - constructed with [ ] (see perlref). To build that anonymous array, the content must first be expanded - and here's where the magic is: it can be any valid expression which returns a list, up to e.g. a complex do BLOCK statement.

Note that '\t' is not expanded into a <Tab> in here-docs. You have to stuff that char into a variable first. The ${t} construct serves to disambiguate from e.g. $tCOUNT.

But generally, when you need to do strange and funny things in here-docs, formats are better, provided you have a somehwat regular data set (i.e. a fixed number of elements in an array). format templates are static, though you could also whip them up on the fly using formline and the accumulator (see perlvar).

But that, again, also means doing funny things.

When you reach the limits of formats, use some templating solution.

--shmem

_($_=" "x(1<<5)."?\n".q·/)Oo.  G°\        /
                              /\_¯/(q    /
----------------------------  \__(m.====·.(_("always off the crowd"))."·
");sub _{s./.($e="'Itrs `mnsgdq Gdbj O`qkdq")=~y/"-y/#-z/;$e.e && print}

Replies are listed 'Best First'.
Re^4: Print array from here-doc
by johngg (Canon) on Apr 25, 2007 at 22:42 UTC
    Note that '\t' is not expanded into a <Tab> in here-docs

    The code here seems to contradict that. I don't know whether the behaviour of here-docs has changed between versions; I never use them as I don't like the way they disrupt the visual clues from code indenting. I am using 5.8.4 on Solaris 10 for SPARC.

    Cheers,

    JohnGG