Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl semicolon at end of statement

Tags:

perl

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?

like image 973
Akki Javed Avatar asked Dec 13 '11 04:12

Akki Javed


People also ask

Does Perl require semicolons?

Semicolons are required after all simple statements in Perl (except at the end of a block). Newline is not a statement delimiter.

How are statements terminated in Perl?

every statement in Perl must end with a semicolon(;).


2 Answers

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/[.,]$//;
like image 178
Filip Roséen - refp Avatar answered Sep 27 '22 23:09

Filip Roséen - refp


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 ;

like image 37
codaddict Avatar answered Sep 27 '22 22:09

codaddict