Monday, August 16, 2010

Act now to study coding

Work From Home
On Sep 21, 2009, at 12:24 AM, Rafael Garcia-Suarez wrote: 2009/9/20 Father Chrysostomos < spr ... @cpan.org >: $[ has now been deprecated in blead (by change #55b6781562aff32ef6499c4f263ab251254ca5cb). This is a useful feature, yet its use has long been discouraged. I suspect that this is because $ [ as a pragma was such a hack. So I would like to suggest that it be made a real pragma, seeing it has been deprecated with no replacement. Attached is a proof-of-concept, called arybase.pm, which is strikingly simple and short, but which relies on undocumented internals. Hence it is probably unwise to make it a standalone CPAN distribution. Could this be included in core (once IÂ've added tests, docs, etc.)? I think that the idea of this deprecation is to get rid of those undocumented internals, for which we can expect performance improvements. So far, no one has objected to the idea. There have only been suggestions as to how it should be implemented. What Mr. Pit suggests sounds good, but it would not actually be necessary until $^H{'${'} is removed. So, attached is a patch containing what I sent before, expanded with docs and tests. Is there any chance this could be applied? Father Chrysostomos diff -Nurp blead/MANIFEST bleadcopy/MANIFEST --- blead/MANIFEST 2009-09-20 13:50:11.000000000 -0700 +++ bleadcopy/MANIFEST 2009-09-22 12:56:15.000000000 -0700 @@ -3325,6 +3325,8 @@ keywords.pl Program to write keywords. lib/abbrev.pl An abbreviation table builder lib/AnyDBM_File.pm Perl module to emulate dbmopen lib/AnyDBM_File.t See if AnyDBM_File works +lib/arybase.pm Set base index for arrays ($[ replacement) +lib/arybase.t See if arybase works lib/assert.pl assertion and panic with stack trace lib/Benchmark.pm Measure execution time lib/Benchmark.t See if Benchmark works diff -Nurp blead/lib/arybase.pm bleadcopy/lib/arybase.pm --- blead/lib/arybase.pm 1969-12-31 16:00:00.000000000 -0800 +++ bleadcopy/lib/arybase.pm 2009-09-22 14:06:46.000000000 -0700 @@ -0,0 +1,60 @@ +# If support for $^H{ '$[' } is ever removed, see +# http://www.nntp.perl.org/group/perl.perl5.porters/2009/09/msg151052.html +# for an explanation of how this could be implemented in XS. + +package arybase; +use 5.01; +$VERSION='1'; +$hint_bits = 0x10; # This bit indicates that the array base is not 0. +sub import{ + $_[1] == 0 and goto &unimport; + $^H |= $hint_bits; + $^H{'$['} = 0+$_[1]; # We have to numify it for some reason. + return; +} +sub unimport { + $^H &= ~$hint_bits; + return; +} +__THE__=>__END__ + +=head1 NAME + +arybase - Set the base for arrays and substrings in Perl + +=head1 SYNOPSIS + + @array = ('one', 'two', 'three'); + + use arybase "1"; + print $array[2], "\n"; # prints "two" + + no arybase; + print $array[2], "\n"; # prints "three" + + { + use arybase +1; + print $array[1], "\n"; # prints "one"; + } + # array base is back to 0 + + use arybase 1; # WRONG! (version check) + use arybase +1; # OK + use arybase "1"; # also OK + +=head1 DESCRIPTION + +C allows one to set the index of the first element in an array +and of the first character in a substring. The default is 0, but you may +set it to 1 to make Perl behave more like auk (or Fortran) when +subscripting and when evaluating the C and C functions. + +This is a replacement for assignment to the C<$[> variable (see +L), which is deprecated as of perl 5.11.0. + +If you really want to make it hard for others to read your code, just put +C at the top of your code. + +=head1 SEE ALSO + +L, L diff -Nurp blead/lib/arybase.t bleadcopy/lib/arybase.t --- blead/lib/arybase.t 1969-12-31 16:00:00.000000000 -0800 +++ bleadcopy/lib/arybase.t 2009-09-22 14:06:51.000000000 -0700 @@ -0,0 +1,27 @@ +#!./perl + +BEGIN { + chdir '..' if -d '../pod' && -d '../t'; + @INC = 'lib'; +} + +use Test::More tests => 7; + +@array = ("one","two","three"); +is $array[1], "two", 'just make sure things are working to begin with'; + +use arybase 1; # version check +is $array[1], "two", 'version check is just a version check'; + +use arybase +1; +is $array[1], "one", 'use arybase with +num'; +is "$[", 1, 'arybase sets $['; + +no arybase; +is $array[1], "two", 'no arybase'; + +{ + use arybase "42"; + is $array[44], 'three', 'use arybase with quotes around number'; +} +is $#array, 2, 'arybase is lexical'; diff -Nurp blead/pod/perl5110delta.pod bleadcopy/pod/perl5110delta.pod --- blead/pod/perl5110delta.pod 2009-09-15 02:10:23.000000000 -0700 +++ bleadcopy/pod/perl5110delta.pod 2009-09-22 13:29:05.000000000 -0700 @@ -376,6 +376,10 @@ time. It provides the key feature of C + +This is a lexically-scoped replacement for the C<$[> variable. + =back =head2 Pragmata Changes @@ -1945,7 +1949,7 @@ emulate setuid permission bits on system =item * -Deprecate assignment to $[ +Assignment to $[ has now been deprecated. See C for a replacement. =item * diff -Nurp blead/pod/perldiag.pod bleadcopy/pod/perldiag.pod --- blead/pod/perldiag.pod 2009-09-13 11:17:37.000000000 -0700 +++ bleadcopy/pod/perldiag.pod 2009-09-22 13:29:55.000000000 -0700 @@ -4685,7 +4685,7 @@ returns no useful value. See L =item Use of assignment to $[ is deprecated (D deprecated) The C<$[> variable (index of the first element in an array) -is deprecated. See L. +is deprecated. See L. See also C for a replacement. =item Use of bare << to mean <<"" is deprecated diff -Nurp blead/pod/perlvar.pod bleadcopy/pod/perlvar.pod --- blead/pod/perlvar.pod 2009-09-07 04:40:05.000000000 -0700 +++ bleadcopy/pod/perlvar.pod 2009-09-22 13:32:19.000000000 -0700 @@ -1037,14 +1037,12 @@ subscripting and when evaluating the ind As of release 5 of Perl, assignment to C<$[> is treated as a compiler directive, and cannot influence the behavior of any other file. -(That's why you can only assign compile-time constants to it.) Its -use is deprecated, and will trigger a warning (if the deprecation -L category is enabled. You did C, right?) +(That's why you can only assign compile-time constants to it.) -Note that, unlike other compile-time directives (such as L), -assignment to C<$[> can be seen from outer lexical scopes in the same file. -However, you can use local() on it to strictly bind its value to a -lexical block. +As of perl 5.11.0, its +use is deprecated, and will trigger a warning (if the deprecation +L category is enabled. You did C, right?). See +C for a replacement. =item $] X<$]>