Nested "if", Missing curly or square brackets

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 get a reported error, but I'm not sure why. In the last elsif , if I add \ to { in the print statement, the code does not generate an error.

IE:

 elsif ($2 eq "else"){ print "$1$2 $3\{\n"; } 

Can someone explain to me why this is happening?

Thanks for your help!

+5
source share
1 answer

Tricky! The problem is that the following hash search start:

 $3{ 

You need equivalent

 $3 . "{" 

which can be written as

 "${3}{" 

In this case, it works because \ cannot be part of a variable:

 "$3\{" 

But this trick cannot always be used. For example, consider

 $foo . "bar" 

If you try

 "$foo\bar" 

You will find that you get

 $foo . chr(0x08) . "ar" 

because "\b" returns a call symbol. It leaves you with

 "${foo}bar" 
+7
source

Source: https://habr.com/ru/post/1203581/


All Articles