Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Re: CSV_XS issue

by talexb (Canon)
on Apr 07, 2013 at 13:52 UTC ( #1027371=note: print w/replies, xml ) Need Help??

in reply to CSV_XS issue

Finally, from a stylistic point of view, you're calling main and then declaring it a line later .. you can just skip both of those steps. Here's how I would re-format your code..

use strict; use warnings; use Text::CSV_XS; { my @row1 = ("aaa","fff","sss" ); my @row2 = ("bbb","fff","sss"); # should set binary att my $csv = Text::CSV_XS->new ( { binary => 1 } ) or die "Cannot use CSV: ".Text::CSV->error_diag (); open my $fh, ">:encoding(utf8)", "new.csv" or die "Failed to open new.csv for writing: $!"; $csv->print ($fh, \@row1); $csv->print ($fh, \@row2); close $fh or die "Failed to close new.csv: $!"; print " finished \n"; }
  • I've added the use of strict and warnings -- the Perl compiler should be happy with your code;
  • I'm declaring the variables at the point at which they're used, rather than at the top of the program;
  • I've expanded the error messages to be more informative; your version would show the same error for both open and close errors;
  • I've spaced things out to make the code more readable -- I'm a fan of blank lines to separate chunks of code;

You could get more cleanup by using perltidy -- I'm a fan of this utility.

Alex / talexb / Toronto

"Groklaw is the open-source mentality applied to legal research" ~ Linus Torvalds

Replies are listed 'Best First'.
Re^2: CSV_XS issue
by anaconda_wly (Scribe) on Apr 08, 2013 at 03:25 UTC
    Yes, thanks for the reformatting. I used to be c\c++ programmer. So I have some habbit incompatible with the Perl like the main and variable declaration and I'm in haste by the little snippet to find the correct usage. Good to know the perl style programming and your experience sharing. That's really cool to me!

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027371]
and God said, "Let Newton be!"...

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (8)
As of 2018-06-25 00:30 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (126 votes). Check out past polls.