When do I need to define a function with "use POSIX qw(.....)" and when not ?

Discussion in 'Perl Misc' started by Thomas Blabb, Dec 12, 2007.

  1. Thomas Blabb

    Thomas Blabb Guest

    I saw sometimes perl scripts where functions are defined in the POSIX line and others where not.
    Example:

    When should I write:

    use POSIX qw(strftime);
    ..... strftime ..... ;

    and when simply:

    use POSIX;
    ..... strftime ..... ;

    Tom
    Thomas Blabb, Dec 12, 2007
    #1
    1. Advertising

  2. Thomas Blabb <> wrote:
    > I saw sometimes perl scripts where functions are defined in

    ^^^^^^^
    > the POSIX line and others where not.



    That is NOT where the function is defined.

    The function is defined in the POSIX.pm module.


    > Example:
    >
    > When should I write:
    >
    > use POSIX qw(strftime);
    > .... strftime ..... ;



    Here only one function from POSIX.pm is imported into your program's
    namespace.


    > and when simply:
    >
    > use POSIX;
    > .... strftime ..... ;



    Here every function in POSIX.pm's @EXPORT is imported into your program's
    namespace.


    The first is better Software Engineering, the second is more convenient.


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Dec 12, 2007
    #2
    1. Advertising

  3. Thomas Blabb

    Ted Zlatanov Guest

    On Wed, 12 Dec 2007 12:04:47 GMT Tad J McClellan <> wrote:

    TJM> Thomas Blabb <> wrote:
    >> use POSIX qw(strftime);
    >> use POSIX;


    TJM> The first is better Software Engineering, the second is more convenient.

    (This is similar to "import package.class" vs. "import package.*" in
    Java, which is also a matter of taste...)

    I wonder if the first really is better. I can think of two problems
    with a full import:

    1) memory usage
    2) name conflicts

    The latter seems bad until you realize none of your code should be
    overriding POSIX functions anyhow. For the former, these are C
    interfaces so they shouldn't take up too much memory, right? What other
    problems are there? I'm curious about this so please give me your
    opinions.

    Ted
    Ted Zlatanov, Dec 12, 2007
    #3
  4. Re: When do I need to define a function with "use POSIX qw(.....)"and when not ?

    On Wed, 12 Dec 2007 07:02:09 -0600, Ted Zlatanov wrote:

    > I wonder if the first really is better. I can think of two problems
    > with a full import:
    >
    > 1) memory usage


    AFAIK if you don't explicitly import specific functions, they'll only be
    loaded when you actually use them. See AutoLoader. This does slow down
    the first call to such a function a bit.

    > 2) name conflicts


    Also, POSIX.pm is *large*. By explicitly importing the functions you use
    it's easy to find where they're coming from without having to look
    anywhere outside the file that uses them or having to rember that
    isxdigit is part of POSIX.pm.

    Joost.
    Joost Diepenmaat, Dec 12, 2007
    #4
  5. Thomas Blabb

    Ben Morrow Guest

    Re: When do I need to define a function with "use POSIX qw(.....)"and when not ?

    Quoth Joost Diepenmaat <>:
    > On Wed, 12 Dec 2007 07:02:09 -0600, Ted Zlatanov wrote:
    >
    > > I wonder if the first really is better. I can think of two problems
    > > with a full import:
    > >
    > > 1) memory usage

    >
    > AFAIK if you don't explicitly import specific functions, they'll only be
    > loaded when you actually use them. See AutoLoader. This does slow down
    > the first call to such a function a bit.


    AutoLoading only happens on first call, regardless of imports:

    ~/src/perl% ktrace perl -MPOSIX=errno
    -e'open my $F, ">", "test-errno"; errno;'
    ~/src/perl% kdump -tn | grep errno
    2127 perl NAMI "test-errno"
    2127 perl NAMI "/usr/local/lib/perl5/5.8.8/mach/auto/POSIX/errno.al"
    2127 perl NAMI "/usr/local/lib/perl5/5.8.8/mach/auto/POSIX/errno.al"
    2127 perl NAMI "/usr/local/lib/perl5/5.8.8/mach/auto/POSIX/errno.al"

    perl doesn't even look for the .al until after it's opened the file.
    Whether functions are AutoLoaded or not depends entirely on the module:
    most of POSIX's useful functions (including strftime) are written in C,
    so they can't be, except insofar as your OS will only page in the
    relevant parts of the .so when they are needed :).

    Ben
    Ben Morrow, Dec 12, 2007
    #5
  6. Ted Zlatanov <> wrote:
    > On Wed, 12 Dec 2007 12:04:47 GMT Tad J McClellan <> wrote:
    >
    > TJM> Thomas Blabb <> wrote:
    >>> use POSIX qw(strftime);



    This declares that strftime() is being imported into the program.


    >>> use POSIX;



    This "smuggles" strftime() (and bunch of others) into the program.


    > TJM> The first is better Software Engineering, the second is more convenient.
    >
    > (This is similar to "import package.class" vs. "import package.*" in
    > Java, which is also a matter of taste...)
    >
    > I wonder if the first really is better. I can think of two problems
    > with a full import:
    >
    > 1) memory usage
    > 2) name conflicts



    I had name conflicts in mind when I posted my followup.


    > The latter seems bad until you realize none of your code should be
    > overriding POSIX functions anyhow.



    My comment was not (meant to be) specific to the POSIX module.

    Explicit imports are better than implicit imports.

    And the potential for name conflicts is not even the biggest reason
    IMNSHO. The biggest reason is so that I don't have to go searching
    for where some function is defined, I can just look at the "use"
    lines to figure out where it came from.

    Determining origin is easier when "declared" rather than "smuggled".


    (No, I am not a Customs Agent, I don't even play one on TV)


    --
    Tad McClellan
    email: perl -le "print scalar reverse qq/moc.noitatibaher\100cmdat/"
    Tad J McClellan, Dec 13, 2007
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. theotyflos
    Replies:
    3
    Views:
    450
    Thomas Matthews
    Feb 19, 2004
  2. FAN
    Replies:
    2
    Views:
    299
    bruno modulix
    Sep 9, 2005
  3. robin liu
    Replies:
    3
    Views:
    810
    Robin Liu
    Apr 21, 2006
  4. Brian Takita

    #define _ and #define __

    Brian Takita, Jan 23, 2006, in forum: Ruby
    Replies:
    0
    Views:
    445
    Brian Takita
    Jan 23, 2006
  5. john.swilting

    use POSIX qw(ceil floor)

    john.swilting, Apr 21, 2007, in forum: Perl Misc
    Replies:
    2
    Views:
    160
    john.swilting
    Apr 26, 2007
Loading...

Share This Page