perl - best way to write code to compile with strict -
what best way check arguments provided subroutines compile strict ( solution works there better way of doing ? )
i have large old library, want correct code can add use strict; it, of error (on page load generates 189 errors):
use of uninitialized value in string eq @ ../lib/cgilibtest.pl line 1510 use of uninitialized value in concatenation (.) or string @ ../lib/cgilibtest.pl line 1511.
they generated subroutines writen this:
#!/usr/bin/perl -w sub example() { $var1 = shift @_; $var2 = shift @_; $var3 = shift @_; if($var2 eq ""){var2 = "something";} # line generates first type of error beacause $var2 not defined return "var1 = ".$var1.", var2 = ".$var2.", var3 = ".$var3; # line generates second type of error, beacause $var3 not defined } print "content-type: text/html\n\n"; $somevar = &example("firstvar"); print $somevar;
my solution use ternary operator , write them as:
#!/usr/bin/perl -w use strict; sub example() { $var1 = ($_[0])?$_[0]:""; $var2 = ($_[1])?$_[1]:"something"; $var3 = ($_[2])?$_[2]:""; return "var1 = ".$var1.", var2 = ".$var2.", var3 = ".$var3; } print "content-type: text/html\n\n"; $somevar = &example("firstvar"); print $somevar;
in php use function example ($var1, $var2 = null, $var3 = null){...}
p.s. if knows tutorial or manual "write strict code in perl" please leave link it.
thank you.
- i think pbp (perl best practices) read on "strict" style. if follow says, at least writing code compliant
strict
,warnings
.
what see uninit warnings. and, uninit warnings come warnings
, not strict
. though goes hand in hand strict.
for particular error, it's important realize when shift arg array (which have tendency call "deck") user of method/function might not have passed in position. idiom;
my $var1 = shift || '';
''
not uninitialized, , won't warning. ternaries means , use them enough in production code.
of course, there's issue though. if take @ code above, expecting? i'm expecting $var1
might not have been passed data. if case, , still want stringify it--and there enough cases in local code--sometimes this:
# don't bug me uninitialized stringed values # concatenate them no warnings 'uninitialized'; return "var1 = ".$var1.", var2 = ".$var2.", var3 = ".$var3;
it's equivalent code doing bunch of variables:
my $var = shift || '';
the idea not taken surprise uninitialized data--having policy fits instance, rather slavishly coding standard.
since sub easy enough, above code work. want make more in future, suggest following construct isolate parts we're okay uninitialized values , don't want caught unaware.
{ no warnings 'uninitialized'; $string = ...; } $obj_that_should_be_defined->do_something_objecty( $string );
do
blocks useful this.
$obj_that_should_be_defined->do_something_objecty( { no warnings 'uninitialized'; sprintf( ... ); } );
of course pass variable, can filter map
. following works if want take care of undefs.
$object->method( map { defined() ? $_ : '' } @anon_args );
- i word of warning hasn't been checked in scope--anything coming somewhere else: parameter, io, regex capture, returns functions--can uninitialized. practice of coding should have checking these things if care in instance whether
undef
of not.
now, lot of times people check undefined variables croak. since perl's messages have gotten better, , perhaps if improve in future. might not necessary croak specifically.
my $feldman = feldman->new( qw<here data you> ) or die 'could not create feldman!' ; $feldman->dont_just_sit_there_do_something();
however without explicit or die
, code above die if $feldman
undefined. can't call method on undefined value, there case not checking. if $feldman
undefined, didn't create it, there 1 more level of inference have make undefined error message. neither tells why object not created, wasn't. (the constructor should @ least have carped didn't have.)
Comments
Post a Comment