interesting behavior for Perl and Postgres

C

cartercc

Can anyone tell me why this happens? Here is the code, the console
output, and the results.

First, the code. This copies data from five external files into
(empty) tables in my database:

eval
{
system('psql -U studentinfo -d studentinfo -c "\\copy student08t5
from \'/home/studentinfo/data/import_student.csv\' using delimiters \'|
\' "') or warn "I could not copy STUDENT file to table, $!";
system('psql -U studentinfo -d studentinfo -c "\\copy section08t5
from \'/home/studentinfo/data/import_section.csv\' using delimiters \'|
\' "') or warn "I could not copy SECTION file to table, $!";
system('psql -U studentinfo -d studentinfo -c "\\copy phone08t5
from \'/home/studentinfo/data/import_phone.csv\' using delimiters \'|
\' "') or warn "I could not copy PHONE file to table, $!";
system('psql -U studentinfo -d studentinfo -c "\\copy email08t5
from \'/home/studentinfo/data/import_email.csv\' using delimiters \'|
\' "') or warn "I could not copy EMAIL file to table, $!";
system('psql -U studentinfo -d studentinfo -c "\\copy address08t5
from \'/home/studentinfo/data/import_address.csv\' using delimiters \'|
\' "') or warn "I could not copy ADDRESS file to table, $!";
};
warn $@ if $@;

Next, the output. This is what is printed to the console when I run
the script:

[studentinfo@tsuse data]$ perl test02.plx
I could not copy STUDENT file to table, No such file or directory at
test02.plx line 24.
I could not copy SECTION file to table, No such file or directory at
test02.plx line 25.
I could not copy PHONE file to table, No such file or directory at
test02.plx line 26.
I could not copy EMAIL file to table, No such file or directory at
test02.plx line 27.
I could not copy ADDRESS file to table, No such file or directory at
test02.plx line 28.
[studentinfo@tsuse data]$

Finally, the results. Well, I won't bore you with the results (the
files contain about 21,000 records) but this successfully writes the
data to the database.

Question: Why do the warn statements print if the copy command works?

CC
 
B

Ben Morrow

Quoth cartercc said:
Can anyone tell me why this happens? Here is the code, the console
output, and the results.

First, the code. This copies data from five external files into
(empty) tables in my database:

eval
{
system('psql -U studentinfo -d studentinfo -c "\\copy student08t5
from \'/home/studentinfo/data/import_student.csv\' using delimiters \'|
\' "') or warn "I could not copy STUDENT file to table, $!";

system returns 0 if the command succeeds, so your 'or' is the wrong
test. $! is only valid after a syscall has failed, so it will contain
something completely irrelevant here: the ENOENT you get is probably the
result of searching $PATH for psql.

Correctly determining if and why system failed is rather tricky: there
are lots of failure modes, and on some systems (notably Win32) some of
them actually report success. I would suggest using a module, like
IPC::Run or IPC::System::Simple.

Ben
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,764
Messages
2,569,564
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top