Dave's Brain

Browse - programming tips - perl semicolon not always needed

Date: 2010sep29
Language: perl

Q.  Do I aways need the semicolon in Perl?

A.  No.  The semicolon is a separator so you only need it between statements.
For example:

	if ($is_something) { next; }

Can be replaced with:

	if ($is_something) { next }

Since there isn't a statement after the 'next' in that block.

(Even more compactly you can write this as:

	next if $is_something;

But that's not the point here.)

Another place you'll find needless semicolons is 'return' statements.
Since by definition there is nothing after them.
eg:

	sub foo() {
		return 'hello';
	}

Does not need the semicolon.

Basically every semicolon before a close brace is unnecessary.

If you keep this info in mind you can avoid quite a few semicolons in your
Perl code and it a bit smaller.
What this info useful to you? You can donate to say thanks

Add a comment

Sign in to add a comment
Copyright © 2008-2012, dave - Code samples on Dave's Brain is licensed under the Creative Commons Attribution 2.5 License. However other material, including English text has all rights reserved.
Advertisements: