#!/usr/bin/perl
use strict;
sub foo {
local $, = '... ';
print ('a', 'list', 'of', "things\n");
}
foo();
print "This", "is", "a", "test\n";
####
a... list... of... things
Thisisatest
####
use strict;
# Transform 'local' to 'my', and get rid of needless
# initialization to 'undef'
my $usefuldata = 1; # in real life, lots of logic here
if ( blah_blah() ) {
# Eliminated second definition of $usefuldata
$usefuldata = 2; # in real life, lots of logic here
}
insert_into_database($usefuldata)
####
use strict;
# Put all of the $usefuldata stuff in a subroutine
sub get_useful_data
{
# Eliminated initial assignment of $usefuldata...
# There's no need to calculate it if blah_blah()
# is true and we're going to replace the value anyway
if ( blah_blah() ) {
# No need to store $usefuldata in a variable...
# just return it
return 2;
}
else {
# Since blah_blah() is false, we return the default
# case
return 1;
}
}
# Now we can calculate $usefuldata exactly where we want it
insert_into_database( get_useful_data() )