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?
In reply to curly braces inside of curly braces by Special_K
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |