Menu

#6 Suggested code clean-up

open
nobody
None
5
2003-07-15
2003-07-15
Anonymous
No

Much of the code base could benefit from the
application of two perl constructs:

1. AUTOLOAD
2. HERE-doc

1. AUTOLOAD
In many files, such as agent.pm, most of the get_* and
set_* functions just get/set a field in a hash. This can
be automated with the AUTOLOAD mechanism. You
could write an AUTOLOAD subroutine that would simply
map get_* and set_* functions to get/set a field in the
hash, automagically. This use of AUTOLOAD essentially
emulates all of those other functions. The nice thing
about it is that you can get rid of most (i.e. trivial)
get/set functions, and then every time you add a new
field for which you need a get/set accessor, it's already
there!

2. HERE-doc
Instead of specifying many print lines one right after the
other (which is I/O inefficient), like this:
print "blah";
print "$var";
print $obj->fn();

You can say this:
print <<"EOF";
blah
$var
${\$obj->fn()}
EOF

Note the use of the ${REF} construct in the example
above. You can also do that with arrays and hashes:
@{\@ary} or @{ [qw/stuff here/] }
%{\%hash} or %{ {qw/key val key1 value1/} }

If you don't want interpolation, you can do this:
print <<'EOF'; # single quotes
blah
literal $var, no interpolation here
literla $obj->fn(), no interpolation here
EOF

This would significantly clean up your code base and
increase maintainability.

Any questions or comments: dpmott@sep.com

Thanks!

Discussion


Log in to post a comment.