« Sightseeing | Main | parse-sudoers - reformat sudoers data »

Delimited Data Generation

Output from the following code may fail validation where a downstream system expects key:value pairs and does not support empty fields at the end of the record.

print "OK:foo:" . join(':', @key_value_pairs) . "\n"

If @key_value_pairs is empty, resulting in the unacceptable OK:foo:. Safer code would include this prefix as an argument to the join function:

my @OUTPUT_PREFIX = qw(OK foo); print join(':', @OUTPUT_PREFIX, @key_value_pairs) . "\n"

This way a proper colon delimited list is generated, or an empty string should both arrays be empty.

No, I haven’t been bitten by this gotcha. No, really!

Technorati Tags: ,