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

Hi Monks,
I am trying to use some of Perl's built-in statistical functions, namely to calculate standard deviation and mean of a list.
I install the two modules:
install Statistics::Basic::Median Statistics::Basic::Median is up to date (undef). install Statistics::Basic::StdDev Statistics::Basic::StdDev is up to date (undef).

however this code:
use Statistics::Basic::StdDev; my $stddev = stddev(1,2,3); print $stddev."\n";

returns:
Undefined subroutine &main::stddev called at test.pl line 2.

What am I doing wrong?

Replies are listed 'Best First'.
Re: Annoying error although module is installed
by tangent (Parson) on Feb 28, 2016 at 22:49 UTC
    stddev() is not a built-in function, it is a function provided by the module. From looking at the docs that module does not export any functions by default so you will need to import them before using them:
    # you can remove this line use Statistics::Basic::StdDev; # add this line use Statistics::Basic qw(:all);
    You can then use all the functions in your script.
Re: Annoying error although module is installed
by GrandFather (Saint) on Feb 29, 2016 at 00:43 UTC

    In addition to the import techniques described above you may instead specify the module in the call:

    use Statistics::Basic::StdDev; my $stddev = Statistics::Basic::StdDev::stddev(1,2,3); print $stddev."\n";

    which helps maintenance in a large script by making it clear where stddev comes from.

    Premature optimization is the root of all job security
Re: Annoying error although module is installed
by 1nickt (Canon) on Feb 28, 2016 at 22:48 UTC

    You're not importing the method before you use it.

    use Statistics::Basic::StdDev 'stddev';
    Untested... could also be:
    use Statistics::Basic 'stddev';

    Hope this helps!

    edit: tangent's answer is more complete.


    The way forward always starts with a minimal test.
      Thank you very much!