jason0 has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

Most of the time, the scripts I write would benefit from using XS modules. For instance, there is (can be) a significant difference using Text::CSV_XS vs Text::CSV. I wrote a bit of code that will "use" Text::CSV_XS if available otherwise load Text::CSV. Often Text::CSV is installed in a system but not Text::CSV_XS.

my @modules=qw/Text::CSV_XS Text::CSV/; my $module; foreach (@modules) { if ( eval "require $_" ){ $module=$_; $module->import(); last; } }
Later, I use the following line:
my $csv=$module->new( { binary => 1, });
My question: is there already a way (such as someone else's module) to do this?

Thank you for your time!

--Jason

Replies are listed 'Best First'.
Re: preferred loading of XS modules
by toolic (Bishop) on Nov 25, 2015 at 18:23 UTC
    I think this is handled automatically for you. Text::CSV states:
    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.

      That's really awesome to hear about Text::CSV. The last time I really read the documentation was about ten years ago...funny that, eh?

      However, I only really used Text::CSV as my example. I was really wondering if there was a general way to instruct perl to use XS modules instead of their pure-perl versions.

Re: preferred loading of XS modules
by CountZero (Bishop) on Nov 25, 2015 at 20:35 UTC
    Module Text::CSV will take care of it automatically, but in general you could use the following:
    my @modules=qw/Module::One_XS Module::Two_PP/; foreach my $module (@modules) { eval qq| use $module |; last unless $@ }

    Update: As choroba astutely remarked the "next if $@" should be "last unless $@"

    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
      Didn't you mean
      last unless $@;

      ? Your code tries to use both the modules regardless of their availability.

      ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
        Indeed, you are right.

        Funny enough, my initial code had the "last unless $@" and then I changed it. Silly me.

        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
        my