Perl module error regarding "undefined routine"

I am trying to use a module called Math :: Counting :

#!/usr/bin/perl use strict; use warnings; use Math::Counting; my $f = factorial(3); print "$f\n"; 

When I run it, I get the following error

 $ perl UsingModules.pl Undefined subroutine &main::factorial called at UsingModules.pl line 8. 

The factorial function does not seem to be exported, but why?

When I used the following

 my $f = Math::Counting::factorial(3); 

instead of what was above, it works fine, but I am curious why the function cannot be exported.

I am using perl v5.10.1 for Cygwin.

+6
source share
6 answers

There is an error in the module. Math :: Counting ISA Exporter , but Math::Counting does not load Exporter .

Workaround: you can require or use Exporter manually.

Better: register an error with the author of the module, specify a test case.

A comment:

Oh, very interesting. The author of the module checks its functions, but Test::More pulls the Exporter , which means that this omission from the module source was not noticed.

Update:

Math :: Counting 0.0904 was released, solving this problem.

+7
source

Math :: Counting looks a little silly (I mean student and engineering modes?). The real factor function provided by the module, bfact is a thin wrapper around Math::BigInt::bfac . So just use Math :: BigInt .

 #!/usr/bin/env perl use strict; use warnings; use Math::BigInt(); print Math::BigInt->bfac('300'), "\n"; 

Conclusion:

  30605751221644063603537046129726862938858880417357699941677674125947653317671686
 74655152914224775733499391478887017263688642639077590031542268429279069745598412
 25476930271954604008012215776252176854255965356903506788725264321896264299365204
 57644883038890975394348962543605322598077652127082243763944912012867867536830571
 22936819436499564604981664502277165001851765464693401122260347297240663332585835
 06870150169794168850353752137554910289126407157154830282284937952636580145235233
 15693648223343679925459409527682060806223281238738388081704960000000000000000000000
 00000000000000000000000000000000000000000000000000000000000 

No, I did not confirm the result .

As others have noted, Math::Counting has:

 our @ISA = qw(Exporter); our @EXPORT = qw( factorial permutation combination bfact bperm bcomb ); 

but no require exporter .

In fact, there is no need for this module to inherit from Exporter. Plain:

 use Exporter 'import'; 

would be enough. In addition, by default, it is not necessary to pollute the user namespace of this module, so it should have:

 our @EXPORT = (); our @EXPORT_OK = qw( factorial permutation combination bfact bperm bcomb ); 

Otherwise, what is the %EXPORT_TAGS definition point?

+7
source

It seems that Math :: Counting is missing require Exporter; , therefore, none of its functions are exported to your namespace.

+2
source

After being warned by a good person who sent a bug report to CPAN regarding my forgotten require statement, I fixed the export of the module, including the comment on “namespace pollution”.

In addition, I added a note that this is a "thin shell" for Math::BigInt->bfac() for real-world applications, in documents. When I did this, I could not find simple calculations for permutation or combination. Now there are many ...

+2
source

It seems that the Math :: Counting module did not export the factor method when you called it using use Math::Counting .

The CPAN page does the following:

 use Math::Counting ':student'; 

and then the factorial method is exported to your namespace, and you can use it without adding the whole package name.

0
source

Take a look at the source code for Math :: Counting and see which version. You can find where the source is located:

 prompt> perldoc -l Math::Counting 

You can also find the module version in about 90% of cases by looking at the $VERSION module variable:

 use Math::Dumper; print "The version of Math::Dumper is $Math::Dumper::VERSION\n"; 

I just downloaded version 0.0902 and the following program works just fine:

 #! /usr/bin/env perl # use strict; use warnings; use feature qw(say); use Math::Counting; say $Math::Counting::VERSION; say factorial(6); 

In this version, I noticed:

 our @ISA = qw(Exporter); our @EXPORT = qw( factorial permutation combination bfact bperm bcomb ); 

So, it seems that the author automatically exports all of his routines in this particular version. The author also has two groups of export tags, defined :student and big .

It could have been in earlier versions, it did not have @EXPORT , but @EXPORT_OK used (which is preferable), and you should have done this:

 use Match::Counting qw(:student); 

or

 use Math::Counting qw(factorial); 
0
source

Source: https://habr.com/ru/post/914140/


All Articles