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

I tried that but it prints out as: ARRAY(0x40052a44) ARRAY(0x40052a80) ARRAY(0x40052abc) ARRAY(0x40052af8) ARRAY(0x400f8310)

Replies are listed 'Best First'.
Re^3: Print array from here-doc
by shmem (Chancellor) on Apr 25, 2007 at 21:32 UTC
    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}
      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

Re^3: Print array from here-doc
by jZed (Prior) on Apr 25, 2007 at 19:55 UTC
    Then you probably have an array *reference* like $foo, in which case you need to de-reference it in the heredoc with @$foo.
      I tried @$output_array with no luck. I don't really understand the referencing/dereferencing so I'm sure it's something I'm doing.
      I tried @$output_array with no luck. I don't really understand the referencing/dereferencing so I'm sure it's something I'm doing.