Reputation:
I currently have the following:
elsif ($line =~ /^(\s*)(if|elif|else)\s*(.+)*\s*:\s*$/) {
# Multiline If
# Print the If/Elif condition
if ($2 eq "if"){
print "$1$2 ($3){\n";
}
elsif ($2 eq "elif"){
print "$1elsif ($3){\n";
}
elsif ($2 eq "else"){
print "$1$2 $3{\n";
}
# Add the space before the word "if"/"elif"/"else" to the stack
push(@indentation_stack, $1);
}
I am getting the stated error, but I'm not sure why. In the final elsif
, if I add a \
before the {
in the print
statement, the code doesn't generate an error.
I.E:
elsif ($2 eq "else"){
print "$1$2 $3\{\n";
}
Could someone please explain to me why this is occurring?
Thanks for your help!
Upvotes: 5
Views: 504
Reputation: 385849
Tricky! The problem is that the following is the start of hash lookup:
$3{
You want the equivalent of
$3 . "{"
which can be written as
"${3}{"
The following works in this case because the \
can't possibly be part of the variable there:
"$3\{"
But that trick can't always be used. For example, consider
$foo . "bar"
If you try
"$foo\bar"
You'll find you get
$foo . chr(0x08) . "ar"
because "\b"
returns the "bell" character. That leaves you with
"${foo}bar"
Upvotes: 7