Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^4: Beware of POSIX constants (sorta)

by tye (Sage)
on Oct 04, 2005 at 02:19 UTC ( [id://497113]=note: print w/replies, xml ) Need Help??


in reply to Re^3: Beware of POSIX constants (sorta)
in thread Beware of POSIX constants

Yes, I don't think Perl Mouse was suggesting it would get a prototype. But so what? Would adding a prototype solve any problems? Usually, the entire program has been compiled at that point already, so adding a prototype doesn't gain anything.

Yes, I don't think tye was suggesting that just adding a prototype would be enough to solve any problem. But so what? Would defining the function earlier solve any problems? The function doesn't have a prototype so defining it early doesn't gain anything. (:

But, in fact, you don't have to define the function earlier. You can still use "POSIX's autoloading behaviour" to delay the defining of the function's body, you just have to fix the exporting of the function to export a prototype (by declaring the function that you haven't defined yet).

Here's an example of how to do that making the bold assumption that all POSIX exported items with ALL_CAPS names are supposed to be constants (and mostly ignoring POSIX's current implementation for the sake of making a quick demonstration):

BEGIN { require POSIX; package POSIX; my $import= \&POSIX::import; *import= sub { warn "import( @_ )\n"; my( $pkg )= @_; eval "sub $_(); 1" || warn "$@" for grep /^[A-Z_]+$/, @_[1..$#_]; goto &$import; }; my $auto= \&POSIX::AUTOLOAD; *AUTOLOAD= sub { if( $AUTOLOAD =~ /^[A-Z_:]+$/ ) { warn "Auto-loading $AUTOLOAD.\n"; my $val = constant( ( split /::/, $AUTOLOAD )[-1] ); if( $! == 0 ) { goto &$AUTOLOAD if eval "sub $AUTOLOAD() { $val }; 1"; warn "$@"; } } goto &$auto; }; } use POSIX 'CHAR_MAX'; print "CHAR_MAX(", prototype("CHAR_MAX"), ")\n"; print CHAR_MAX+1, $/;

which outputs:

$ perl posix.pl import( POSIX CHAR_MAX ) CHAR_MAX() Auto-loading POSIX::CHAR_MAX. 128

Which shows that Perl was forced to autoload the function after the line that prints the prototype and yet the problem is fixed.

- tye        

Replies are listed 'Best First'.
Re^5: Beware of POSIX constants (sorta)
by Perl Mouse (Chaplain) on Oct 04, 2005 at 08:44 UTC
    Currently, POSIX doesn't require you to import anything - and I think your solution does. Furthermore, your solution fixes the problem of the missing prototype at the cost of why POSIX's constants don't have prototypes: in order to reduce the overhead of 'use POSIX', functions are autoloaded - you don't pay compile costs for functions you don't need. Your solution does a string eval for each all caps function. Then you might as well have listed all of them in POSIX.pm.
    Perl --((8:>*
      [...] your solution [...]

      ...was a "quick demonstration". Your solutions... oh... you don't have a solution. Just trying to prop up your claim that autoloading is the source of the problem? (:

      Note also that you can have POSIX export real constants but have the autoloaded ones in POSIX:: not require predeclaration. That would mean that the only "performance hit" is for people who do "use POSIX;", who are already getting a "performance hit" for importing hundreds of symbols. But even predeclaring all several hundred POSIX constants as actually being constants takes a fraction of a 1/100th of a second on my lowly PC, so I don't see this as a big problem that needs to be avoided.

      - tye        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://497113]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (3)
As of 2024-03-29 15:00 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found