McMahon has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks (and jZed)...
This code:
print "Type the name of the CSV file:\n"; my $CSVfile =<STDIN>; chomp $CSVfile; my $dbh = DBI->connect(qq{DBI:CSV:csv_sep_char=\\\t}); $dbh->{'csv_tables'}->{'tabdelimited'} = { 'file' => '$CSVfile'};

yields this error:
Execution ERROR: Cannot open .\$CSVfile: No such file or directory at + C:/Perl/site/lib/DBD/File.pm line 496, <STDIN> line 1.
Is there syntax to connect to a file specified by a variable?

(Update: forgot the "chomp".)

Replies are listed 'Best First'.
Re: DBD:CSV doesn't interpret variable in "$dbh->" open command?
by Aristotle (Chancellor) on Aug 10, 2004 at 18:24 UTC

    DBD::CSV is a red herring. It has nothing to do with the problem: you're putting $CSVfile in single quotes.

    Always assume the problem is in your own code first.

    Makeshifts last the longest.

      Double-quotes don't work either. Neither do /'s. That's why I wonder if there is any syntax that succeeds. This example is the closest one to the DBD::CSV documentation.
        Have you printed out what $csvFile contains? Maybe it isn't what you think ...

        ------
        We are the carpenters and bricklayers of the Information Age.

        Then there are Damian modules.... *sigh* ... that's not about being less-lazy -- that's about being on some really good drugs -- you know, there is no spoon. - flyingmoose

        I shouldn't have to say this, but any code, unless otherwise stated, is untested

        From the error message you show, it looks to me like double quotes will work (or better yet, no quotes), but possibly only for relative paths.
Re: DBD:CSV doesn't interpret variable in "$dbh->" open command?
by jZed (Prior) on Aug 15, 2004 at 18:21 UTC
    You definitely need to lose the quotes around '$CSVfile'. Additionally, note that unless the filename includes an absolute path (e.g. starts with a volume letter or a slash), DBD::CSV will assume that the file is in the f_dir directory which defaults to whatever the script assumes is the current working directory. Check to make sure that you are in the directory you think you are and explicitly set f_dir if you aren't.