djerius has asked for the wisdom of the Perl Monks concerning the following question:
My module doesn't care which YAML is around, just that it can Dump and Load.
I'd like to make it YAML module agnostic, as there are several out there.
There's YAML::Any, but that ships with YAML, and what if that's not installed? This works; but it ain't pretty:
Is there a more refined approach?use List::Util qw[ first ]; my $class = first { eval "use $_; 1" } qw[ YAML::XS YAML::Syck YAML::T +iny YAML];
Thanks!
Update 1: fixed code to stop after the first successful load.
Update 2: code is even better now
Update 3: The code should be polite and use an already loaded module, if possible.
use List::Util qw[ first ]; use Class::Load qw[ load_first_existing_class is_class_loaded ]; my @CLASSES = qw[ YAML::Tiny YAML::XS YAML::Syck YAML ]; my $class = ( first { is_class_loaded $_ } @CLASSES ) || ( load_first_existing_class @CLASSES );
|
---|
Replies are listed 'Best First'. | |
---|---|
Re: What's the best way to use Any YAML (not YAML::Any)
by kcott (Archbishop) on Mar 15, 2014 at 05:46 UTC | |
by djerius (Beadle) on Mar 15, 2014 at 16:00 UTC | |
by kcott (Archbishop) on Mar 15, 2014 at 21:35 UTC | |
by djerius (Beadle) on Mar 17, 2014 at 17:56 UTC | |
Re: What's the best way to use Any YAML (not YAML::Any)
by CountZero (Bishop) on Mar 15, 2014 at 12:52 UTC | |
by djerius (Beadle) on Mar 15, 2014 at 16:32 UTC | |
by CountZero (Bishop) on Mar 17, 2014 at 19:35 UTC | |
by djerius (Beadle) on Mar 18, 2014 at 01:27 UTC |
Back to
Seekers of Perl Wisdom