in reply to Re: Questions about context
in thread Questions about context

Ah, I see, so it's kind of a trick: you create an anony array reference and then you dereference it, so it's like it never existed! The only point was to interpolate what was inside.

Would it work if I used an anony hash reference, instead of an array reference? For example:

%{ { (localtime)[5] } }

Replies are listed 'Best First'.
Re^3: Questions about context
by davido (Cardinal) on Jan 30, 2005 at 05:04 UTC

    Hashes don't interpolate in double-quoted strings. That's the first reason it doesn't work. The second reason is that hashes need an even number of elements, and you're generating an odd number of elements.

    To illustrate both of those points, try the following:

    my %hash = ( one => 1, two => 2 ); print "%hash\n"; # See, it doesn't interpolate. %hash = ( localtime )[5]; # Now you get a warning about uneven number +of elements.

    Far more than you ever wanted to know about string interpolation is described in perlop.


    Dave

      Far more than you ever wanted to know about string interpolation is described in perlop.

      And yet more in MJD's Identity.

Re^3: Questions about context
by borisz (Canon) on Jan 30, 2005 at 02:06 UTC
    No, that does not work.
    But you can write  print "@{[ %x ]}" if you want.
    Boris
Re^3: Questions about context
by ysth (Canon) on Jan 30, 2005 at 10:18 UTC
    No, hashes don't interpolate into double-quoted strings in perl5. But you can use a scalar de-ref/ref:
    print "year is ${\((localtime)[5]+1900)}!\n";
      ... you can use a scalar de-ref/ref:
      print "year is ${\((localtime)[5]+1900)}!\n";

      True, but I've finally managed to get this mechanism excised from the FAQ because it is messy-looking and confusing.

      Many people looking at the that code (including the authors of early versions of the FAQ) would expect the expression to be evaluated in a scalar context. It is not. The expression in \(EXPR) is evaluated in a list context. Not, of course, that this makes any difference for the + operator.

        Then perhaps it would be better to advocate this approach instead of the more symmetrically beautiful "@{[]}".

        It might lead more people to learn of the weird but wonderful context properties of  \. (Sometimes people wonder why perl doesn't have a "list" operator corresponding to "scalar". The answer usually given is that it would rub the camel hair the wrong way, or that it is never needed. In point of fact it is sometimes needed when doing things like recursive calls to subroutines that return a map. And the way you spell list(EXPR) is ${\(EXPR)}.)