GSP
Quick Navigator

Search Site

Unix VPS
A - Starter
B - Basic
C - Preferred
D - Commercial
MPS - Dedicated
Previous VPSs
* Sign Up! *

Support
Contact Us
Online Help
Handbooks
Domain Status
Man Pages

FAQ
Virtual Servers
Pricing
Billing
Technical

Network
Facilities
Connectivity
Topology Map

Miscellaneous
Server Agreement
Year 2038
Credits
 

USA Flag

 

 

Man Pages
constant::lexical(3) User Contributed Perl Documentation constant::lexical(3)

constant::lexical - Perl pragma to declare lexical compile-time constants

2.0003

  use constant::lexical DEBUG => 0;
  {
          use constant::lexical PI => 4 * atan2 1, 1;
          use constant::lexical DEBUG => 1;

          print "Pi equals ", PI, "...\n" if DEBUG;
  }
  print "just testing...\n" if DEBUG; # prints nothing
                                        (DEBUG is 0 again)
  use constant::lexical \%hash_of_constants;
  use constant::lexical WEEKDAYS => @weekdays; # list

  use constant::lexical { PIE        => 4 * atan2(1,1),
                          CHEESECAKE => 3 * atan2(1,1),
                         };

This module creates compile-time constants in the manner of constant.pm, but makes them local to the enclosing scope.

I sometimes use these for objects that are blessed arrays, which are faster than hashes. I use constants instead of keys, but I don't want them exposed as methods, so this is where lexical constants come in handy.

This module requires perl 5.8.0 or later. If you are using a version of perl lower than 5.22.0, then you will need one of the following modules, which you can get from the CPAN:
  • For perl 5.12.0 and higher: Lexical::Sub
  • For lower perl versions: Sub::Delete

The following three bugs have been fixed for perl 5.12.0 and higher, but are still present for older versions of perl:
  • These constants are no longer available at run time, so they won't work in a string "eval" (unless, of course, the "use" statement itself is inside the "eval").
  • These constants actually are accessible to other scopes during compile-time, as in the following example:

      sub foo { print "Debugging is on\n" if &{'DEBUG'} }
      {
              use constant::lexical DEBUG => 1;
              BEGIN { foo }
      }
        
  • If you switch to another package within a constant's scope, it (the constant) will apparently disappear.

If you find any other bugs, please report them to the author via e-mail.

The idea of using objects in "%^H" (in the pre-5.10 code) was stolen from namespace::clean. The idea of doing cleanup in a DELETE method on a tied field hash (in the 5.10 code) was likewise stolen from namespace::clean.

Some of the code for the perl 5.12.0+ version is plagiarised from constant.pm by Tom Phoenix.

Copyright (C) 2008, 2010, 2012, 2016 Father Chrysostomos (sprout at, um, cpan dot org)

This program is free software; you may redistribute or modify it (or both) under the same terms as perl.

constant, Sub::Delete, namespace::clean, Lexical::Sub

Hey! The above document had some coding errors, which are explained below:
Around line 248:
Non-ASCII character seen before =encoding in '‘package’'. Assuming UTF-8
2016-06-19 perl v5.32.1

Search for    or go to Top of page |  Section 3 |  Main Index

Powered by GSP Visit the GSP FreeBSD Man Page Interface.
Output converted with ManDoc.