Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical
 
PerlMonks  

Reusing a Parse::CSV module

by adambe (Initiate)
on Dec 01, 2011 at 16:14 UTC ( [id://941110]=perlquestion: print w/replies, xml ) Need Help??

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

Problem was with some filenames in Windows which were causing trouble... Not_a_Number, I've added the "or die" clause, hopefully it will catch such problematic file names. Thanks, Adam. I've been playing around with Parse::CSV for a simple case of comparing two files – meaning I'd like to re-use the instance of the module. The second instantiation of the object results in an empty ARRAY reference… Am I missing something obvious or is there another way of going around this?
sub get_line { my ($file, $row_num) = @_; my $parser = Parse::CSV->new( file => $file, ); while ( my $value = $parser->fetch ) { if ($parser->row() == $row_num) { return $value; } ....
Thanks, Adam.

Replies are listed 'Best First'.
Re: Reusing a Parse::CSV module
by RichardK (Parson) on Dec 01, 2011 at 17:22 UTC

    I have a few questions for you :-

    • What do you mean by "re-use the instance of the module" ?
    • How does that code sample relate to your problem ?
    • What does fetch return when there are no more lines left ?

Re: Reusing a Parse::CSV module
by Not_a_Number (Prior) on Dec 01, 2011 at 17:45 UTC

    Presuming that you just pass two filenames, one after the other, to your sub, your code works for me as is.

    But I don't see any error checking. Maybe the scond file doesn't exist, or you've misspelt its name?

    Change the second line in your sub to:

    my $parser = Parse::CSV->new( file => $file ) or die Parse::CSV->errstr;

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://941110]
Approved by herveus
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (6)
As of 2024-04-23 21:06 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found