in reply to Best Practice for Lots of Static Data
Giving that you are trying to improve source-code management, as opposed to speed or configurability, I would move the static code into a proper module. You say that it is unlikely that other developers will need your code, but I would be surprised if such a large amount of code went into only one of *your* programs. Surprise aside, your code will be more testable with a 'use'able division for your tests to hook.
And yes, I said 'code', not 'data'. Your technique of assigning ordered fieldnames to positional data is excellent, but since those fieldnames have no independent use outside your code, I would treat them as code (to be stored in modules) instead of data (to be stored in a database or flat files).
Your first option, 'do/require', is not very different from a module with a bad interface, so I would skip the 'do' stage and go straight to a proper module. As long as you remain in single program/programmer mode, you can freely evolve the interface, piecemeal, from bad to great. See Perl Best Practices for great API advice.
As a proof of concept, I built a simple module implementing a basic (and bad) interface. I used h2xs because it is core Perl, but would use ModuleMaker or Module::Starter in real life.
$ h2xs -A -B -C -X -n SirBones::PerlMonks::Static_Data $ cd SirBones-PerlMonks-Static_Data $ edit lib/SirBones/PerlMonks/Static_Data.pm $ edit t/SirBones-PerlMonks-Static_Data.t $ prove -Ilib t/SirBones-PerlMonks-Static_Data.t
In the .t file, I changed the tests from '=> 1' to '=> 2', and added:
use SirBones::PerlMonks::Static_Data 'status_data'; is( status_data('dev2')->[2], 'UV_condition', '%status dev2' );
In the .pm file, I change the EXPORT_OK line to:
and added this just before the '1;' :our @EXPORT_OK = ( 'status_data' );
our %status = ( 'dev1' => [ qw( enabled crit_fault warning_fault ) ], 'dev2' => [ qw( power_on OV_condition UV_condition phase_fault ) ], ); sub status_data { my ( $device ) = @_; return $status{$device}; }
All (OK, both) tests passed.
|
|---|