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

Hi monks,

I am a novice perl user. I ahve installed Active perl. Now I want to make use of the pow function which is actually present in d:\perl\lib\posix.pm as well as d:\perl\lib\auto\POSIX\pow.al where d:\perl is my installation directory.

now i write a single one line statement in 1.pl

$b = pow(2,3).

on execution of 1.pl it says "Undefined subroutine &main::pow called at 1.pl line 1."

now what should i do to access the pow subroutine. my @INC array shows that d:\perl\lib as well as d:\perl\site\lib is being used. can someone suggest a method to access all the subroutines that comes with the standard lib provided by activeperl i.e all subroutines found in the d:\perl\lib.

thank you

Edited by planetscape - added code tags and rudimentary formatting

Replies are listed 'Best First'.
Re: standard perl routine
by davido (Cardinal) on Apr 30, 2006 at 07:21 UTC

    You have to tell perl to actually use the POSIX module. It's not enough to just put it in your @INC path, you have to specifically tell Perl to use it, as follows:

    #!/usr/bin/perl use strict; use warnings; use POSIX; # This line loads the POSIX module and imports pow(). print pow( 2, 3 ); # See? Now it works.

    By the way: my $number = pow( 2, 3 ); could be written like this instead: my $number = 2 ** 3;, and that wouldn't require the use of the POSIX module.


    Dave

      But is there a way by which i can ,sort of autoload all the standard subroutines instead of individually specifying them.
        Since there is no agreement on what "all the standard subroutines" should contain, there is no way you can autoload them.

        Also it would be quite rare that you need "all the standard subroutines" (whatever they may be) in the same script.

        CountZero

        "If you have four groups working on a compiler, you'll get a 4-pass compiler." - Conway's Law

Re: standard perl routine
by Cody Pendant (Prior) on Apr 30, 2006 at 07:59 UTC
    Any particular reason why you're using the POSIX routine instead of just the normal perl "2 ** 3" syntax?


    ($_='kkvvttuu bbooppuuiiffss qqffssmm iibbddllffss')
    =~y~b-v~a-z~s; print
Re: standard perl routine
by BrowserUk (Patriarch) on Apr 30, 2006 at 07:29 UTC

    You need to use use.

    use POSIX qw[ pow ]; print pow(2,3); 8 print pow( 1000, 0.33333333333333333 );; 10

    Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
    Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
    "Science is about questioning the status quo. Questioning authority".
    In the absence of evidence, opinion is indistinguishable from prejudice.

      If I understand the docs correctly, POSIX automatically exports pretty much everything except for subroutine names that conflict with Perl built-ins. If you wish to protect your namespace, the easiest way to override this unfortunate behavior is to invoke POSIX like this:

      use POSIX (); # Import nothing. print POSIX::pow( 2, 3 ); # Use the fully-qualified name.

      Dave

        Specifying an import list explicitly prevents POSIX from importing anything other than the functions you specify:

        c:\test\546536>p1 print scalar keys %main::;; ## Before use POSIX main has how many glob +als? 67 use POSIX qw[ pow ];; print scalar keys %main::;; ## and after is has 72 Terminating on signal SIGINT(2) c:\test\546536>p1 $h{ $_ }++ for keys %main::;; ## Record what we have before use POSIX qw[ pow ];; $h{ $_ }++ for keys %main::;; ## Again after print for grep{ $h{ $_ } == 1 } keys %h;; ## Display the differences _<c:/Perl/lib/auto/POSIX/POSIX.dll XSLoader:: POSIX:: pow _<POSIX.c

        Two 'private' paths, two new namespaces and one new function (pow) in main.


        Examine what is said, not who speaks -- Silence betokens consent -- Love the truth but pardon error.
        Lingua non convalesco, consenesco et abolesco. -- Rule 1 has a caveat! -- Who broke the cabal?
        "Science is about questioning the status quo. Questioning authority".
        In the absence of evidence, opinion is indistinguishable from prejudice.
Re: standard perl routine
by swampyankee (Parson) on Apr 30, 2006 at 15:31 UTC

    This may be a silly question: but why do you want to use the POSIX pow function when Perl has a perfectly good built-in exponentiation operator? Go to perlop and look for "Exponentiation."

    emc

    "Being forced to write comments actually improves code, because it is easier to fix a crock than to explain it. "
    —G. Steele