Akki Javed
Akki Javed

Reputation: 215

Perl semicolon at end of statement

The ; is used as a statement delimiter, so placing multiple ; at the end of a statement is fine as it just adds empty statements.

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

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

should be same as

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

but it is not. What's going on?

Upvotes: 7

Views: 4047

Answers (3)

ysth
ysth

Reputation: 98388

A semicolon is not universally a statement separator; it can also be a quoted string or regex delimiter. Or even a variable name, as in this classic JAPH by Abigail, entitled "Things are not what they seem like."

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

Upvotes: 2

codaddict
codaddict

Reputation: 454970

In your code only the last ; is the statement delimiter. The others are regex delimiters which the substitution operator takes. A better way to write this is:

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

Since you must have the statement delimiter and regex delimiters in your statement, you can't drop any of the trailing ;

Upvotes: 8

Filip Roséen
Filip Roséen

Reputation: 63797

in the snippet provided by you a ; is used as a delimiter for a search-n-replace regular expression.

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

is equivalent to

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

Upvotes: 13

Related Questions