package Utilities::Data;
use strict;
use vars qw(@ISA @EXPORT_OK);

require Exporter;

@ISA       = qw(Exporter);
@EXPORT_OK = qw(deffed filtered quoted trimmed);

# This subroutine returns a list that is a copy of the input list with all undefs being replaced with an empty string.
sub deffed
{
    my @vals = map {defined($_) ? $_ : ''} @_;
    wantarray ? @vals : $vals[0];
}

my $illegal_characters = q/"|[]{}/; # emacs kludge => ";
my $tr_command         = "tr/$illegal_characters//d";

# This subroutine filters out all the illegal characters in each element of the list that is passed in.
sub filtered
{
    my @vals = @_; # copy the input so that we don't inadvertently try to alter an rvalue.
    (defined($_) && eval $tr_command) foreach @vals;
    wantarray ? @vals : $vals[0];
}

# This subroutine returns a list that has each element single quoted and with single quotes within the element doubled.
sub quoted
{
    my @vals = @_; # copy the input so that we don't try to alter an rvalue
    (defined($_) && (s/\'/\'\'/g, $_ = "'$_'")) foreach @vals;
    wantarray ? @vals : $vals[0];
}

# This subroutine returns a list in which each element has had its leading and trailing whitespace removed.
sub trimmed
{
    my @vals = @_; # copy the input so that we don't try to alter an rvalue
    (defined($_) && (s/^\s+//, s/\s+$//)) foreach @vals;
    wantarray ? @vals : $vals[0];
}

1;


    


