my $record = REC::->new(
map { s/#.*$//; /^\s*([^=]+)=(.*?)\s*$/ ? ($1, $2) : () } read_file($file)
);
####
$ perl -Mstrict -Mwarnings -e '
my $raw_data = q{
# Some comment
NAME=John Smith
AGE=15 # years only!
RANK=Tier 1
URL=http://somesite.com
URI=http://example.com
};
package REC;
my @req_args = qw{NAME AGE RANK URL};
sub new {
my ($class, %args) = @_;
exists $args{$_} or die "FAILED: $_" for @req_args;
bless { %args } => $class;
}
sub print_for_config {
my $self = shift;
print "$_=", $self->{$_}, "\n" for @req_args;
}
package main;
print "=" x 20, "\nDATA IN\n", "=" x 20, "\n$raw_data\n";
my $record = REC::->new(
map { s/#.*$//; /^\s*([^=]+)=(.*?)\s*$/ ? ($1, $2) : () }
split /\n/ => $raw_data
);
print "=" x 20, "\nDATA OUT\n", "=" x 20, "\n";
$record->print_for_config;
'
====================
DATA IN
====================
# Some comment
NAME=John Smith
AGE=15 # years only!
RANK=Tier 1
URL=http://somesite.com
URI=http://example.com
====================
DATA OUT
====================
NAME=John Smith
AGE=15
RANK=Tier 1
URL=http://somesite.com
####
my record =map{