Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

Reusing a Parse::CSV module

by adambe (Initiate)
on Dec 01, 2011 at 16:14 UTC ( #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?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://941110]
Approved by herveus
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (2)
As of 2018-03-24 08:40 GMT
Find Nodes?
    Voting Booth?
    When I think of a mole I think of:

    Results (297 votes). Check out past polls.