Slash Boxes
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

use Perl Log In

Log In

[ Create a new account ]

jdavidb (1361)

  (email not shown publicly)

J. David Blackstone has a Bachelor of Science in Computer Science and Engineering and nine years of experience at a wireless telecommunications company, where he learned Perl and never looked back. J. David has an advantage in that he works really hard, he has a passion for writing good software, and he knows many of the world's best Perl programmers.

Journal of jdavidb (1361)

Monday August 05, 2002
03:57 PM

First use of tie

[ #6911 ]

In DBD::CSV, certain assumptions are made:

  • Your files are comma delimited (good assumption, I'd think)
  • Your tables are DOS line-ending format. (Not all that great, but good for compatibility with Excel.
  • Your tables are stored in files named exactly the same as the table, not as "table.csv" or something. (Not all that great, and not even good for compatibility with Excel.)

All is not lost, though, as one of my more than capable coworkers discovered a while back. You can override all these on a per-database-handle, per-table basis. For example, my employee table is in the file "EMPLOYEE.TXT" and has Macintosh line endings. I just:

$dbh->{csv_tables}->{employee} = {
  file => "EMPLOYEE.txt",
  eol => "\r",

That works great, but now I'm working on a database with 20 tables. Every one of them is in a .csv file, has UNIX line endings, and is semicolon delimited instead of comma delimited. A big loop at the beginning of the program suggested itself, but didn't feel natural.

Then, a sure sign that I'm finally starting to think like a real perl programmer, the obvious solution presented itself: tie the $dbh->{csv_tables} hash. Of course!

As far as I know, I have never tied a hash in production code. In fact, I think I've only done it once or twice just to experiment with the feature. I'm glad tying is finally presenting itself as a tool in my arsenal.

In related news, I just discovered the ecode tag.

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
More | Login | Reply
Loading... please wait.
  • by Ovid (2709) on 2002.08.05 16:39 (#11430) Homepage Journal

    If you work with CSV databases, you know that validating the database can be a pain. I wrote a CSV database validation [] program that you might find useful. You can develop the schema in a syntax very similar to SQL, designate unique fields (such as IDs), foreign key constraints, and even specify a regex to validate individual fields against. Very useful, IMHO.