Special_K has asked for the wisdom of the Perl Monks concerning the following question:
If I run the following code:
#!/usr/bin/perl -w use strict; my %test_hash = ("foo_bar" => "baz"); my $test = "foo"; my $key = "${test}_bar"; my $var1 = "aaa/$test_hash{$key}"; printf("var1 = $var1\n"); my $var2 = "aaa/$test_hash{${test}_bar}"; printf("var2 = $var1\n");
I receive the following error:
> ./braces_inside_braces.pl Bareword found where operator expected at ./braces_inside_braces.pl li +ne 14, near "${test}_bar" (Missing operator before _bar?) syntax error at ./braces_inside_braces.pl line 14, near "${test}_bar" Execution of ./braces_inside_braces.pl aborted due to compilation erro +rs.
The curly braces around "test" are required to separate it from "_bar", which is not part of the variable name, yet they are conflicting with the outer curly braces of test_hash. Is there any way to write that on one line without syntax errors, or do I have to use the method shown in the first case where I use the intermediate variable $key?
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: curly braces inside of curly braces
by Fletch (Bishop) on Feb 04, 2022 at 19:02 UTC | |
|
Re: curly braces inside of curly braces
by LanX (Saint) on Feb 04, 2022 at 19:26 UTC | |
by AnomalousMonk (Archbishop) on Feb 05, 2022 at 20:51 UTC | |
by AnomalousMonk (Archbishop) on Feb 06, 2022 at 20:57 UTC | |
by LanX (Saint) on Feb 05, 2022 at 21:08 UTC | |
|
Re: curly braces inside of curly braces
by talexb (Chancellor) on Feb 05, 2022 at 18:36 UTC |