String interpolation
Jonathan McKeown
jonathan at hst.org.za
Tue Nov 21 07:41:33 GMT 2006
On Monday 20 November 2006 18:40, Ovid wrote:
> --- Daniel Barlow <dan at telent.net> wrote:
> > DB<2> x "2 plus 2 is @{[2+2]}"
> > 0 '2 plus 2 is 4'
> > DB<3> x "hello, my name is @{[ucfirst getlogin()]}. Who are you?"
> > 0 'hello, my name is Dan. Who are you?'
> >
> > I can't remember having seen this before: is that because (a) it's
> > new (unlikely, I know), (b) it's a bad idea, or (c) I just don't pay
> > enough attention when reading other people's code?
>
> It's not new, but it should be used with caution. If you put too much
> logic in there, it can get very confusing when a string throws an
> exception.
It's actually documented in perlfaq4. The other reason to be cautious about
its use is that it creates an anonymous array which it immediately
dereferences and interpolates - which means that the stuff inside is
evaluated in list context. When this makes a difference and you weren't
expecting it, it can bite you. I suspect fiddling with $" might be another
route to added astonishment.
Jonathan
More information about the london.pm
mailing list