Re: Switching Modules as Necessary
by CountZero (Bishop) on Dec 08, 2014 at 10:20 UTC
|
Text::CSV already checks if Text:/CSV_XS is available and falls back to Text:/CSV_PP if not.From the docs: Text::CSV provides facilities for the composition and decomposition of comma-separated values using Text::CSV_XS or its pure Perl version.
CountZero A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James My blog: Imperial Deltronics
| [reply] |
Re: Switching Modules as Necessary
by Eily (Monsignor) on Dec 08, 2014 at 10:22 UTC
|
The pure perl implementation is Text::CSV_PP, Text::CSV is actually a wrapper that uses the XS version by default, or the pure perl version as fallback. The module's documentation states:
When you use Text::CSV, it calls a backend worker module - Text::CSV_XS or Text::CSV_PP. By default, Text::CSV tries to use Text::CSV_XS which must be complied and installed properly. If this call is fail, Text::CSV uses Text::CSV_PP.
Edit: Too slow, I agree with CountZero then :)
| [reply] |
|
Well, now I'm confused... I found that CSV_XS wasn't installed, couldn't install it, so tried CSV, but never went near PP. I can only assume that PP was already installed. Or perhaps CSV installs CSV_PP by default? CSV_PP doesn't appear to be a core module - at least it's not on my linux box or my Win XP machine.
That aside, I'm still a little puzzled. I could presumably have a situation where I've got both CSV_XS and CSV_PP installed, but my script would now fail due to lack of the CSV wrapper? (which makes it seem like the wrapper could be more trouble than it's worth)
Edit to add: It looks like Text::CSV_PP is included when you install Text::CSV, so that makes sense... However, this still means I want to run my own test for CSV_XS, otherwise my script will fail when CSV_XS is there if CSV is not installed... Have I got that right?
map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2
-$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk
| [reply] [d/l] |
|
Since the pure perl is the fallback solution, and it does not require anything but perl (and well, if you're trying to install Text::CSV you probably have perl on your system), it's not really surprising to see CSV_PP is provided with Text::CSV (it's not installed at the same time, it's part of the module. So you can't have CSV_PP without Text::CSV being installed (or it was done on purpose for some reason)).
I don't see anything preventing Text::CSV_XS being installed without Text::CSV, so indeed, this might be an issue. But Text::CSV with its PP implementation is a fairly simple module: only to .pm files, without any compilation needed (that's actually why it exists, in case the compilation for XS fails), so it should be easy to install even "by hand" (unzip and perl Makefile.PL; make; make test; make install), or even just include without any installation in your program. Besides, its main benefit is that the implementation is abstracted away, and you get one unique interface for all your calls, so no my $csv = Text::CSV_PP->new vs my $csv = Text::CSV_XS->new, you just use Text::CSV and everything in the Text::CSV namespace will point to the right element, which was your second problem :) .
| [reply] [d/l] [select] |
Re: Switching Modules as Necessary
by Corion (Patriarch) on Dec 08, 2014 at 09:38 UTC
|
Package names are just strings, so I would approach the problem as follows:
use vars qw($csv_class);
if( $i_want_to_use_XS ) {
require Text::CSV_XS;
$csv_class= 'Text::CSV_XS';
} else {
require Text::CSV;
$csv_class= 'Text::CSV';
};
my $csv= $csv_class->new(...);
| [reply] [d/l] |
Re: Switching Modules as Necessary
by choroba (Cardinal) on Dec 08, 2014 at 09:41 UTC
|
use if 'aix' eq $^O, Text::CVS;
use if 'aix' ne $^O, Text::CVS_XS;
| [reply] [d/l] |
|
I'd considered using a simple variable (and some AIX boxes might have a perfectly good compiler), but I wondering if there was a way of actually testing for which module was available - e.g. use Text:CSV_XS||use Text:CSV||die "no CSV module available";
Hmm... I wonder if I could do this in an eval... (scurries off to try)
map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2
-$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk
| [reply] [d/l] |
|
print eval{ require version; 1; } ? 'Found it!' : "T'ain't available!"
+;;
Found it!
print eval{ require Does::Not::Exist; 1; } ? 'Found it!' : "T'ain't av
+ailable!";;
T'ain't available!
With the rise and rise of 'Social' network sites: 'Computers are making people easier to use everyday'
Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
"Science is about questioning the status quo. Questioning authority".
In the absence of evidence, opinion is indistinguishable from prejudice.
| [reply] [d/l] |
Re: Switching Modules as Necessary
by Melly (Chaplain) on Dec 15, 2014 at 14:12 UTC
|
This was my solution - I realise that I could just 'insist' that at least Text::CSV is installed and then avoid the need for evals altogether, but I'm working in multiple environments with different requirements, so this seemed the best 'will always work if possible even if only CSV_XS is installed' solution:
# work out which module to use
my $csv_module;
if(eval{require Text::CSV_XS}){
$csv_module = 'Text::CSV_XS';
}
else{
$csv_module = 'Text::CSV';
}
eval("use $csv_module;");
map{$a=1-$_/10;map{$d=$a;$e=$b=$_/20-2;map{($d,$e)=(2*$d*$e+$a,$e**2
-$d**2+$b);$c=$d**2+$e**2>4?$d=8:_}1..50;print$c}0..59;print$/}0..20
Tom Melly, pm (at) cursingmaggot (stop) co (stop) uk
| [reply] [d/l] [select] |