There are many ways to accomplish this task. If it's a one-time deal, you can do something quick-n-dirty:
#!/usr/bin/perl -nl
chomp;
@record = split /\|/, $_;
print "INSERT INTO table (foo, bar, baz)"
, " VALUES ('$record[0]', '$record[1]', '$record[2]')";
Then run it with something like:
./yourscript < inputdata > output.sql
And you'd end up with a .sql file you could run against the database.
Now, if you need something more reliable and reusable, you'll probably want to wrap your own I/O code rather than letting perl -n take care of it for you (for more on this, you can see PerlMonks' Basic Input and Output tutorial). You'd probably want to insert the data directly into the database with DBI (make sure you use placeholders, as documented in the DBI docs). The basic idea about splitting the lines, as my quick-n-dirty snippet demonstrates, would be the same.
Update: Oh yeah, also forgot to mention. For anything other than a one-off throwaway script, you should probably use strict and warnings, unless there's a good reason not to. |