summaryrefslogtreecommitdiffstats
path: root/contrib/perl5/lib/constant.pm
diff options
context:
space:
mode:
Diffstat (limited to 'contrib/perl5/lib/constant.pm')
-rw-r--r--contrib/perl5/lib/constant.pm14
1 files changed, 14 insertions, 0 deletions
diff --git a/contrib/perl5/lib/constant.pm b/contrib/perl5/lib/constant.pm
index 464e20c..5d3dd91 100644
--- a/contrib/perl5/lib/constant.pm
+++ b/contrib/perl5/lib/constant.pm
@@ -20,6 +20,18 @@ constant - Perl pragma to declare constants
print "This line does nothing" unless DEBUGGING;
+ # references can be declared constant
+ use constant CHASH => { foo => 42 };
+ use constant CARRAY => [ 1,2,3,4 ];
+ use constant CPSEUDOHASH => [ { foo => 1}, 42 ];
+ use constant CCODE => sub { "bite $_[0]\n" };
+
+ print CHASH->{foo};
+ print CARRAY->[$i];
+ print CPSEUDOHASH->{foo};
+ print CCODE->("me");
+ print CHASH->[10]; # compile-time error
+
=head1 DESCRIPTION
This will declare a symbol to be a constant with the given scalar
@@ -86,6 +98,8 @@ constants at compile time, allowing for way cool stuff like this.
print E2BIG, "\n"; # something like "Arg list too long"
print 0+E2BIG, "\n"; # "7"
+Errors in dereferencing constant references are trapped at compile-time.
+
=head1 TECHNICAL NOTE
In the current implementation, scalar constants are actually
OpenPOWER on IntegriCloud