Perl with a semicolon at the end of the statement

;It is used as a separator of instructions, so placing a few ;at the end of an instruction is excellent, because it simply adds empty statements.

I came across this code, which has several ;at the end, but deleting them, causing errors:

$line =~s;[.,]$;;;

should be the same as

$line =~s;[.,;]$;

but this is not so. What's happening?

+5
source share
3 answers

In your code, only the last one ;is a separator. The rest are regular expression delimiters that the substitution operator accepts. The best way to write this is:

$line =~s/[.,]$//;

, ;

+7

; search-n-replace.

$line =~s;[.,]$;;;

$line =~ s/[.,]$//;
+13

The semicolon is not a universal operator separator; it can also be a quote string or a regex separator. Or even a variable name, as in this classic JAPH from Abigail, entitled "Things are not what they seem."

$;                              # A lone dollar?
=$";                            # Pod? 
$;                              # The return of the lone dollar?
{Just=>another=>Perl=>Hacker=>} # Bare block?
=$/;                            # More pod?
print%;                         # No right operand for %?
+3
source

All Articles