Best Practice For PERL

use strict;
use warnings;
use diagnostics;

use strict

  • Generates an error if you use symbolic references ( Exp: $x=10; $var = “x”; $$var = “30”; print $x; )
  • It generates an error if you use a variable without declaring it. ( Exp: $a = 10; )
  • It also generates an error if you leave barewords hanging around in the script ( Exp:  foo()  #correct; foo #incorrect; )

use warnings

Generates a warning message based on many criteria, which are described in the POD ‘perllexwarn’. These warnings have nothing to do with strictures, but rather, watch for the most common “gotchas” one is likely to encounter in their programming. It is a best practice to use warnings while writing scripts too. In some cases where the message might be undesirable a certain warning category may be locally disabled within a scope. Additional info is described in ‘warnings’.

Example: Give warning message if uninitialized variable is used in script.

use diagnostics

makes the warnings more verbose, and in a development or learning environment, particularly among newcomers, that’s highly desirable.

Further more errors will be trapped by eval {} and Try::Tiny…

( Exp: $a = 10; )


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s