$ cat sample.txt
a 5
b 6
c 7
d 8
####
#!/usr/bin/perl
use strict;
use warnings;
use CGI::FormBuilder;
my @fields = qw(a b c d);
my %values = get_values(shift || '');
my $form = CGI::FormBuilder->new(fields => \@fields, values => \%values);
print "$_ => ", $form->field($_), "\n" for @fields;
sub get_values {
my $way = shift;
return (a => 1, b => 2, c => 3, d => 4) unless $way;
my %fetched;
if ($way eq 'file') {
open my $fh, '<', 'sample.txt' or die "open failed: $!\n";
while (<$fh>) {
chomp;
my($key, $value) = split;
$fetched{$key} = $value;
}
close $fh;
}
# additional if??
return %fetched;
}
####
$ perl cfb.pl
a => 1
b => 2
c => 3
d => 4
$ perl cfb.pl file
a => 5
b => 6
c => 7
d => 8