in reply to Re: Interpolation requires a spurious variable. Why?
in thread Interpolation requires a spurious variable. Why?

I missed the parenthesization approach of ikegami and LanX, but my personal preference (after moritz's approach, of course) would be  "@{[ ... code ... ]}" temporary array interpolation. This is probably more expensive computationally, but it appeals to me as 'cleaner' code.

Replies are listed 'Best First'.
Re^3: Interpolation requires a spurious variable. Why?
by LanX (Saint) on Sep 24, 2009 at 05:17 UTC
    Why should it be cleaner? It's just another case and depends on the context you want for the inner code.

    Instead of a scalar you require a list to be interpolated into a string!

    (which also involves the extra complication of setting $" appropiately.)

    Cheers Rolf

      It's just another case and depends on the context you want for the inner code.
      Not really.
      "@{[ ... list context ... ]}";
      That's what everyone expects. But:
      "${\(... list context ... )}";
      The reference operator (\) provides list context to its arguments. I've been bitten by this in the past as well.
        ups that's true, I forgot about this, Thx! :)

        Cheers Rolf

      Update please disregard, I should have read more carefully...

      It's cleaner because it also works when multiple elements are present:

      use strict; use warnings; use 5.010; my ($a, $b) = (4, 5); say "@{ [$a // 2, $b] }"; # 4 5 say "@{\($a // 2, $b) }"; # Not an ARRAY reference at foo.pl l +ine 8.

      The feature of \( LIST ) to return the same as map \$_, LIST is not always intuitive.

      Perl 6 - links to (nearly) everything that is Perl 6.
        Noone suggested @{\ }. That would never work unless you had an array, in which case you don't need this trick at all. @{[ ]} was being compared to ${\ } and/or ${\( )}.
Re^3: Interpolation requires a spurious variable. Why?
by Porculus (Hermit) on Sep 24, 2009 at 21:15 UTC

    It's not so much that it's "cleaner". The reason I'd prefer it myself is that it's idiomatic. It's a fairly well-known construct. Even if you don't use it yourself, it's worth knowing because it's something you'll probably see in others' code.

    I don't think the same is as true of "${\()}" -- I can't recall ever seeing that before today.