in reply to Interpolation requires a spurious variable. Why?
When the expression
\ $name || $default
is evaulated, the sub-expression \ $name has highest precedence. A reference is always true, so the second sub-expression (i.e., the $default scalar) is never evaluated.
I would prefer the approach given by moritz above, but if you absolutely must do code interpolation within a string, here are a couple of approaches:
>perl -wMstrict -le "my $default = q{''}; for my $name ('', 'fred') { print qq{name = ${ \ do { $name || $default }}}; print qq{name = @{[ $name || $default ]}}; } " name = '' name = '' name = fred name = fred
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: Interpolation requires a spurious variable. Why?
by ikegami (Patriarch) on Sep 23, 2009 at 22:13 UTC | |
|
Re^2: Interpolation requires a spurious variable. Why?
by LanX (Saint) on Sep 23, 2009 at 22:18 UTC | |
|
Re^2: Interpolation requires a spurious variable. Why?
by AnomalousMonk (Archbishop) on Sep 24, 2009 at 03:10 UTC | |
by LanX (Saint) on Sep 24, 2009 at 05:17 UTC | |
by JavaFan (Canon) on Sep 29, 2009 at 10:29 UTC | |
by LanX (Saint) on Sep 29, 2009 at 12:17 UTC | |
by moritz (Cardinal) on Sep 24, 2009 at 13:42 UTC | |
by ikegami (Patriarch) on Sep 24, 2009 at 14:13 UTC | |
by Porculus (Hermit) on Sep 24, 2009 at 21:15 UTC |