+---------------+-------------+------+-----+------------+-------+
| Field | Type | Null | Key | Default | Extra |
+---------------+-------------+------+-----+------------+-------+
| employee_id | varchar(9) | | PRI | | |
| firstname | varchar(50) | | | | |
| last_name | varchar(50) | | | | |
| title | varchar(10) | | | | |
| dept | varchar(10) | | | | |
| salary | mediumint | | | | |
| date_hired | date | | | | |
+---------------+-------------+------+-----+------------+-------+
####
#!/usr/bin/perl -w
use strict;
use DB_File;
use Mojo::DB qw(get_dbh); # this is just my shortcut for accessing DBI
$| = 1;
my $dbh = get_dbh();
unless @ARGV { die "No filename argument provided"; }
my $file = shift @ARGV;
unlink $file;
my %IDS;
# note this line right here:
tie %IDS, "DB_File", $file or die "Can't open $file: $!";
my @keys =
qw(employee_id firstname lastname title dept salary date_hired);
my $keys = join(', ', @keys);
my $query = "select $keys from employee_data";
my @values = @{$dbh->selectall_arrayref($query)};
for my $v (@values){
my @vals = @$v; # one ID per array
for (@vals){
$_ = '-' unless $_; # in case of empty fields
}
# here's where I clumsily interpolate the column headings
my @ary;
for (@keys){
push @ary, $_;
push @ary, shift @vals;
}
my @bry;
shift @ary; # lose the first column heading since the employee IDs are going the be the keys now... I know, it's cringe-worthy...
my $key = shift @ary;
while (@ary){
my $k = shift @ary;
my $v = shift @ary;
push @bry, { $k => $v };
}
$IDS{$key} = \@bry;
}
# and then because I don't trust anything unless I see it for myself:
for (keys %IDS){
print "\n\n$_:\n";
for my $key (@{$IDS{$_}}){
while (my($k, $v) = each %{$key}){
print "$k => $v\n";
}
}
}
untie %IDS;
####
12345678:
firstame => Fritz
lastname => Froemming
title => analyst
dept => research
salary => peanuts
date_hired => 2006-01-20
####
#!/usr/bin/perl -w
use strict;
use DB_File;
$| = 1;
unless @ARGV { die "No filename argument provided"; }
my $file = shift @ARGV;
# here's this line again:
tie my %IDS, "DB_File", $file or die "Can't open $file: $!";
# and this block is the same as I used for testing above
for (keys %IDS){
print "\n\n$_:\n";
for my $key (@{$IDS{$_}}){
while (my($k, $v) = each %{$key}){
print "$k => $v\n";
}
}
}
####
12345678:
Can't use string ("ARRAY(0x84d90b8)") as an ARRAY ref while "strict refs" in
use at read_db.pl line 18 (#1)
(F) Only hard references are allowed by "strict refs". Symbolic
references are disallowed. See perlref.
Uncaught exception from user code:
Can't use string ("ARRAY(0x84d90b8)") as an ARRAY ref while "strict refs" in use at read_db.pl line 18.
at read_db.pl line 18
####
tie %IDS, "DB_File", $file or die "Can't open $file: $!";
####
12345678:
firstame => Fritz
lastname => Froemming
title => analyst
dept => research
salary => peanuts
date_hired => 2006-01-20