That, my friend, is a maintainer's nightmare.
So, we need some way of abstracting out which fields are there. Then, we need a way of identifying which fields we actually want. Lastly, we need a way of making sure that this information (which will become quite large) doesn't impact our main program.
Enter "Modular Design". Let's create a module where we store some data about what fields we want. Let's also create a few functions that allow us to talk about various aspects (or attributes) of each field.
Now, you don't need a full-blown OO implementation of this. That would be too much work and too unwieldy to manage. However, you can do a very nice Exporter implementation of this. Something along the lines of:
You would then use this module as so:# Untested, but should work use 5.6.0; use strict; use warnings; package FieldStorage; use Exporter; our @ISA = qw(Exporter); # Using @EXPORT instead of @EXPORT_OK because we want these definitely + # in the caller's namespace. If we don't, use @EXPORT_OK. our @EXPORT = qw( getFieldNames isRequired ); my %Fields = ( FOO => { Order => 0, Required => 1, }, BAR => { Order => 1, Required => 0, }, .... ); sub getFieldNames { return sort { $a->{Order} <=> $b->{Order} } keys %Fields; } sub getFieldIndices { return map { $Fields{$_}{Order} } @_ || getFieldNames; } sub isRequired { my $field = shift; return undef unless exists $Fields{$field}; return $Fields{$field}{Required}; } 1; __END__
use FieldStorage; # Some stuff here my @fieldNames = grep { isRequired($_) } getFieldNames; my @fieldIndices = getFieldIndices(@fieldNames); my %records; @records{@fieldNames} = (split(/\t/, $_))[@fieldIndices];
------
We are the carpenters and bricklayers of the Information Age.
Don't go borrowing trouble. For programmers, this means Worry only about what you need to implement.
In reply to Re2: Ignoring values in a hash slice assignment
by dragonchild
in thread Ignoring values in a hash slice assignment
by impossiblerobot
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |