Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Perl > Perl Misc > When do I need to define a function with "use POSIX qw(.....)" and when not ?

Reply
Thread Tools

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

 
 
Thomas Blabb
Guest
Posts: n/a
 
      12-12-2007
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

 
Reply With Quote
 
 
 
 
Tad J McClellan
Guest
Posts: n/a
 
      12-12-2007
Thomas Blabb <(E-Mail Removed)> 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/"
 
Reply With Quote
 
 
 
 
Ted Zlatanov
Guest
Posts: n/a
 
      12-12-2007
On Wed, 12 Dec 2007 12:04:47 GMT Tad J McClellan <(E-Mail Removed)> wrote:

TJM> Thomas Blabb <(E-Mail Removed)> 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
 
Reply With Quote
 
Joost Diepenmaat
Guest
Posts: n/a
 
      12-12-2007
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.

 
Reply With Quote
 
Ben Morrow
Guest
Posts: n/a
 
      12-12-2007

Quoth Joost Diepenmaat <(E-Mail Removed)>:
> 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

 
Reply With Quote
 
Tad J McClellan
Guest
Posts: n/a
 
      12-13-2007
Ted Zlatanov <(E-Mail Removed)> wrote:
> On Wed, 12 Dec 2007 12:04:47 GMT Tad J McClellan <(E-Mail Removed)> wrote:
>
> TJM> Thomas Blabb <(E-Mail Removed)> 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/"
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
About typedef -- define the function pointer or define function model? robin liu C Programming 3 04-21-2006 03:26 PM
#define _ and #define __ Brian Takita Ruby 0 01-23-2006 04:34 AM
Posix Thread : C++ : poiinter to Member function raxitsheth@gmail.com C++ 2 05-13-2005 02:38 PM
Creating POSIX threads with a class function Christian Buckl C++ 6 10-08-2004 02:33 PM
How to define a define that defines some defines ? theotyflos C Programming 3 02-19-2004 05:07 PM



Advertisments