X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=lib%2Fconstant.pm;h=5d3dd91b46f94f01999fd9eed46aab737dfe0ed8;hb=4499d6b9d14594604210bc28005c5e4ab2e49947;hp=464e20cd91d0ec64543da8a63c38ffcc6f08ec2d;hpb=a3cb178b0bad32fa8be934503d051b96a3cb1fea;p=p5sagit%2Fp5-mst-13.2.git diff --git a/lib/constant.pm b/lib/constant.pm index 464e20c..5d3dd91 100644 --- a/lib/constant.pm +++ b/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