PageRenderTime 26ms CodeModel.GetById 22ms RepoModel.GetById 0ms app.codeStats 0ms

/lib/Math/ToMath/Parser.pm

https://github.com/a-maier/mathtomath
Perl | 61 lines | 53 code | 4 blank | 4 comment | 2 complexity | 459ec6020c0995c9d1dd1bdeb7d5dc75 MD5 | raw file
  1. package Math::ToMath::Parser;
  2. use strict;
  3. use warnings;
  4. use constant Parser => __PACKAGE__;
  5. # Export the 'Parser' constant
  6. use parent 'Exporter';
  7. our @EXPORT_OK = qw(Parser);
  8. # automatically load all contained parsers
  9. # and return a list of them using the "get_parsers" class method
  10. use Module::Pluggable (
  11. search_path => 'Math::ToMath::Parser',
  12. sub_name => '_get_parsers',
  13. inner => 0,
  14. );
  15. # This wrapper makes sure that Generic comes first in the list
  16. sub get_parsers {
  17. my $class = shift;
  18. my @parsers = $class->_get_parsers(@_);
  19. foreach my $parser (@parsers) {
  20. if (not eval "require $parser; 1;") {
  21. my $err = $@ || "Zombie error";
  22. die "Couldn't require '$parser': $err";
  23. }
  24. }
  25. my $generic = __PACKAGE__ . "::Generic";
  26. my @nongeneric = grep $_ ne $generic, @parsers;
  27. if (@nongeneric == @parsers) {
  28. return @parsers;
  29. }
  30. else {
  31. return($generic, @nongeneric);
  32. }
  33. }
  34. sub get_parser {
  35. my $class = shift;
  36. my $name = shift;
  37. my @matching = grep /::\Q$name\E$/, $class->get_parsers
  38. or die "Found no parser for $name\n";
  39. return $matching[0];
  40. }
  41. 1;
  42. =head1 AUTHOR
  43. Andreas Maier, E<lt>email@addressE<gt>
  44. =head1 COPYRIGHT AND LICENSE
  45. Copyright (C) 2010-2011 by Andreas Maier
  46. This library is free software; you can redistribute it and/or modify
  47. it under the same terms as Perl itself, either Perl version 5.10.1 or,
  48. at your option, any later version of Perl 5 you may have available.
  49. =cut