Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

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


Comment on Re: CSV_XS issue
Select or Download Code
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?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1027371]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (7)
As of 2014-12-20 00:14 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (94 votes), past polls