Beefy Boxes and Bandwidth Generously Provided by pair Networks
We don't bite newbies here... much
 
PerlMonks  

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.

Comment on Reusing a Parse::CSV module
Download Code
Replies are listed 'Best First'.
Re: Reusing a Parse::CSV module
by RichardK (Vicar) 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 (Parson) 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
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? | Other CB clients
Other Users?
Others rifling through the Monastery: (7)
As of 2015-07-31 06:33 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    The top three priorities of my open tasks are (in descending order of likelihood to be worked on) ...









    Results (274 votes), past polls