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

T

Thomas Blabb

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
 
T

Tad J McClellan

Thomas Blabb said:
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.
 
T

Ted Zlatanov

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
 
J

Joost Diepenmaat

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.
 
B

Ben Morrow

Quoth Joost Diepenmaat said:
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
 
T

Tad J McClellan

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



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)
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,764
Messages
2,569,566
Members
45,041
Latest member
RomeoFarnh

Latest Threads

Top