This is a live mirror of the Perl 5 development currently hosted at https://github.com/perl/perl5
perlfunc: re-document old split() @_ side effect
[perl5.git] / lib / English.pm
index f38c313..e40cc12 100644 (file)
@@ -1,7 +1,9 @@
 package English;
 
+our $VERSION = '1.10';
+
 require Exporter;
-@ISA = (Exporter);
+@ISA = qw(Exporter);
 
 =head1 NAME
 
@@ -10,6 +12,9 @@ English - use nice English (or awk) names for ugly punctuation variables
 =head1 SYNOPSIS
 
     use English;
+    use English qw( -no_match_vars ) ;  # Avoids regex performance
+                                        # penalty in perl 5.16 and
+                                        # earlier
     ...
     if ($ERRNO =~ /denied/) { ... }
 
@@ -27,29 +32,56 @@ $INPUT_RECORD_SEPARATOR if you are using the English module.
 
 See L<perlvar> for a complete list of these.
 
-=head1 BUGS
+=head1 PERFORMANCE
+
+NOTE: This was fixed in perl 5.20.  Mentioning these three variables no
+longer makes a speed difference.  This section still applies if your code
+is to run on perl 5.18 or earlier.
+
+This module can provoke sizeable inefficiencies for regular expressions,
+due to unfortunate implementation details.  If performance matters in
+your application and you don't need $PREMATCH, $MATCH, or $POSTMATCH,
+try doing
 
-This module provokes sizeable inefficiencies for regular expressions,
-due to unfortunate implementation details.  If performance matters,
-consider avoiding English.
+   use English qw( -no_match_vars ) ;
+
+.  B<It is especially important to do this in modules to avoid penalizing
+all applications which use them.>
 
 =cut
 
 no warnings;
 
+my $globbed_match ;
+
 # Grandfather $NAME import
 sub import {
     my $this = shift;
-    my @list = @_;
+    my @list = grep { ! /^-no_match_vars$/ } @_ ;
     local $Exporter::ExportLevel = 1;
+    if ( @_ == @list ) {
+        *EXPORT = \@COMPLETE_EXPORT ;
+        $globbed_match ||= (
+           eval q{
+               *MATCH                          = *&    ;
+               *PREMATCH                       = *`    ;
+               *POSTMATCH                      = *'    ;
+               1 ;
+              }
+           || do {
+               require Carp ;
+               Carp::croak("Can't create English for match leftovers: $@") ;
+           }
+       ) ;
+    }
+    else {
+        *EXPORT = \@MINIMAL_EXPORT ;
+    }
     Exporter::import($this,grep {s/^\$/*/} @list);
 }
 
-@EXPORT = qw(
+@MINIMAL_EXPORT = qw(
        *ARG
-       *MATCH
-       *PREMATCH
-       *POSTMATCH
        *LAST_PAREN_MATCH
        *INPUT_LINE_NUMBER
        *NR
@@ -87,7 +119,9 @@ sub import {
        *EGID
        *PROGRAM_NAME
        *PERL_VERSION
+       *OLD_PERL_VERSION
        *ACCUMULATOR
+       *COMPILING
        *DEBUGGING
        *SYSTEM_FD_MAX
        *INPLACE_EDIT
@@ -98,20 +132,28 @@ sub import {
        *OSNAME
        *LAST_REGEXP_CODE_RESULT
        *EXCEPTIONS_BEING_CAUGHT
+       *LAST_SUBMATCH_RESULT
        @LAST_MATCH_START
        @LAST_MATCH_END
 );
 
-# The ground of all being. @ARG is deprecated (5.005 makes @_ lexical)
+
+@MATCH_EXPORT = qw(
+       *MATCH
+       *PREMATCH
+       *POSTMATCH
+);
+
+@COMPLETE_EXPORT = ( @MINIMAL_EXPORT, @MATCH_EXPORT ) ;
+
+# The ground of all being.
 
        *ARG                                    = *_    ;
 
 # Matching.
 
-       *MATCH                                  = *&    ;
-       *PREMATCH                               = *`    ;
-       *POSTMATCH                              = *'    ;
        *LAST_PAREN_MATCH                       = *+    ;
+       *LAST_SUBMATCH_RESULT                   = *^N ;
        *LAST_MATCH_START                       = *-{ARRAY} ;
        *LAST_MATCH_END                         = *+{ARRAY} ;
 
@@ -140,7 +182,7 @@ sub import {
 
        *FORMAT_PAGE_NUMBER                     = *%    ;
        *FORMAT_LINES_PER_PAGE                  = *=    ;
-       *FORMAT_LINES_LEFT                      = *-    ;
+       *FORMAT_LINES_LEFT                      = *-{SCALAR}    ;
        *FORMAT_NAME                            = *~    ;
        *FORMAT_TOP_NAME                        = *^    ;
        *FORMAT_LINE_BREAK_CHARACTERS           = *:    ;
@@ -151,6 +193,8 @@ sub import {
        *CHILD_ERROR                            = *?    ;
        *OS_ERROR                               = *!    ;
            *ERRNO                              = *!    ;
+       *OS_ERROR                               = *!    ;
+           *ERRNO                              = *!    ;
        *EXTENDED_OS_ERROR                      = *^E   ;
        *EVAL_ERROR                             = *@    ;
 
@@ -171,6 +215,7 @@ sub import {
 # Internals.
 
        *PERL_VERSION                           = *^V   ;
+       *OLD_PERL_VERSION                       = *]    ;
        *ACCUMULATOR                            = *^A   ;
        *COMPILING                              = *^C   ;
        *DEBUGGING                              = *^D   ;
@@ -188,7 +233,5 @@ sub import {
 
 #      *ARRAY_BASE                             = *[    ;
 #      *OFMT                                   = *#    ;
-#      *MULTILINE_MATCHING                     = **    ;
-#      *OLD_PERL_VERSION                       = *]    ;
 
 1;