Daniel Muey


first - use the first loadable module in a list


  use first 'YAML::Syck', 'YAML::TINY', 'YAML';
  if( $first::module ) {
      print "Looks like I'll be using $first::module for this YAML..."
  else {
      die "I have no YAML modules: $@";
  my $yaml = $first::module->new();
  use first 'CGI::Simple', 'CGI::Minimal', 'CGI';
  my $cgi = $first::module ? $first::module->new() : $fallback_obj;


Two main circumstances I've encountered where this is useful is:

  • when you have a list of modules that have the same interface but are more desirable for one reason or another like speed, portability, or availability.

        use first 'CGI::Simple', 'CGI::Minimal', 'CGI';
  • when you have a list of modules that do the same task but via different methods

        use first 'YAML::Syck', 'YAML::TINY', 'YAML', 'XML::Tiny', 'XML::Simple', 'Storable';
        my $serializer = $first::module;
        # now use functions based on $serializer / $first::module, perhaps keeping it in a hash that maps funtions to the name space for a consistent API where none existed before


Arguments after 'use first' can be a name space string or an array reference whose first item is a name space and the rest is what would get passed to/after 'use Name::Space'


These variables are available after 'use first' and are reset upon each call of 'use first' (Similar to how $@ is reset with every eval)


Contains the namespace that was loaded, if any. undefined otherwise if none could be loaded.


Is a hashref whose keys are the name space that could not be loaded and the values are the given key's error message.


Contains the last error, if any.


last, any, all, fake


More tests as per first.t


Daniel Muey, http://drmuey.com/cpan_contact.pl


Copyright (C) 2007 by Daniel Muey

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself, either Perl version 5.8.6 or, at your option, any later version of Perl 5 you may have available.

Hosting generously
sponsored by Bytemark