diff options
author | Todd C. Miller <millert@cvs.openbsd.org> | 2001-05-24 18:26:02 +0000 |
---|---|---|
committer | Todd C. Miller <millert@cvs.openbsd.org> | 2001-05-24 18:26:02 +0000 |
commit | 6c39ed6825893b17d2e0b3365a3fa070968c41fa (patch) | |
tree | ca10ab1c9daf38cd472b91d0ebd808dd8785c09b /gnu/usr.bin | |
parent | 4f55a7aa5dcc7b3afaa0ff6060bea0380965d082 (diff) |
stock perl 5.6.1
Diffstat (limited to 'gnu/usr.bin')
-rw-r--r-- | gnu/usr.bin/perl/pod/perldebtut.pod | 115 | ||||
-rw-r--r-- | gnu/usr.bin/perl/pod/perlebcdic.pod | 1447 | ||||
-rw-r--r-- | gnu/usr.bin/perl/pod/perlmodlib.PL | 1297 | ||||
-rw-r--r-- | gnu/usr.bin/perl/pod/perlnewmod.pod | 117 | ||||
-rw-r--r-- | gnu/usr.bin/perl/pod/perlrequick.pod | 114 | ||||
-rw-r--r-- | gnu/usr.bin/perl/pod/perlretut.pod | 1437 | ||||
-rw-r--r-- | gnu/usr.bin/perl/pod/perlutil.pod | 157 | ||||
-rw-r--r-- | gnu/usr.bin/perl/t/op/anonsub.t | 74 | ||||
-rw-r--r-- | gnu/usr.bin/perl/t/op/length.t | 191 | ||||
-rw-r--r-- | gnu/usr.bin/perl/t/op/my_stash.t | 14 | ||||
-rw-r--r-- | gnu/usr.bin/perl/t/op/reverse.t | 91 | ||||
-rw-r--r-- | gnu/usr.bin/perl/t/op/utf8decode.t | 302 | ||||
-rw-r--r-- | gnu/usr.bin/perl/t/run/runenv.t | 303 |
13 files changed, 2177 insertions, 3482 deletions
diff --git a/gnu/usr.bin/perl/pod/perldebtut.pod b/gnu/usr.bin/perl/pod/perldebtut.pod index cc4f5051e17..e11102e5676 100644 --- a/gnu/usr.bin/perl/pod/perldebtut.pod +++ b/gnu/usr.bin/perl/pod/perldebtut.pod @@ -18,8 +18,7 @@ This is for them. First of all, there's a few things you can do to make your life a lot more straightforward when it comes to debugging perl programs, without using the -debugger at all. To demonstrate, here's a simple script, named "hello", with -a problem: +debugger at all. To demonstrate, here's a simple script with a problem: #!/usr/bin/perl @@ -36,9 +35,9 @@ is, it will print out a newline character, and you'll get what looks like a blank line. It looks like there's 2 variables when (because of the typo) there's really 3: - $var1 = 'Hello World'; - $varl = undef; - $var2 = "\n"; + $var1 = 'Hello World' + $varl = undef + $var2 = "\n" To catch this kind of problem, we can force each variable to be declared before use by pulling in the strict module, by putting 'use strict;' after the @@ -60,7 +59,7 @@ script looks like this: use strict; my $var1 = 'Hello World'; - my $varl = undef; + my $varl = ''; my $var2 = "$varl\n"; print $var2; @@ -72,11 +71,11 @@ We then do (always a good idea) a syntax check before we try to run it again: hello syntax OK And now when we run it, we get "\n" still, but at least we know why. Just -getting this script to compile has exposed the '$varl' (with the letter 'l') +getting this script to compile has exposed the '$varl' (with the letter 'l) variable, and simply changing $varl to $var1 solves the problem. -=head1 Looking at data and -w and v +=head1 Looking at data and -w and w Ok, but how about when you want to really see your data, what's in that dynamic variable, just before using it? @@ -145,48 +144,46 @@ That's it, you're back on home turf again. =head1 help Fire the debugger up again on your script and we'll look at the help menu. -There's a couple of ways of calling help: a simple 'B<h>' will get the summary -help list, 'B<|h>' (pipe-h) will pipe the help through your pager (which is -(probably 'more' or 'less'), and finally, 'B<h h>' (h-space-h) will give you -the entire help screen. Here is the summary page: - -DB<1>h +There's a couple of ways of calling help: a simple 'B<h>' will get you a long +scrolled list of help, 'B<|h>' (pipe-h) will pipe the help through your pager +('more' or 'less' probably), and finally, 'B<h h>' (h-space-h) will give you a +helpful mini-screen snapshot: + DB<1> h h List/search source lines: Control script execution: - l [ln|sub] List source code T Stack trace - - or . List previous/current line s [expr] Single step [in expr] - v [line] View around line n [expr] Next, steps over subs - f filename View source in file <CR/Enter> Repeat last n or s - /pattern/ ?patt? Search forw/backw r Return from subroutine - M Show module versions c [ln|sub] Continue until position - Debugger controls: L List break/watch/actions - o [...] Set debugger options t [expr] Toggle trace [trace expr] - <[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set breakpoint - ! [N|pat] Redo a previous command B ln|* Delete a/all breakpoints - H [-num] Display last num commands a [ln] cmd Do cmd before line - = [a val] Define/list an alias A ln|* Delete a/all actions - h [db_cmd] Get help on command w expr Add a watch expression - h h Complete help page W expr|* Delete a/all watch exprs - |[|]db_cmd Send output to pager ![!] syscmd Run cmd in a subprocess - q or ^D Quit R Attempt a restart - Data Examination: expr Execute perl code, also see: s,n,t expr - x|m expr Evals expr in list context, dumps the result or lists methods. - p expr Print expression (uses script's current package). - S [[!]pat] List subroutine names [not] matching pattern - V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or !pattern. - X [Vars] Same as "V current_package [Vars]". - y [n [Vars]] List lexicals in higher scope <n>. Vars same as V. - For more help, type h cmd_letter, or run man perldebug for all docs. + l [ln|sub] List source code T Stack trace + - or . List previous/current line s [expr] Single step [in expr] + w [line] List around line n [expr] Next, steps over subs + f filename View source in file <CR/Enter> Repeat last n or s + /pattern/ ?patt? Search forw/backw r Return from subroutine + v Show versions of modules c [ln|sub] Continue until position + Debugger controls: L List +break/watch/actions + O [...] Set debugger options t [expr] Toggle trace [trace expr] + <[<]|{[{]|>[>] [cmd] Do pre/post-prompt b [ln|event|sub] [cnd] Set breakpoint + ! [N|pat] Redo a previous command d [ln] or D Delete a/all breakpoints + H [-num] Display last num commands a [ln] cmd Do cmd before line + = [a val] Define/list an alias W expr Add a watch expression + h [db_cmd] Get help on command A or W Delete all actions/watch + |[|]db_cmd Send output to pager ![!] syscmd Run cmd in a subprocess + q or ^D Quit R Attempt a restart + Data Examination: expr Execute perl code, also see: s,n,t expr + x|m expr Evals expr in list context, dumps the result or lists methods. + p expr Print expression (uses script's current package). + S [[!]pat] List subroutine names [not] matching pattern + V [Pk [Vars]] List Variables in Package. Vars can be ~pattern or !pattern. + X [Vars] Same as "V current_package [Vars]". + For more help, type h cmd_letter, or run man perldebug for all docs. More confusing options than you can shake a big stick at! It's not as bad as it looks and it's very useful to know more about all of it, and fun too! There's a couple of useful ones to know about straight away. You wouldn't -think we're using any libraries at all at the moment, but 'B<M>' will show -which modules are currently loaded, and their version number, while 'B<m>' -will show the methods, and 'B<S>' shows all subroutines (by pattern) as -shown below. 'B<V>' and 'B<X>' show variables in the program by package -scope and can be constrained by pattern. +think we're using any libraries at all at the moment, but 'B<v>' will show +which modules are currently loaded, by the debugger as well your script. +'B<V>' and 'B<X>' show variables in the program by package scope and can be +constrained by pattern. 'B<m>' shows methods and 'B<S>' shows all subroutines +(by pattern): DB<2>S str dumpvar::stringify @@ -201,10 +198,11 @@ the 'name': FileHandle(stderr) => fileno(2) Remember we're in our tiny program with a problem, we should have a look at -where we are, and what our data looks like. First of all let's view some code -at our present position (the first line of code in this case), via 'B<v>': +where we are, and what our data looks like. First of all let's have a window +on our present position (the first line of code in this case), via the letter +'B<w>': - DB<4> v + DB<4> w 1 #!/usr/bin/perl 2: use strict; 3 @@ -217,9 +215,9 @@ at our present position (the first line of code in this case), via 'B<v>': 10 ); At line number 4 is a helpful pointer, that tells you where you are now. To -see more code, type 'v' again: +see more code, type 'w' again: - DB<4> v + DB<4> w 8 'welcome' => q(Hello World), 9 'zip' => q(welcome), 10 ); @@ -331,8 +329,8 @@ While we're here, take a closer look at the 'B<x>' command, it's really useful and will merrily dump out nested references, complete objects, partial objects - just about whatever you throw at it: -Let's make a quick object and x-plode it, first we'll start the debugger: -it wants some form of input from STDIN, so we give it something non-committal, +Let's make a quick object and x-plode it, first we'll start the the debugger: +it wants some form of input from STDIN, so we give it something non-commital, a zero: > perl -de 0 @@ -479,9 +477,9 @@ We'll simply continue down to our pre-set breakpoint with a 'B<c>': DB<1> c main::(temp:10): if ($deg eq 'c') { -Followed by a view command to see where we are: +Followed by a window command to see where we are: - DB<1> v + DB<1> w 7: my ($deg, $num) = ($1, $2); 8: my ($in, $out) = ($num, $num); 9: $DB::single=2; @@ -512,15 +510,15 @@ using the list 'L' command: 17: print "$out $deg\n"; break if (1) -Note that to delete a breakpoint you use 'B'. +Note that to delete a breakpoint you use 'd' or 'D'. Now we'll continue down into our subroutine, this time rather than by line -number, we'll use the subroutine name, followed by the now familiar 'v': +number, we'll use the subroutine name, followed by the now familiar 'w': DB<3> c f2c main::f2c(temp:30): my $f = shift; - DB<4> v + DB<4> w 24: exit; 25 26 sub f2c { @@ -588,7 +586,7 @@ Actions, watch variables, stack traces etc.: on the TODO list. a - w + W t @@ -668,7 +666,7 @@ and there's a B<vi> interface too. You don't have to do this all on the command line, though, there are a few GUI options out there. The nice thing about these is you can wave a mouse over a -variable and a dump of its data will appear in an appropriate window, or in a +variable and a dump of it's data will appear in an appropriate window, or in a popup balloon, no more tiresome typing of 'x $varname' :-) In particular have a hunt around for the following: @@ -702,12 +700,13 @@ place to go), and of course, experiment. L<perldebug>, L<perldebguts>, L<perldiag>, +L<dprofpp>, L<perlrun> =head1 AUTHOR -Richard Foley <richard.foley@rfi.net> Copyright (c) 2000 +Richard Foley <richard@rfi.net> Copyright (c) 2000 =head1 CONTRIBUTORS diff --git a/gnu/usr.bin/perl/pod/perlebcdic.pod b/gnu/usr.bin/perl/pod/perlebcdic.pod index 36f93e8d973..12ea2f3ef4b 100644 --- a/gnu/usr.bin/perl/pod/perlebcdic.pod +++ b/gnu/usr.bin/perl/pod/perlebcdic.pod @@ -1,5 +1,3 @@ -=encoding utf8 - =head1 NAME perlebcdic - Considerations for running Perl on EBCDIC platforms @@ -7,32 +5,26 @@ perlebcdic - Considerations for running Perl on EBCDIC platforms =head1 DESCRIPTION An exploration of some of the issues facing Perl programmers -on EBCDIC based computers. We do not cover localization, -internationalization, or multi-byte character set issues other -than some discussion of UTF-8 and UTF-EBCDIC. +on EBCDIC based computers. We do not cover localization, +internationalization, or multi byte character set issues (yet). Portions that are still incomplete are marked with XXX. -Perl used to work on EBCDIC machines, but there are now areas of the code where -it doesn't. If you want to use Perl on an EBCDIC machine, please let us know -by sending mail to perlbug@perl.org - =head1 COMMON CHARACTER CODE SETS =head2 ASCII -The American Standard Code for Information Interchange (ASCII or US-ASCII) is a -set of -integers running from 0 to 127 (decimal) that imply character -interpretation by the display and other systems of computers. -The range 0..127 can be covered by setting the bits in a 7-bit binary -digit, hence the set is sometimes referred to as "7-bit ASCII". -ASCII was described by the American National Standards Institute -document ANSI X3.4-1986. It was also described by ISO 646:1991 -(with localization for currency symbols). The full ASCII set is -given in the table below as the first 128 elements. Languages that -can be written adequately with the characters in ASCII include -English, Hawaiian, Indonesian, Swahili and some Native American +The American Standard Code for Information Interchange is a set of +integers running from 0 to 127 (decimal) that imply character +interpretation by the display and other system(s) of computers. +The range 0..127 can be covered by setting the bits in a 7-bit binary +digit, hence the set is sometimes referred to as a "7-bit ASCII". +ASCII was described by the American National Standards Institute +document ANSI X3.4-1986. It was also described by ISO 646:1991 +(with localization for currency symbols). The full ASCII set is +given in the table below as the first 128 elements. Languages that +can be written adequately with the characters in ASCII include +English, Hawaiian, Indonesian, Swahili and some Native American languages. There are many character sets that extend the range of integers @@ -41,211 +33,87 @@ One common one is the ISO 8859-1 character set. =head2 ISO 8859 -The ISO 8859-$n are a collection of character code sets from the -International Organization for Standardization (ISO), each of which -adds characters to the ASCII set that are typically found in European -languages, many of which are based on the Roman, or Latin, alphabet. +The ISO 8859-$n are a collection of character code sets from the +International Organization for Standardization (ISO) each of which +adds characters to the ASCII set that are typically found in European +languages many of which are based on the Roman, or Latin, alphabet. =head2 Latin 1 (ISO 8859-1) -A particular 8-bit extension to ASCII that includes grave and acute -accented Latin characters. Languages that can employ ISO 8859-1 -include all the languages covered by ASCII as well as Afrikaans, -Albanian, Basque, Catalan, Danish, Faroese, Finnish, Norwegian, -Portuguese, Spanish, and Swedish. Dutch is covered albeit without -the ij ligature. French is covered too but without the oe ligature. +A particular 8-bit extension to ASCII that includes grave and acute +accented Latin characters. Languages that can employ ISO 8859-1 +include all the languages covered by ASCII as well as Afrikaans, +Albanian, Basque, Catalan, Danish, Faroese, Finnish, Norwegian, +Portugese, Spanish, and Swedish. Dutch is covered albeit without +the ij ligature. French is covered too but without the oe ligature. German can use ISO 8859-1 but must do so without German-style -quotation marks. This set is based on Western European extensions +quotation marks. This set is based on Western European extensions to ASCII and is commonly encountered in world wide web work. In IBM character code set identification terminology ISO 8859-1 is also known as CCSID 819 (or sometimes 0819 or even 00819). =head2 EBCDIC -The Extended Binary Coded Decimal Interchange Code refers to a -large collection of single- and multi-byte coded character sets that are -different from ASCII or ISO 8859-1 and are all slightly different from each -other; they typically run on host computers. The EBCDIC encodings derive from -8-bit byte extensions of Hollerith punched card encodings. The layout on the -cards was such that high bits were set for the upper and lower case alphabet -characters [a-z] and [A-Z], but there were gaps within each Latin alphabet -range. - -Some IBM EBCDIC character sets may be known by character code set -identification numbers (CCSID numbers) or code page numbers. +The Extended Binary Coded Decimal Interchange Code refers to a +large collection of slightly different single and multi byte +coded character sets that are different from ASCII or ISO 8859-1 +and typically run on host computers. The EBCDIC encodings derive +from 8 bit byte extensions of Hollerith punched card encodings. +The layout on the cards was such that high bits were set for the +upper and lower case alphabet characters [a-z] and [A-Z], but there +were gaps within each latin alphabet range. -Perl can be compiled on platforms that run any of three commonly used EBCDIC -character sets, listed below. +Some IBM EBCDIC character sets may be known by character code set +identification numbers (CCSID numbers) or code page numbers. Leading +zero digits in CCSID numbers within this document are insignificant. +E.g. CCSID 0037 may be referred to as 37 in places. -=head3 The 13 variant characters +=head2 13 variant characters Among IBM EBCDIC character code sets there are 13 characters that are often mapped to different integer values. Those characters are known as the 13 "variant" characters and are: - \ [ ] { } ^ ~ ! # | $ @ ` - -When Perl is compiled for a platform, it looks at some of these characters to -guess which EBCDIC character set the platform uses, and adapts itself -accordingly to that platform. If the platform uses a character set that is not -one of the three Perl knows about, Perl will either fail to compile, or -mistakenly and silently choose one of the three. -They are: - -=over + \ [ ] { } ^ ~ ! # | $ @ ` -=item B<0037> +=head2 0037 -Character code set ID 0037 is a mapping of the ASCII plus Latin-1 -characters (i.e. ISO 8859-1) to an EBCDIC set. 0037 is used -in North American English locales on the OS/400 operating system -that runs on AS/400 computers. CCSID 0037 differs from ISO 8859-1 +Character code set ID 0037 is a mapping of the ASCII plus Latin-1 +characters (i.e. ISO 8859-1) to an EBCDIC set. 0037 is used +in North American English locales on the OS/400 operating system +that runs on AS/400 computers. CCSID 37 differs from ISO 8859-1 in 237 places, in other words they agree on only 19 code point values. -=item B<1047> +=head2 1047 -Character code set ID 1047 is also a mapping of the ASCII plus -Latin-1 characters (i.e. ISO 8859-1) to an EBCDIC set. 1047 is -used under Unix System Services for OS/390 or z/OS, and OpenEdition -for VM/ESA. CCSID 1047 differs from CCSID 0037 in eight places. +Character code set ID 1047 is also a mapping of the ASCII plus +Latin-1 characters (i.e. ISO 8859-1) to an EBCDIC set. 1047 is +used under Unix System Services for OS/390, and OpenEdition for VM/ESA. +CCSID 1047 differs from CCSID 0037 in eight places. -=item B<POSIX-BC> +=head2 POSIX-BC The EBCDIC code page in use on Siemens' BS2000 system is distinct from 1047 and 0037. It is identified below as the POSIX-BC set. -=back - -=head2 Unicode code points versus EBCDIC code points - -In Unicode terminology a I<code point> is the number assigned to a -character: for example, in EBCDIC the character "A" is usually assigned -the number 193. In Unicode the character "A" is assigned the number 65. -This causes a problem with the semantics of the pack/unpack "U", which -are supposed to pack Unicode code points to characters and back to numbers. -The problem is: which code points to use for code points less than 256? -(for 256 and over there's no problem: Unicode code points are used) -In EBCDIC, for the low 256 the EBCDIC code points are used. This -means that the equivalences - - pack("U", ord($character)) eq $character - unpack("U", $character) == ord $character - -will hold. (If Unicode code points were applied consistently over -all the possible code points, pack("U",ord("A")) would in EBCDIC -equal I<A with acute> or chr(101), and unpack("U", "A") would equal -65, or I<non-breaking space>, not 193, or ord "A".) - -=head2 Remaining Perl Unicode problems in EBCDIC - -=over 4 - -=item * - -Many of the remaining problems seem to be related to case-insensitive matching - -=item * - -The extensions Unicode::Collate and Unicode::Normalized are not -supported under EBCDIC, likewise for the encoding pragma. - -=back - -=head2 Unicode and UTF - -UTF stands for C<Unicode Transformation Format>. -UTF-8 is an encoding of Unicode into a sequence of 8-bit byte chunks, based on -ASCII and Latin-1. -The length of a sequence required to represent a Unicode code point -depends on the ordinal number of that code point, -with larger numbers requiring more bytes. -UTF-EBCDIC is like UTF-8, but based on EBCDIC. - -You may see the term C<invariant> character or code point. -This simply means that the character has the same numeric -value when encoded as when not. -(Note that this is a very different concept from L</The 13 variant characters> -mentioned above.) -For example, the ordinal value of 'A' is 193 in most EBCDIC code pages, -and also is 193 when encoded in UTF-EBCDIC. -All variant code points occupy at least two bytes when encoded. -In UTF-8, the code points corresponding to the lowest 128 -ordinal numbers (0 - 127: the ASCII characters) are invariant. -In UTF-EBCDIC, there are 160 invariant characters. -(If you care, the EBCDIC invariants are those characters -which have ASCII equivalents, plus those that correspond to -the C1 controls (80..9f on ASCII platforms).) - -A string encoded in UTF-EBCDIC may be longer (but never shorter) than -one encoded in UTF-8. - -=head2 Using Encode - -Starting from Perl 5.8 you can use the standard new module Encode -to translate from EBCDIC to Latin-1 code points. -Encode knows about more EBCDIC character sets than Perl can currently -be compiled to run on. - - use Encode 'from_to'; - - my %ebcdic = ( 176 => 'cp37', 95 => 'cp1047', 106 => 'posix-bc' ); - - # $a is in EBCDIC code points - from_to($a, $ebcdic{ord '^'}, 'latin1'); - # $a is ISO 8859-1 code points - -and from Latin-1 code points to EBCDIC code points - - use Encode 'from_to'; - - my %ebcdic = ( 176 => 'cp37', 95 => 'cp1047', 106 => 'posix-bc' ); - - # $a is ISO 8859-1 code points - from_to($a, 'latin1', $ebcdic{ord '^'}); - # $a is in EBCDIC code points - -For doing I/O it is suggested that you use the autotranslating features -of PerlIO, see L<perluniintro>. - -Since version 5.8 Perl uses the new PerlIO I/O library. This enables -you to use different encodings per IO channel. For example you may use - - use Encode; - open($f, ">:encoding(ascii)", "test.ascii"); - print $f "Hello World!\n"; - open($f, ">:encoding(cp37)", "test.ebcdic"); - print $f "Hello World!\n"; - open($f, ">:encoding(latin1)", "test.latin1"); - print $f "Hello World!\n"; - open($f, ">:encoding(utf8)", "test.utf8"); - print $f "Hello World!\n"; - -to get four files containing "Hello World!\n" in ASCII, CP 0037 EBCDIC, -ISO 8859-1 (Latin-1) (in this example identical to ASCII since only ASCII -characters were printed), and -UTF-EBCDIC (in this example identical to normal EBCDIC since only characters -that don't differ between EBCDIC and UTF-EBCDIC were printed). See the -documentation of Encode::PerlIO for details. - -As the PerlIO layer uses raw IO (bytes) internally, all this totally -ignores things like the type of your filesystem (ASCII or EBCDIC). - =head1 SINGLE OCTET TABLES The following tables list the ASCII and Latin 1 ordered sets including the subsets: C0 controls (0..31), ASCII graphics (32..7e), delete (7f), -C1 controls (80..9f), and Latin-1 (a.k.a. ISO 8859-1) (a0..ff). In the -table names of the Latin 1 -extensions to ASCII have been labelled with character names roughly -corresponding to I<The Unicode Standard, Version 6.1> albeit with -substitutions such as s/LATIN// and s/VULGAR// in all cases, s/CAPITAL -LETTER// in some cases, and s/SMALL LETTER ([A-Z])/\l$1/ in some other -cases. Controls are listed using their Unicode 6.2 abbreviations. -The differences between the 0037 and 1047 sets are -flagged with **. The differences between the 1047 and POSIX-BC sets -are flagged with ##. All ord() numbers listed are decimal. If you -would rather see this table listing octal values, then run the table -(that is, the pod source text of this document, since this recipe may not +C1 controls (80..9f), and Latin-1 (a.k.a. ISO 8859-1) (a0..ff). In the +table non-printing control character names as well as the Latin 1 +extensions to ASCII have been labelled with character names roughly +corresponding to I<The Unicode Standard, Version 2.0> albeit with +substitutions such as s/LATIN// and s/VULGAR// in all cases, +s/CAPITAL LETTER// in some cases, and s/SMALL LETTER ([A-Z])/\l$1/ +in some other cases (the C<charnames> pragma names unfortunately do +not list explicit names for the C0 or C1 control characters). The +"names" of the C1 control set (128..159 in ISO 8859-1) listed here are +somewhat arbitrary. The differences between the 0037 and 1047 sets are +flagged with ***. The differences between the 1047 and POSIX-BC sets +are flagged with ###. All ord() numbers listed are decimal. If you +would rather see this table listing octal values then run the table +(that is, the pod version of this document since this recipe may not work with a pod2_other_format translation) through: =over 4 @@ -254,396 +122,332 @@ work with a pod2_other_format translation) through: =back - perl -ne 'if(/(.{29})(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/)' \ - -e '{printf("%s%-5.03o%-5.03o%-5.03o%.03o\n",$1,$2,$3,$4,$5)}' \ - perlebcdic.pod - -If you want to retain the UTF-x code points then in script form you -might want to write: - -=over 4 - -=item recipe 1 - -=back - - open(FH,"<perlebcdic.pod") or die "Could not open perlebcdic.pod: $!"; - while (<FH>) { - if (/(.{29})(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\.?(\d*)\s+(\d+)\.?(\d*)/) - { - if ($7 ne '' && $9 ne '') { - printf( - "%s%-5.03o%-5.03o%-5.03o%-5.03o%-3o.%-5o%-3o.%.03o\n", - $1,$2,$3,$4,$5,$6,$7,$8,$9); - } - elsif ($7 ne '') { - printf("%s%-5.03o%-5.03o%-5.03o%-5.03o%-3o.%-5o%.03o\n", - $1,$2,$3,$4,$5,$6,$7,$8); - } - else { - printf("%s%-5.03o%-5.03o%-5.03o%-5.03o%-5.03o%.03o\n", - $1,$2,$3,$4,$5,$6,$8); - } - } - } + perl -ne 'if(/(.{33})(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/)' \ + -e '{printf("%s%-9o%-9o%-9o%-9o\n",$1,$2,$3,$4,$5)}' perlebcdic.pod If you would rather see this table listing hexadecimal values then run the table through: =over 4 -=item recipe 2 - -=back - - perl -ne 'if(/(.{29})(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/)' \ - -e '{printf("%s%-5.02X%-5.02X%-5.02X%.02X\n",$1,$2,$3,$4,$5)}' \ - perlebcdic.pod - -Or, in order to retain the UTF-x code points in hexadecimal: - -=over 4 - -=item recipe 3 +=item recipe 1 =back - open(FH,"<perlebcdic.pod") or die "Could not open perlebcdic.pod: $!"; - while (<FH>) { - if (/(.{29})(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\s+(\d+)\.?(\d*)\s+(\d+)\.?(\d*)/) - { - if ($7 ne '' && $9 ne '') { - printf( - "%s%-5.02X%-5.02X%-5.02X%-5.02X%-2X.%-6.02X%02X.%02X\n", - $1,$2,$3,$4,$5,$6,$7,$8,$9); - } - elsif ($7 ne '') { - printf("%s%-5.02X%-5.02X%-5.02X%-5.02X%-2X.%-6.02X%02X\n", - $1,$2,$3,$4,$5,$6,$7,$8); - } - else { - printf("%s%-5.02X%-5.02X%-5.02X%-5.02X%-5.02X%02X\n", - $1,$2,$3,$4,$5,$6,$8); - } - } - } - - - ISO - 8859-1 POS- - CCSID CCSID CCSID IX- - chr 0819 0037 1047 BC UTF-8 UTF-EBCDIC - --------------------------------------------------------------------- - <NUL> 0 0 0 0 0 0 - <SOH> 1 1 1 1 1 1 - <STX> 2 2 2 2 2 2 - <ETX> 3 3 3 3 3 3 - <EOT> 4 55 55 55 4 55 - <ENQ> 5 45 45 45 5 45 - <ACK> 6 46 46 46 6 46 - <BEL> 7 47 47 47 7 47 - <BS> 8 22 22 22 8 22 - <HT> 9 5 5 5 9 5 - <LF> 10 37 21 21 10 21 ** - <VT> 11 11 11 11 11 11 - <FF> 12 12 12 12 12 12 - <CR> 13 13 13 13 13 13 - <SO> 14 14 14 14 14 14 - <SI> 15 15 15 15 15 15 - <DLE> 16 16 16 16 16 16 - <DC1> 17 17 17 17 17 17 - <DC2> 18 18 18 18 18 18 - <DC3> 19 19 19 19 19 19 - <DC4> 20 60 60 60 20 60 - <NAK> 21 61 61 61 21 61 - <SYN> 22 50 50 50 22 50 - <ETB> 23 38 38 38 23 38 - <CAN> 24 24 24 24 24 24 - <EOM> 25 25 25 25 25 25 - <SUB> 26 63 63 63 26 63 - <ESC> 27 39 39 39 27 39 - <FS> 28 28 28 28 28 28 - <GS> 29 29 29 29 29 29 - <RS> 30 30 30 30 30 30 - <US> 31 31 31 31 31 31 - <SPACE> 32 64 64 64 32 64 - ! 33 90 90 90 33 90 - " 34 127 127 127 34 127 - # 35 123 123 123 35 123 - $ 36 91 91 91 36 91 - % 37 108 108 108 37 108 - & 38 80 80 80 38 80 - ' 39 125 125 125 39 125 - ( 40 77 77 77 40 77 - ) 41 93 93 93 41 93 - * 42 92 92 92 42 92 - + 43 78 78 78 43 78 - , 44 107 107 107 44 107 - - 45 96 96 96 45 96 - . 46 75 75 75 46 75 - / 47 97 97 97 47 97 - 0 48 240 240 240 48 240 - 1 49 241 241 241 49 241 - 2 50 242 242 242 50 242 - 3 51 243 243 243 51 243 - 4 52 244 244 244 52 244 - 5 53 245 245 245 53 245 - 6 54 246 246 246 54 246 - 7 55 247 247 247 55 247 - 8 56 248 248 248 56 248 - 9 57 249 249 249 57 249 - : 58 122 122 122 58 122 - ; 59 94 94 94 59 94 - < 60 76 76 76 60 76 - = 61 126 126 126 61 126 - > 62 110 110 110 62 110 - ? 63 111 111 111 63 111 - @ 64 124 124 124 64 124 - A 65 193 193 193 65 193 - B 66 194 194 194 66 194 - C 67 195 195 195 67 195 - D 68 196 196 196 68 196 - E 69 197 197 197 69 197 - F 70 198 198 198 70 198 - G 71 199 199 199 71 199 - H 72 200 200 200 72 200 - I 73 201 201 201 73 201 - J 74 209 209 209 74 209 - K 75 210 210 210 75 210 - L 76 211 211 211 76 211 - M 77 212 212 212 77 212 - N 78 213 213 213 78 213 - O 79 214 214 214 79 214 - P 80 215 215 215 80 215 - Q 81 216 216 216 81 216 - R 82 217 217 217 82 217 - S 83 226 226 226 83 226 - T 84 227 227 227 84 227 - U 85 228 228 228 85 228 - V 86 229 229 229 86 229 - W 87 230 230 230 87 230 - X 88 231 231 231 88 231 - Y 89 232 232 232 89 232 - Z 90 233 233 233 90 233 - [ 91 186 173 187 91 173 ** ## - \ 92 224 224 188 92 224 ## - ] 93 187 189 189 93 189 ** - ^ 94 176 95 106 94 95 ** ## - _ 95 109 109 109 95 109 - ` 96 121 121 74 96 121 ## - a 97 129 129 129 97 129 - b 98 130 130 130 98 130 - c 99 131 131 131 99 131 - d 100 132 132 132 100 132 - e 101 133 133 133 101 133 - f 102 134 134 134 102 134 - g 103 135 135 135 103 135 - h 104 136 136 136 104 136 - i 105 137 137 137 105 137 - j 106 145 145 145 106 145 - k 107 146 146 146 107 146 - l 108 147 147 147 108 147 - m 109 148 148 148 109 148 - n 110 149 149 149 110 149 - o 111 150 150 150 111 150 - p 112 151 151 151 112 151 - q 113 152 152 152 113 152 - r 114 153 153 153 114 153 - s 115 162 162 162 115 162 - t 116 163 163 163 116 163 - u 117 164 164 164 117 164 - v 118 165 165 165 118 165 - w 119 166 166 166 119 166 - x 120 167 167 167 120 167 - y 121 168 168 168 121 168 - z 122 169 169 169 122 169 - { 123 192 192 251 123 192 ## - | 124 79 79 79 124 79 - } 125 208 208 253 125 208 ## - ~ 126 161 161 255 126 161 ## - <DEL> 127 7 7 7 127 7 - <PAD> 128 32 32 32 194.128 32 - <HOP> 129 33 33 33 194.129 33 - <BPH> 130 34 34 34 194.130 34 - <NBH> 131 35 35 35 194.131 35 - <IND> 132 36 36 36 194.132 36 - <NEL> 133 21 37 37 194.133 37 ** - <SSA> 134 6 6 6 194.134 6 - <ESA> 135 23 23 23 194.135 23 - <HTS> 136 40 40 40 194.136 40 - <HTJ> 137 41 41 41 194.137 41 - <VTS> 138 42 42 42 194.138 42 - <PLD> 139 43 43 43 194.139 43 - <PLU> 140 44 44 44 194.140 44 - <RI> 141 9 9 9 194.141 9 - <SS2> 142 10 10 10 194.142 10 - <SS3> 143 27 27 27 194.143 27 - <DCS> 144 48 48 48 194.144 48 - <PU1> 145 49 49 49 194.145 49 - <PU2> 146 26 26 26 194.146 26 - <STS> 147 51 51 51 194.147 51 - <CCH> 148 52 52 52 194.148 52 - <MW> 149 53 53 53 194.149 53 - <SPA> 150 54 54 54 194.150 54 - <EPA> 151 8 8 8 194.151 8 - <SOS> 152 56 56 56 194.152 56 - <SGC> 153 57 57 57 194.153 57 - <SCI> 154 58 58 58 194.154 58 - <CSI> 155 59 59 59 194.155 59 - <ST> 156 4 4 4 194.156 4 - <OSC> 157 20 20 20 194.157 20 - <PM> 158 62 62 62 194.158 62 - <APC> 159 255 255 95 194.159 255 ## - <NON-BREAKING SPACE> 160 65 65 65 194.160 128.65 - <INVERTED "!" > 161 170 170 170 194.161 128.66 - <CENT SIGN> 162 74 74 176 194.162 128.67 ## - <POUND SIGN> 163 177 177 177 194.163 128.68 - <CURRENCY SIGN> 164 159 159 159 194.164 128.69 - <YEN SIGN> 165 178 178 178 194.165 128.70 - <BROKEN BAR> 166 106 106 208 194.166 128.71 ## - <SECTION SIGN> 167 181 181 181 194.167 128.72 - <DIAERESIS> 168 189 187 121 194.168 128.73 ** ## - <COPYRIGHT SIGN> 169 180 180 180 194.169 128.74 - <FEMININE ORDINAL> 170 154 154 154 194.170 128.81 - <LEFT POINTING GUILLEMET> 171 138 138 138 194.171 128.82 - <NOT SIGN> 172 95 176 186 194.172 128.83 ** ## - <SOFT HYPHEN> 173 202 202 202 194.173 128.84 - <REGISTERED TRADE MARK> 174 175 175 175 194.174 128.85 - <MACRON> 175 188 188 161 194.175 128.86 ## - <DEGREE SIGN> 176 144 144 144 194.176 128.87 - <PLUS-OR-MINUS SIGN> 177 143 143 143 194.177 128.88 - <SUPERSCRIPT TWO> 178 234 234 234 194.178 128.89 - <SUPERSCRIPT THREE> 179 250 250 250 194.179 128.98 - <ACUTE ACCENT> 180 190 190 190 194.180 128.99 - <MICRO SIGN> 181 160 160 160 194.181 128.100 - <PARAGRAPH SIGN> 182 182 182 182 194.182 128.101 - <MIDDLE DOT> 183 179 179 179 194.183 128.102 - <CEDILLA> 184 157 157 157 194.184 128.103 - <SUPERSCRIPT ONE> 185 218 218 218 194.185 128.104 - <MASC. ORDINAL INDICATOR> 186 155 155 155 194.186 128.105 - <RIGHT POINTING GUILLEMET> 187 139 139 139 194.187 128.106 - <FRACTION ONE QUARTER> 188 183 183 183 194.188 128.112 - <FRACTION ONE HALF> 189 184 184 184 194.189 128.113 - <FRACTION THREE QUARTERS> 190 185 185 185 194.190 128.114 - <INVERTED QUESTION MARK> 191 171 171 171 194.191 128.115 - <A WITH GRAVE> 192 100 100 100 195.128 138.65 - <A WITH ACUTE> 193 101 101 101 195.129 138.66 - <A WITH CIRCUMFLEX> 194 98 98 98 195.130 138.67 - <A WITH TILDE> 195 102 102 102 195.131 138.68 - <A WITH DIAERESIS> 196 99 99 99 195.132 138.69 - <A WITH RING ABOVE> 197 103 103 103 195.133 138.70 - <CAPITAL LIGATURE AE> 198 158 158 158 195.134 138.71 - <C WITH CEDILLA> 199 104 104 104 195.135 138.72 - <E WITH GRAVE> 200 116 116 116 195.136 138.73 - <E WITH ACUTE> 201 113 113 113 195.137 138.74 - <E WITH CIRCUMFLEX> 202 114 114 114 195.138 138.81 - <E WITH DIAERESIS> 203 115 115 115 195.139 138.82 - <I WITH GRAVE> 204 120 120 120 195.140 138.83 - <I WITH ACUTE> 205 117 117 117 195.141 138.84 - <I WITH CIRCUMFLEX> 206 118 118 118 195.142 138.85 - <I WITH DIAERESIS> 207 119 119 119 195.143 138.86 - <CAPITAL LETTER ETH> 208 172 172 172 195.144 138.87 - <N WITH TILDE> 209 105 105 105 195.145 138.88 - <O WITH GRAVE> 210 237 237 237 195.146 138.89 - <O WITH ACUTE> 211 238 238 238 195.147 138.98 - <O WITH CIRCUMFLEX> 212 235 235 235 195.148 138.99 - <O WITH TILDE> 213 239 239 239 195.149 138.100 - <O WITH DIAERESIS> 214 236 236 236 195.150 138.101 - <MULTIPLICATION SIGN> 215 191 191 191 195.151 138.102 - <O WITH STROKE> 216 128 128 128 195.152 138.103 - <U WITH GRAVE> 217 253 253 224 195.153 138.104 ## - <U WITH ACUTE> 218 254 254 254 195.154 138.105 - <U WITH CIRCUMFLEX> 219 251 251 221 195.155 138.106 ## - <U WITH DIAERESIS> 220 252 252 252 195.156 138.112 - <Y WITH ACUTE> 221 173 186 173 195.157 138.113 ** ## - <CAPITAL LETTER THORN> 222 174 174 174 195.158 138.114 - <SMALL LETTER SHARP S> 223 89 89 89 195.159 138.115 - <a WITH GRAVE> 224 68 68 68 195.160 139.65 - <a WITH ACUTE> 225 69 69 69 195.161 139.66 - <a WITH CIRCUMFLEX> 226 66 66 66 195.162 139.67 - <a WITH TILDE> 227 70 70 70 195.163 139.68 - <a WITH DIAERESIS> 228 67 67 67 195.164 139.69 - <a WITH RING ABOVE> 229 71 71 71 195.165 139.70 - <SMALL LIGATURE ae> 230 156 156 156 195.166 139.71 - <c WITH CEDILLA> 231 72 72 72 195.167 139.72 - <e WITH GRAVE> 232 84 84 84 195.168 139.73 - <e WITH ACUTE> 233 81 81 81 195.169 139.74 - <e WITH CIRCUMFLEX> 234 82 82 82 195.170 139.81 - <e WITH DIAERESIS> 235 83 83 83 195.171 139.82 - <i WITH GRAVE> 236 88 88 88 195.172 139.83 - <i WITH ACUTE> 237 85 85 85 195.173 139.84 - <i WITH CIRCUMFLEX> 238 86 86 86 195.174 139.85 - <i WITH DIAERESIS> 239 87 87 87 195.175 139.86 - <SMALL LETTER eth> 240 140 140 140 195.176 139.87 - <n WITH TILDE> 241 73 73 73 195.177 139.88 - <o WITH GRAVE> 242 205 205 205 195.178 139.89 - <o WITH ACUTE> 243 206 206 206 195.179 139.98 - <o WITH CIRCUMFLEX> 244 203 203 203 195.180 139.99 - <o WITH TILDE> 245 207 207 207 195.181 139.100 - <o WITH DIAERESIS> 246 204 204 204 195.182 139.101 - <DIVISION SIGN> 247 225 225 225 195.183 139.102 - <o WITH STROKE> 248 112 112 112 195.184 139.103 - <u WITH GRAVE> 249 221 221 192 195.185 139.104 ## - <u WITH ACUTE> 250 222 222 222 195.186 139.105 - <u WITH CIRCUMFLEX> 251 219 219 219 195.187 139.106 - <u WITH DIAERESIS> 252 220 220 220 195.188 139.112 - <y WITH ACUTE> 253 141 141 141 195.189 139.113 - <SMALL LETTER thorn> 254 142 142 142 195.190 139.114 - <y WITH DIAERESIS> 255 223 223 223 195.191 139.115 + perl -ne 'if(/(.{33})(\d+)\s+(\d+)\s+(\d+)\s+(\d+)/)' \ + -e '{printf("%s%-9X%-9X%-9X%-9X\n",$1,$2,$3,$4,$5)}' perlebcdic.pod + + + 8859-1 + chr 0819 0037 1047 POSIX-BC + ---------------------------------------------------------------- + <NULL> 0 0 0 0 + <START OF HEADING> 1 1 1 1 + <START OF TEXT> 2 2 2 2 + <END OF TEXT> 3 3 3 3 + <END OF TRANSMISSION> 4 55 55 55 + <ENQUIRY> 5 45 45 45 + <ACKNOWLEDGE> 6 46 46 46 + <BELL> 7 47 47 47 + <BACKSPACE> 8 22 22 22 + <HORIZONTAL TABULATION> 9 5 5 5 + <LINE FEED> 10 37 21 21 *** + <VERTICAL TABULATION> 11 11 11 11 + <FORM FEED> 12 12 12 12 + <CARRIAGE RETURN> 13 13 13 13 + <SHIFT OUT> 14 14 14 14 + <SHIFT IN> 15 15 15 15 + <DATA LINK ESCAPE> 16 16 16 16 + <DEVICE CONTROL ONE> 17 17 17 17 + <DEVICE CONTROL TWO> 18 18 18 18 + <DEVICE CONTROL THREE> 19 19 19 19 + <DEVICE CONTROL FOUR> 20 60 60 60 + <NEGATIVE ACKNOWLEDGE> 21 61 61 61 + <SYNCHRONOUS IDLE> 22 50 50 50 + <END OF TRANSMISSION BLOCK> 23 38 38 38 + <CANCEL> 24 24 24 24 + <END OF MEDIUM> 25 25 25 25 + <SUBSTITUTE> 26 63 63 63 + <ESCAPE> 27 39 39 39 + <FILE SEPARATOR> 28 28 28 28 + <GROUP SEPARATOR> 29 29 29 29 + <RECORD SEPARATOR> 30 30 30 30 + <UNIT SEPARATOR> 31 31 31 31 + <SPACE> 32 64 64 64 + ! 33 90 90 90 + " 34 127 127 127 + # 35 123 123 123 + $ 36 91 91 91 + % 37 108 108 108 + & 38 80 80 80 + ' 39 125 125 125 + ( 40 77 77 77 + ) 41 93 93 93 + * 42 92 92 92 + + 43 78 78 78 + , 44 107 107 107 + - 45 96 96 96 + . 46 75 75 75 + / 47 97 97 97 + 0 48 240 240 240 + 1 49 241 241 241 + 2 50 242 242 242 + 3 51 243 243 243 + 4 52 244 244 244 + 5 53 245 245 245 + 6 54 246 246 246 + 7 55 247 247 247 + 8 56 248 248 248 + 9 57 249 249 249 + : 58 122 122 122 + ; 59 94 94 94 + < 60 76 76 76 + = 61 126 126 126 + > 62 110 110 110 + ? 63 111 111 111 + @ 64 124 124 124 + A 65 193 193 193 + B 66 194 194 194 + C 67 195 195 195 + D 68 196 196 196 + E 69 197 197 197 + F 70 198 198 198 + G 71 199 199 199 + H 72 200 200 200 + I 73 201 201 201 + J 74 209 209 209 + K 75 210 210 210 + L 76 211 211 211 + M 77 212 212 212 + N 78 213 213 213 + O 79 214 214 214 + P 80 215 215 215 + Q 81 216 216 216 + R 82 217 217 217 + S 83 226 226 226 + T 84 227 227 227 + U 85 228 228 228 + V 86 229 229 229 + W 87 230 230 230 + X 88 231 231 231 + Y 89 232 232 232 + Z 90 233 233 233 + [ 91 186 173 187 *** ### + \ 92 224 224 188 ### + ] 93 187 189 189 *** + ^ 94 176 95 106 *** ### + _ 95 109 109 109 + ` 96 121 121 74 ### + a 97 129 129 129 + b 98 130 130 130 + c 99 131 131 131 + d 100 132 132 132 + e 101 133 133 133 + f 102 134 134 134 + g 103 135 135 135 + h 104 136 136 136 + i 105 137 137 137 + j 106 145 145 145 + k 107 146 146 146 + l 108 147 147 147 + m 109 148 148 148 + n 110 149 149 149 + o 111 150 150 150 + p 112 151 151 151 + q 113 152 152 152 + r 114 153 153 153 + s 115 162 162 162 + t 116 163 163 163 + u 117 164 164 164 + v 118 165 165 165 + w 119 166 166 166 + x 120 167 167 167 + y 121 168 168 168 + z 122 169 169 169 + { 123 192 192 251 ### + | 124 79 79 79 + } 125 208 208 253 ### + ~ 126 161 161 255 ### + <DELETE> 127 7 7 7 + <C1 0> 128 32 32 32 + <C1 1> 129 33 33 33 + <C1 2> 130 34 34 34 + <C1 3> 131 35 35 35 + <C1 4> 132 36 36 36 + <C1 5> 133 21 37 37 *** + <C1 6> 134 6 6 6 + <C1 7> 135 23 23 23 + <C1 8> 136 40 40 40 + <C1 9> 137 41 41 41 + <C1 10> 138 42 42 42 + <C1 11> 139 43 43 43 + <C1 12> 140 44 44 44 + <C1 13> 141 9 9 9 + <C1 14> 142 10 10 10 + <C1 15> 143 27 27 27 + <C1 16> 144 48 48 48 + <C1 17> 145 49 49 49 + <C1 18> 146 26 26 26 + <C1 19> 147 51 51 51 + <C1 20> 148 52 52 52 + <C1 21> 149 53 53 53 + <C1 22> 150 54 54 54 + <C1 23> 151 8 8 8 + <C1 24> 152 56 56 56 + <C1 25> 153 57 57 57 + <C1 26> 154 58 58 58 + <C1 27> 155 59 59 59 + <C1 28> 156 4 4 4 + <C1 29> 157 20 20 20 + <C1 30> 158 62 62 62 + <C1 31> 159 255 255 95 ### + <NON-BREAKING SPACE> 160 65 65 65 + <INVERTED EXCLAMATION MARK> 161 170 170 170 + <CENT SIGN> 162 74 74 176 ### + <POUND SIGN> 163 177 177 177 + <CURRENCY SIGN> 164 159 159 159 + <YEN SIGN> 165 178 178 178 + <BROKEN BAR> 166 106 106 208 ### + <SECTION SIGN> 167 181 181 181 + <DIAERESIS> 168 189 187 121 *** ### + <COPYRIGHT SIGN> 169 180 180 180 + <FEMININE ORDINAL INDICATOR> 170 154 154 154 + <LEFT POINTING GUILLEMET> 171 138 138 138 + <NOT SIGN> 172 95 176 186 *** ### + <SOFT HYPHEN> 173 202 202 202 + <REGISTERED TRADE MARK SIGN> 174 175 175 175 + <MACRON> 175 188 188 161 ### + <DEGREE SIGN> 176 144 144 144 + <PLUS-OR-MINUS SIGN> 177 143 143 143 + <SUPERSCRIPT TWO> 178 234 234 234 + <SUPERSCRIPT THREE> 179 250 250 250 + <ACUTE ACCENT> 180 190 190 190 + <MICRO SIGN> 181 160 160 160 + <PARAGRAPH SIGN> 182 182 182 182 + <MIDDLE DOT> 183 179 179 179 + <CEDILLA> 184 157 157 157 + <SUPERSCRIPT ONE> 185 218 218 218 + <MASC. ORDINAL INDICATOR> 186 155 155 155 + <RIGHT POINTING GUILLEMET> 187 139 139 139 + <FRACTION ONE QUARTER> 188 183 183 183 + <FRACTION ONE HALF> 189 184 184 184 + <FRACTION THREE QUARTERS> 190 185 185 185 + <INVERTED QUESTION MARK> 191 171 171 171 + <A WITH GRAVE> 192 100 100 100 + <A WITH ACUTE> 193 101 101 101 + <A WITH CIRCUMFLEX> 194 98 98 98 + <A WITH TILDE> 195 102 102 102 + <A WITH DIAERESIS> 196 99 99 99 + <A WITH RING ABOVE> 197 103 103 103 + <CAPITAL LIGATURE AE> 198 158 158 158 + <C WITH CEDILLA> 199 104 104 104 + <E WITH GRAVE> 200 116 116 116 + <E WITH ACUTE> 201 113 113 113 + <E WITH CIRCUMFLEX> 202 114 114 114 + <E WITH DIAERESIS> 203 115 115 115 + <I WITH GRAVE> 204 120 120 120 + <I WITH ACUTE> 205 117 117 117 + <I WITH CIRCUMFLEX> 206 118 118 118 + <I WITH DIAERESIS> 207 119 119 119 + <CAPITAL LETTER ETH> 208 172 172 172 + <N WITH TILDE> 209 105 105 105 + <O WITH GRAVE> 210 237 237 237 + <O WITH ACUTE> 211 238 238 238 + <O WITH CIRCUMFLEX> 212 235 235 235 + <O WITH TILDE> 213 239 239 239 + <O WITH DIAERESIS> 214 236 236 236 + <MULTIPLICATION SIGN> 215 191 191 191 + <O WITH STROKE> 216 128 128 128 + <U WITH GRAVE> 217 253 253 224 ### + <U WITH ACUTE> 218 254 254 254 + <U WITH CIRCUMFLEX> 219 251 251 221 ### + <U WITH DIAERESIS> 220 252 252 252 + <Y WITH ACUTE> 221 173 186 173 *** ### + <CAPITAL LETTER THORN> 222 174 174 174 + <SMALL LETTER SHARP S> 223 89 89 89 + <a WITH GRAVE> 224 68 68 68 + <a WITH ACUTE> 225 69 69 69 + <a WITH CIRCUMFLEX> 226 66 66 66 + <a WITH TILDE> 227 70 70 70 + <a WITH DIAERESIS> 228 67 67 67 + <a WITH RING ABOVE> 229 71 71 71 + <SMALL LIGATURE ae> 230 156 156 156 + <c WITH CEDILLA> 231 72 72 72 + <e WITH GRAVE> 232 84 84 84 + <e WITH ACUTE> 233 81 81 81 + <e WITH CIRCUMFLEX> 234 82 82 82 + <e WITH DIAERESIS> 235 83 83 83 + <i WITH GRAVE> 236 88 88 88 + <i WITH ACUTE> 237 85 85 85 + <i WITH CIRCUMFLEX> 238 86 86 86 + <i WITH DIAERESIS> 239 87 87 87 + <SMALL LETTER eth> 240 140 140 140 + <n WITH TILDE> 241 73 73 73 + <o WITH GRAVE> 242 205 205 205 + <o WITH ACUTE> 243 206 206 206 + <o WITH CIRCUMFLEX> 244 203 203 203 + <o WITH TILDE> 245 207 207 207 + <o WITH DIAERESIS> 246 204 204 204 + <DIVISION SIGN> 247 225 225 225 + <o WITH STROKE> 248 112 112 112 + <u WITH GRAVE> 249 221 221 192 ### + <u WITH ACUTE> 250 222 222 222 + <u WITH CIRCUMFLEX> 251 219 219 219 + <u WITH DIAERESIS> 252 220 220 220 + <y WITH ACUTE> 253 141 141 141 + <SMALL LETTER thorn> 254 142 142 142 + <y WITH DIAERESIS> 255 223 223 223 If you would rather see the above table in CCSID 0037 order rather than ASCII + Latin-1 order then run the table through: =over 4 -=item recipe 4 +=item recipe 2 =back - perl \ - -ne 'if(/.{29}\d{1,3}\s{2,4}\d{1,3}\s{2,4}\d{1,3}\s{2,4}\d{1,3}/)'\ + perl -ne 'if(/.{33}\d{1,3}\s{6,8}\d{1,3}\s{6,8}\d{1,3}\s{6,8}\d{1,3}/)'\ -e '{push(@l,$_)}' \ -e 'END{print map{$_->[0]}' \ - -e ' sort{$a->[1] <=> $b->[1]}' \ - -e ' map{[$_,substr($_,34,3)]}@l;}' perlebcdic.pod + -e ' sort{$a->[1] <=> $b->[1]}' \ + -e ' map{[$_,substr($_,42,3)]}@l;}' perlebcdic.pod -If you would rather see it in CCSID 1047 order then change the number -34 in the last line to 39, like this: +If you would rather see it in CCSID 1047 order then change the digit +42 in the last line to 51, like this: =over 4 -=item recipe 5 +=item recipe 3 =back - perl \ - -ne 'if(/.{29}\d{1,3}\s{2,4}\d{1,3}\s{2,4}\d{1,3}\s{2,4}\d{1,3}/)'\ - -e '{push(@l,$_)}' \ - -e 'END{print map{$_->[0]}' \ - -e ' sort{$a->[1] <=> $b->[1]}' \ - -e ' map{[$_,substr($_,39,3)]}@l;}' perlebcdic.pod + perl -ne 'if(/.{33}\d{1,3}\s{6,8}\d{1,3}\s{6,8}\d{1,3}\s{6,8}\d{1,3}/)'\ + -e '{push(@l,$_)}' \ + -e 'END{print map{$_->[0]}' \ + -e ' sort{$a->[1] <=> $b->[1]}' \ + -e ' map{[$_,substr($_,51,3)]}@l;}' perlebcdic.pod -If you would rather see it in POSIX-BC order then change the number -39 in the last line to 44, like this: +If you would rather see it in POSIX-BC order then change the digit +51 in the last line to 60, like this: =over 4 -=item recipe 6 +=item recipe 4 =back - perl \ - -ne 'if(/.{29}\d{1,3}\s{2,4}\d{1,3}\s{2,4}\d{1,3}\s{2,4}\d{1,3}/)'\ + perl -ne 'if(/.{33}\d{1,3}\s{6,8}\d{1,3}\s{6,8}\d{1,3}\s{6,8}\d{1,3}/)'\ -e '{push(@l,$_)}' \ -e 'END{print map{$_->[0]}' \ - -e ' sort{$a->[1] <=> $b->[1]}' \ - -e ' map{[$_,substr($_,44,3)]}@l;}' perlebcdic.pod + -e ' sort{$a->[1] <=> $b->[1]}' \ + -e ' map{[$_,substr($_,60,3)]}@l;}' perlebcdic.pod =head1 IDENTIFYING CHARACTER CODE SETS -To determine the character set you are running under from perl one -could use the return value of ord() or chr() to test one or more +To determine the character set you are running under from perl one +could use the return value of ord() or chr() to test one or more character values. For example: $is_ascii = "A" eq chr(65); @@ -673,13 +477,13 @@ However, it would be unwise to write tests such as: $is_ascii = "\r" ne chr(13); # WRONG $is_ascii = "\n" ne chr(10); # ILL ADVISED -Obviously the first of these will fail to distinguish most ASCII platforms -from either a CCSID 0037, a 1047, or a POSIX-BC EBCDIC platform since "\r" eq -chr(13) under all of those coded character sets. But note too that -because "\n" is chr(13) and "\r" is chr(10) on the Macintosh (which is an -ASCII platform) the second C<$is_ascii> test will lead to trouble there. +Obviously the first of these will fail to distinguish most ASCII machines +from either a CCSID 0037, a 1047, or a POSIX-BC EBCDIC machine since "\r" eq +chr(13) under all of those coded character sets. But note too that +because "\n" is chr(13) and "\r" is chr(10) on the MacIntosh (which is an +ASCII machine) the second C<$is_ascii> test will lead to trouble there. -To determine whether or not perl was built under an EBCDIC +To determine whether or not perl was built under an EBCDIC code page you can use the Config module like so: use Config; @@ -687,59 +491,49 @@ code page you can use the Config module like so: =head1 CONVERSIONS -=head2 C<utf8::unicode_to_native()> and C<utf8::native_to_unicode()> - -These functions take an input numeric code point in one encoding and -return what its equivalent value is in the other. - =head2 tr/// -In order to convert a string of characters from one character set to +In order to convert a string of characters from one character set to another a simple list of numbers, such as in the right columns in the -above table, along with perl's tr/// operator is all that is needed. -The data in the table are in ASCII/Latin1 order, hence the EBCDIC columns -provide easy-to-use ASCII/Latin1 to EBCDIC operations that are also easily +above table, along with perl's tr/// operator is all that is needed. +The data in the table are in ASCII order hence the EBCDIC columns +provide easy to use ASCII to EBCDIC operations that are also easily reversed. -For example, to convert ASCII/Latin1 to code page 037 take the output of the -second numbers column from the output of recipe 2 (modified to add '\' -characters), and use it in tr/// like so: - - $cp_037 = - '\x00\x01\x02\x03\x37\x2D\x2E\x2F\x16\x05\x25\x0B\x0C\x0D\x0E\x0F' . - '\x10\x11\x12\x13\x3C\x3D\x32\x26\x18\x19\x3F\x27\x1C\x1D\x1E\x1F' . - '\x40\x5A\x7F\x7B\x5B\x6C\x50\x7D\x4D\x5D\x5C\x4E\x6B\x60\x4B\x61' . - '\xF0\xF1\xF2\xF3\xF4\xF5\xF6\xF7\xF8\xF9\x7A\x5E\x4C\x7E\x6E\x6F' . - '\x7C\xC1\xC2\xC3\xC4\xC5\xC6\xC7\xC8\xC9\xD1\xD2\xD3\xD4\xD5\xD6' . - '\xD7\xD8\xD9\xE2\xE3\xE4\xE5\xE6\xE7\xE8\xE9\xBA\xE0\xBB\xB0\x6D' . - '\x79\x81\x82\x83\x84\x85\x86\x87\x88\x89\x91\x92\x93\x94\x95\x96' . - '\x97\x98\x99\xA2\xA3\xA4\xA5\xA6\xA7\xA8\xA9\xC0\x4F\xD0\xA1\x07' . - '\x20\x21\x22\x23\x24\x15\x06\x17\x28\x29\x2A\x2B\x2C\x09\x0A\x1B' . - '\x30\x31\x1A\x33\x34\x35\x36\x08\x38\x39\x3A\x3B\x04\x14\x3E\xFF' . - '\x41\xAA\x4A\xB1\x9F\xB2\x6A\xB5\xBD\xB4\x9A\x8A\x5F\xCA\xAF\xBC' . - '\x90\x8F\xEA\xFA\xBE\xA0\xB6\xB3\x9D\xDA\x9B\x8B\xB7\xB8\xB9\xAB' . - '\x64\x65\x62\x66\x63\x67\x9E\x68\x74\x71\x72\x73\x78\x75\x76\x77' . - '\xAC\x69\xED\xEE\xEB\xEF\xEC\xBF\x80\xFD\xFE\xFB\xFC\xAD\xAE\x59' . - '\x44\x45\x42\x46\x43\x47\x9C\x48\x54\x51\x52\x53\x58\x55\x56\x57' . - '\x8C\x49\xCD\xCE\xCB\xCF\xCC\xE1\x70\xDD\xDE\xDB\xDC\x8D\x8E\xDF'; +For example, to convert ASCII to code page 037 take the output of the second +column from the output of recipe 0 (modified to add \\ characters) and use +it in tr/// like so: + + $cp_037 = + '\000\001\002\003\234\011\206\177\227\215\216\013\014\015\016\017' . + '\020\021\022\023\235\205\010\207\030\031\222\217\034\035\036\037' . + '\200\201\202\203\204\012\027\033\210\211\212\213\214\005\006\007' . + '\220\221\026\223\224\225\226\004\230\231\232\233\024\025\236\032' . + '\040\240\342\344\340\341\343\345\347\361\242\056\074\050\053\174' . + '\046\351\352\353\350\355\356\357\354\337\041\044\052\051\073\254' . + '\055\057\302\304\300\301\303\305\307\321\246\054\045\137\076\077' . + '\370\311\312\313\310\315\316\317\314\140\072\043\100\047\075\042' . + '\330\141\142\143\144\145\146\147\150\151\253\273\360\375\376\261' . + '\260\152\153\154\155\156\157\160\161\162\252\272\346\270\306\244' . + '\265\176\163\164\165\166\167\170\171\172\241\277\320\335\336\256' . + '\136\243\245\267\251\247\266\274\275\276\133\135\257\250\264\327' . + '\173\101\102\103\104\105\106\107\110\111\255\364\366\362\363\365' . + '\175\112\113\114\115\116\117\120\121\122\271\373\374\371\372\377' . + '\134\367\123\124\125\126\127\130\131\132\262\324\326\322\323\325' . + '\060\061\062\063\064\065\066\067\070\071\263\333\334\331\332\237' ; my $ebcdic_string = $ascii_string; eval '$ebcdic_string =~ tr/\000-\377/' . $cp_037 . '/'; -To convert from EBCDIC 037 to ASCII just reverse the order of the tr/// +To convert from EBCDIC 037 to ASCII just reverse the order of the tr/// arguments like so: my $ascii_string = $ebcdic_string; - eval '$ascii_string =~ tr/' . $cp_037 . '/\000-\377/'; + eval '$ascii_string = tr/' . $cp_037 . '/\000-\377/'; -Similarly one could take the output of the third numbers column from recipe 2 -to obtain a C<$cp_1047> table. The fourth numbers column of the output from -recipe 2 could provide a C<$cp_posix_bc> table suitable for transcoding as -well. - -If you wanted to see the inverse tables, you would first have to sort on the -desired numbers column as in recipes 4, 5 or 6, then take the output of the -first numbers column. +Similarly one could take the output of the third column from recipe 0 to +obtain a C<$cp_1047> table. The fourth column of the output from recipe +0 could provide a C<$cp_posix_bc> table suitable for transcoding as well. =head2 iconv @@ -747,102 +541,97 @@ XPG operability often implies the presence of an I<iconv> utility available from the shell or from the C library. Consult your system's documentation for information on iconv. -On OS/390 or z/OS see the iconv(1) manpage. One way to invoke the iconv +On OS/390 see the iconv(1) man page. One way to invoke the iconv shell utility from within perl would be to: - # OS/390 or z/OS example + # OS/390 example $ascii_data = `echo '$ebcdic_data'| iconv -f IBM-1047 -t ISO8859-1` or the inverse map: - # OS/390 or z/OS example + # OS/390 example $ebcdic_data = `echo '$ascii_data'| iconv -f ISO8859-1 -t IBM-1047` -For other perl-based conversion options see the Convert::* modules on CPAN. +For other perl based conversion options see the Convert::* modules on CPAN. =head2 C RTL -The OS/390 and z/OS C run-time libraries provide _atoe() and _etoa() functions. +The OS/390 C run time library provides _atoe() and _etoa() functions. =head1 OPERATOR DIFFERENCES -The C<..> range operator treats certain character ranges with -care on EBCDIC platforms. For example the following array -will have twenty six elements on either an EBCDIC platform -or an ASCII platform: +The C<..> range operator treats certain character ranges with +care on EBCDIC machines. For example the following array +will have twenty six elements on either an EBCDIC machine +or an ASCII machine: @alphabet = ('A'..'Z'); # $#alphabet == 25 The bitwise operators such as & ^ | may return different results -when operating on string or character data in a perl program running -on an EBCDIC platform than when run on an ASCII platform. Here is +when operating on string or character data in a perl program running +on an EBCDIC machine than when run on an ASCII machine. Here is an example adapted from the one in L<perlop>: # EBCDIC-based examples print "j p \n" ^ " a h"; # prints "JAPH\n" - print "JA" | " ph\n"; # prints "japh\n" + print "JA" | " ph\n"; # prints "japh\n" print "JAPH\nJunk" & "\277\277\277\277\277"; # prints "japh\n"; print 'p N$' ^ " E<H\n"; # prints "Perl\n"; An interesting property of the 32 C0 control characters in the ASCII table is that they can "literally" be constructed -as control characters in perl, e.g. C<(chr(0)> eq C<\c@>)> -C<(chr(1)> eq C<\cA>)>, and so on. Perl on EBCDIC platforms has been -ported to take C<\c@> to chr(0) and C<\cA> to chr(1), etc. as well, but the -characters that result depend on which code page you are -using. The table below uses the standard acronyms for the controls. -The POSIX-BC and 1047 sets are -identical throughout this range and differ from the 0037 set at only +as control characters in perl, e.g. C<(chr(0) eq "\c@")> +C<(chr(1) eq "\cA")>, and so on. Perl on EBCDIC machines has been +ported to take "\c@" to chr(0) and "\cA" to chr(1) as well, but the +thirty three characters that result depend on which code page you are +using. The table below uses the character names from the previous table +but with substitutions such as s/START OF/S.O./; s/END OF /E.O./; +s/TRANSMISSION/TRANS./; s/TABULATION/TAB./; s/VERTICAL/VERT./; +s/HORIZONTAL/HORIZ./; s/DEVICE CONTROL/D.C./; s/SEPARATOR/SEP./; +s/NEGATIVE ACKNOWLEDGE/NEG. ACK./;. The POSIX-BC and 1047 sets are +identical throughout this range and differ from the 0037 set at only one spot (21 decimal). Note that the C<LINE FEED> character -may be generated by C<\cJ> on ASCII platforms but by C<\cU> on 1047 or POSIX-BC -platforms and cannot be generated as a C<"\c.letter."> control character on -0037 platforms. Note also that C<\c\> cannot be the final element in a string -or regex, as it will absorb the terminator. But C<\c\I<X>> is a C<FILE -SEPARATOR> concatenated with I<X> for all I<X>. -The outlier C<\c?> on ASCII, which yields a non-C0 control C<DEL>, -yields the outlier control C<APC> on EBCDIC, the one that isn't in the -block of contiguous controls. - - chr ord 8859-1 0037 1047 && POSIX-BC - ----------------------------------------------------------------------- - \c@ 0 <NUL> <NUL> <NUL> - \cA 1 <SOH> <SOH> <SOH> - \cB 2 <STX> <STX> <STX> - \cC 3 <ETX> <ETX> <ETX> - \cD 4 <EOT> <ST> <ST> - \cE 5 <ENQ> <HT> <HT> - \cF 6 <ACK> <SSA> <SSA> - \cG 7 <BEL> <DEL> <DEL> - \cH 8 <BS> <EPA> <EPA> - \cI 9 <HT> <RI> <RI> - \cJ 10 <LF> <SS2> <SS2> - \cK 11 <VT> <VT> <VT> - \cL 12 <FF> <FF> <FF> - \cM 13 <CR> <CR> <CR> - \cN 14 <SO> <SO> <SO> - \cO 15 <SI> <SI> <SI> - \cP 16 <DLE> <DLE> <DLE> - \cQ 17 <DC1> <DC1> <DC1> - \cR 18 <DC2> <DC2> <DC2> - \cS 19 <DC3> <DC3> <DC3> - \cT 20 <DC4> <OSC> <OSC> - \cU 21 <NAK> <NEL> <LF> ** - \cV 22 <SYN> <BS> <BS> - \cW 23 <ETB> <ESA> <ESA> - \cX 24 <CAN> <CAN> <CAN> - \cY 25 <EOM> <EOM> <EOM> - \cZ 26 <SUB> <PU2> <PU2> - \c[ 27 <ESC> <SS3> <SS3> - \c\X 28 <FS>X <FS>X <FS>X - \c] 29 <GS> <GS> <GS> - \c^ 30 <RS> <RS> <RS> - \c_ 31 <US> <US> <US> - \c? * <DEL> <APC> <APC> - -C<*> Note: C<\c?> maps to ordinal 127 (C<DEL>) on ASCII platforms, but -since ordinal 127 is a not a control character on EBCDIC machines, -C<\c?> instead maps to C<APC>, which is 255 in 0037 and 1047, and 95 in -POSIX-BC. +may be generated by "\cJ" on ASCII machines but by "\cU" on 1047 or POSIX-BC +machines and cannot be generated as a C<"\c.letter."> control character on +0037 machines. Note also that "\c\\" maps to two characters +not one. + + chr ord 8859-1 0037 1047 && POSIX-BC + ------------------------------------------------------------------------ + "\c?" 127 <DELETE> " " ***>< + "\c@" 0 <NULL> <NULL> <NULL> ***>< + "\cA" 1 <S.O. HEADING> <S.O. HEADING> <S.O. HEADING> + "\cB" 2 <S.O. TEXT> <S.O. TEXT> <S.O. TEXT> + "\cC" 3 <E.O. TEXT> <E.O. TEXT> <E.O. TEXT> + "\cD" 4 <E.O. TRANS.> <C1 28> <C1 28> + "\cE" 5 <ENQUIRY> <HORIZ. TAB.> <HORIZ. TAB.> + "\cF" 6 <ACKNOWLEDGE> <C1 6> <C1 6> + "\cG" 7 <BELL> <DELETE> <DELETE> + "\cH" 8 <BACKSPACE> <C1 23> <C1 23> + "\cI" 9 <HORIZ. TAB.> <C1 13> <C1 13> + "\cJ" 10 <LINE FEED> <C1 14> <C1 14> + "\cK" 11 <VERT. TAB.> <VERT. TAB.> <VERT. TAB.> + "\cL" 12 <FORM FEED> <FORM FEED> <FORM FEED> + "\cM" 13 <CARRIAGE RETURN> <CARRIAGE RETURN> <CARRIAGE RETURN> + "\cN" 14 <SHIFT OUT> <SHIFT OUT> <SHIFT OUT> + "\cO" 15 <SHIFT IN> <SHIFT IN> <SHIFT IN> + "\cP" 16 <DATA LINK ESCAPE> <DATA LINK ESCAPE> <DATA LINK ESCAPE> + "\cQ" 17 <D.C. ONE> <D.C. ONE> <D.C. ONE> + "\cR" 18 <D.C. TWO> <D.C. TWO> <D.C. TWO> + "\cS" 19 <D.C. THREE> <D.C. THREE> <D.C. THREE> + "\cT" 20 <D.C. FOUR> <C1 29> <C1 29> + "\cU" 21 <NEG. ACK.> <C1 5> <LINE FEED> *** + "\cV" 22 <SYNCHRONOUS IDLE> <BACKSPACE> <BACKSPACE> + "\cW" 23 <E.O. TRANS. BLOCK> <C1 7> <C1 7> + "\cX" 24 <CANCEL> <CANCEL> <CANCEL> + "\cY" 25 <E.O. MEDIUM> <E.O. MEDIUM> <E.O. MEDIUM> + "\cZ" 26 <SUBSTITUTE> <C1 18> <C1 18> + "\c[" 27 <ESCAPE> <C1 15> <C1 15> + "\c\\" 28 <FILE SEP.>\ <FILE SEP.>\ <FILE SEP.>\ + "\c]" 29 <GROUP SEP.> <GROUP SEP.> <GROUP SEP.> + "\c^" 30 <RECORD SEP.> <RECORD SEP.> <RECORD SEP.> ***>< + "\c_" 31 <UNIT SEP.> <UNIT SEP.> <UNIT SEP.> ***>< + =head1 FUNCTION DIFFERENCES @@ -850,21 +639,21 @@ POSIX-BC. =item chr() -chr() must be given an EBCDIC code number argument to yield a desired -character return value on an EBCDIC platform. For example: +chr() must be given an EBCDIC code number argument to yield a desired +character return value on an EBCDIC machine. For example: $CAPITAL_LETTER_A = chr(193); =item ord() -ord() will return EBCDIC code number values on an EBCDIC platform. +ord() will return EBCDIC code number values on an EBCDIC machine. For example: $the_number_193 = ord("A"); =item pack() -The c and C templates for pack() are dependent upon character set +The c and C templates for pack() are dependent upon character set encoding. Examples of usage on EBCDIC include: $foo = pack("CCCC",193,194,195,196); @@ -880,33 +669,33 @@ encoding. Examples of usage on EBCDIC include: One must be careful with scalars and strings that are passed to print that contain ASCII encodings. One common place for this to occur is in the output of the MIME type header for -CGI script writing. For example, many perl programming guides +CGI script writing. For example, many perl programming guides recommend something similar to: - print "Content-type:\ttext/html\015\012\015\012"; + print "Content-type:\ttext/html\015\012\015\012"; # this may be wrong on EBCDIC -Under the IBM OS/390 USS Web Server or WebSphere on z/OS for example -you should instead write that as: +Under the IBM OS/390 USS Web Server for example you should instead +write that as: - print "Content-type:\ttext/html\r\n\r\n"; # OK for DGW et al + print "Content-type:\ttext/html\r\n\r\n"; # OK for DGW et alia That is because the translation from EBCDIC to ASCII is done by the web server in this case (such code will not be appropriate for -the Macintosh however). Consult your web server's documentation for +the Macintosh however). Consult your web server's documentation for further details. =item printf() The formats that can convert characters to numbers and vice versa will be different from their ASCII counterparts when executed -on an EBCDIC platform. Examples include: +on an EBCDIC machine. Examples include: printf("%c%c%c",193,194,195); # prints ABC =item sort() -EBCDIC sort results may differ from ASCII sort results especially for +EBCDIC sort results may differ from ASCII sort results especially for mixed case strings. This is discussed in more detail below. =item sprintf() @@ -924,19 +713,15 @@ See the discussion of pack() above. =head1 REGULAR EXPRESSION DIFFERENCES -As of perl 5.005_03 the letter range regular expressions such as -[A-Z] and [a-z] have been especially coded to not pick up gap -characters. For example, characters such as E<ocirc> C<o WITH CIRCUMFLEX> -that lie between I and J would not be matched by the -regular expression range C</[H-K]/>. This works in -the other direction, too, if either of the range end points is -explicitly numeric: C<[\x89-\x91]> will match C<\x8e>, even -though C<\x89> is C<i> and C<\x91 > is C<j>, and C<\x8e> -is a gap character from the alphabetic viewpoint. - -If you do want to match the alphabet gap characters in a single octet -regular expression try matching the hex or octal code such -as C</\313/> on EBCDIC or C</\364/> on ASCII platforms to +As of perl 5.005_03 the letter range regular expression such as +[A-Z] and [a-z] have been especially coded to not pick up gap +characters. For example, characters such as E<ocirc> C<o WITH CIRCUMFLEX> +that lie between I and J would not be matched by the +regular expression range C</[H-K]/>. + +If you do want to match the alphabet gap characters in a single octet +regular expression try matching the hex or octal code such +as C</\313/> on EBCDIC or C</\364/> on ASCII machines to have your regular expression match C<o WITH CIRCUMFLEX>. Another construct to be wary of is the inappropriate use of hex or @@ -963,59 +748,93 @@ set of subs: $char =~ /[\200-\237]/; } - sub is_latin_1 { # But not ASCII; not C1 + sub is_latin_1 { my $char = substr(shift,0,1); $char =~ /[\240-\377]/; } -These are valid only on ASCII platforms, but can be easily rewritten to -work on any platform as follows: +The above would be adequate if the concern was only with numeric code points. +However, the concern may be with characters rather than code points +and on an EBCDIC machine it may be desirable for constructs such as +C<if (is_print_ascii("A")) {print "A is a printable character\n";}> to print +out the expected message. One way to represent the above collection +of character classification subs that is capable of working across the +four coded character sets discussed in this document is as follows: sub Is_c0 { my $char = substr(shift,0,1); - return $char =~ /[[:cntrl:]]/ - && $char =~ /[[:ascii:]]/ - && ! Is_delete($char); + if (ord('^')==94) { # ascii + return $char =~ /[\000-\037]/; + } + if (ord('^')==176) { # 37 + return $char =~ /[\000-\003\067\055-\057\026\005\045\013-\023\074\075\062\046\030\031\077\047\034-\037]/; + } + if (ord('^')==95 || ord('^')==106) { # 1047 || posix-bc + return $char =~ /[\000-\003\067\055-\057\026\005\025\013-\023\074\075\062\046\030\031\077\047\034-\037]/; + } } sub Is_print_ascii { my $char = substr(shift,0,1); - - return $char =~ /[[:print:]]/ && $char =~ /[[:ascii:]]/; - - # Alternatively: - # return $char - # =~ /[ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~]/; + $char =~ /[ !"\#\$%&'()*+,\-.\/0-9:;<=>?\@A-Z[\\\]^_`a-z{|}~]/; } sub Is_delete { my $char = substr(shift,0,1); - return utf8::native_to_unicode(ord $char) == 0x7F; + if (ord('^')==94) { # ascii + return $char eq "\177"; + } + else { # ebcdic + return $char eq "\007"; + } } sub Is_c1 { - use feature 'unicode_strings'; my $char = substr(shift,0,1); - return $char =~ /[[:cntrl:]]/ && $char !~ /[[:ascii:]]/; + if (ord('^')==94) { # ascii + return $char =~ /[\200-\237]/; + } + if (ord('^')==176) { # 37 + return $char =~ /[\040-\044\025\006\027\050-\054\011\012\033\060\061\032\063-\066\010\070-\073\040\024\076\377]/; + } + if (ord('^')==95) { # 1047 + return $char =~ /[\040-\045\006\027\050-\054\011\012\033\060\061\032\063-\066\010\070-\073\040\024\076\377]/; + } + if (ord('^')==106) { # posix-bc + return $char =~ + /[\040-\045\006\027\050-\054\011\012\033\060\061\032\063-\066\010\070-\073\040\024\076\137]/; + } } - sub Is_latin_1 { # But not ASCII; not C1 - use feature 'unicode_strings'; + sub Is_latin_1 { my $char = substr(shift,0,1); - return ord($char) < 256 - && $char !~ [[:ascii:]] - && $char !~ [[:cntrl:]]; + if (ord('^')==94) { # ascii + return $char =~ /[\240-\377]/; + } + if (ord('^')==176) { # 37 + return $char =~ + /[\101\252\112\261\237\262\152\265\275\264\232\212\137\312\257\274\220\217\352\372\276\240\266\263\235\332\233\213\267\270\271\253\144\145\142\146\143\147\236\150\164\161-\163\170\165-\167\254\151\355\356\353\357\354\277\200\375\376\373\374\255\256\131\104\105\102\106\103\107\234\110\124\121-\123\130\125-\127\214\111\315\316\313\317\314\341\160\335\336\333\334\215\216\337]/; + } + if (ord('^')==95) { # 1047 + return $char =~ + /[\101\252\112\261\237\262\152\265\273\264\232\212\260\312\257\274\220\217\352\372\276\240\266\263\235\332\233\213\267\270\271\253\144\145\142\146\143\147\236\150\164\161-\163\170\165-\167\254\151\355\356\353\357\354\277\200\375\376\373\374\272\256\131\104\105\102\106\103\107\234\110\124\121-\123\130\125-\127\214\111\315\316\313\317\314\341\160\335\336\333\334\215\216\337]/; + } + if (ord('^')==106) { # posix-bc + return $char =~ + /[\101\252\260\261\237\262\320\265\171\264\232\212\272\312\257\241\220\217\352\372\276\240\266\263\235\332\233\213\267\270\271\253\144\145\142\146\143\147\236\150\164\161-\163\170\165-\167\254\151\355\356\353\357\354\277\200\340\376\335\374\255\256\131\104\105\102\106\103\107\234\110\124\121-\123\130\125-\127\214\111\315\316\313\317\314\341\160\300\336\333\334\215\216\337]/; + } } -Another way to write C<Is_latin_1()> would be +Note however that only the C<Is_ascii_print()> sub is really independent +of coded character set. Another way to write C<Is_latin_1()> would be to use the characters in the range explicitly: sub Is_latin_1 { my $char = substr(shift,0,1); - $char =~ /[ ¡¢£¤¥¦§¨©ª«¬Â®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÃÂÃÄÅÆÇÈÉÊËÌÃÃŽÃÃÑÒÓÔÕÖ×ØÙÚÛÜÃÞßà áâãäåæçèéêëìÃîïðñòóôõö÷øùúûüýþÿ]/; + $char =~ /[ ¡¢£¤¥¦§¨©ª«¬®¯°±²³´µ¶·¸¹º»¼½¾¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷øùúûüýþÿ]/; } -Although that form may run into trouble in network transit (due to the +Although that form may run into trouble in network transit (due to the presence of 8 bit characters) or on non ISO-Latin character sets. =head1 SOCKETS @@ -1028,27 +847,27 @@ output. =head1 SORTING -One big difference between ASCII-based character sets and EBCDIC ones +One big difference between ASCII based character sets and EBCDIC ones are the relative positions of upper and lower case letters and the -letters compared to the digits. If sorted on an ASCII-based platform the -two-letter abbreviation for a physician comes before the two letter -abbreviation for drive; that is: +letters compared to the digits. If sorted on an ASCII based machine the +two letter abbreviation for a physician comes before the two letter +for drive, that is: - @sorted = sort(qw(Dr. dr.)); # @sorted holds ('Dr.','dr.') on ASCII, + @sorted = sort(qw(Dr. dr.)); # @sorted holds ('Dr.','dr.') on ASCII, # but ('dr.','Dr.') on EBCDIC -The property of lowercase before uppercase letters in EBCDIC is +The property of lower case before uppercase letters in EBCDIC is even carried to the Latin 1 EBCDIC pages such as 0037 and 1047. -An example would be that E<Euml> C<E WITH DIAERESIS> (203) comes -before E<euml> C<e WITH DIAERESIS> (235) on an ASCII platform, but -the latter (83) comes before the former (115) on an EBCDIC platform. -(Astute readers will note that the uppercase version of E<szlig> -C<SMALL LETTER SHARP S> is simply "SS" and that the upper case version of -E<yuml> C<y WITH DIAERESIS> is not in the 0..255 range but it is +An example would be that E<Euml> C<E WITH DIAERESIS> (203) comes +before E<euml> C<e WITH DIAERESIS> (235) on an ASCII machine, but +the latter (83) comes before the former (115) on an EBCDIC machine. +(Astute readers will note that the upper case version of E<szlig> +C<SMALL LETTER SHARP S> is simply "SS" and that the upper case version of +E<yuml> C<y WITH DIAERESIS> is not in the 0..255 range but it is at U+x0178 in Unicode, or C<"\x{178}"> in a Unicode enabled Perl). The sort order will cause differences between results obtained on -ASCII platforms versus EBCDIC platforms. What follows are some suggestions +ASCII machines versus EBCDIC machines. What follows are some suggestions on how to deal with these differences. =head2 Ignore ASCII vs. EBCDIC sort differences. @@ -1058,26 +877,26 @@ some user education. =head2 MONO CASE then sort data. -In order to minimize the expense of mono casing mixed-case text, try to +In order to minimize the expense of mono casing mixed test try to C<tr///> towards the character set case most employed within the data. If the data are primarily UPPERCASE non Latin 1 then apply tr/[a-z]/[A-Z]/ then sort(). If the data are primarily lowercase non Latin 1 then apply tr/[A-Z]/[a-z]/ before sorting. If the data are primarily UPPERCASE -and include Latin-1 characters then apply: +and include Latin-1 characters then apply: - tr/[a-z]/[A-Z]/; - tr/[à áâãäåæçèéêëìÃîïðñòóôõöøùúûüýþ]/[ÀÃÂÃÄÅÆÇÈÉÊËÌÃÃŽÃÃÑÒÓÔÕÖØÙÚÛÜÃÞ/; - s/ß/SS/g; + tr/[a-z]/[A-Z]/; + tr/[àáâãäåæçèéêëìíîïðñòóôõöøùúûüýþ]/[ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖØÙÚÛÜÝÞ]/; + s/ß/SS/g; -then sort(). Do note however that such Latin-1 manipulation does not -address the E<yuml> C<y WITH DIAERESIS> character that will remain at -code point 255 on ASCII platforms, but 223 on most EBCDIC platforms -where it will sort to a place less than the EBCDIC numerals. With a -Unicode-enabled Perl you might try: +then sort(). Do note however that such Latin-1 manipulation does not +address the E<yuml> C<y WITH DIAERESIS> character that will remain at +code point 255 on ASCII machines, but 223 on most EBCDIC machines +where it will sort to a place less than the EBCDIC numerals. With a +Unicode enabled Perl you might try: tr/^?/\x{178}/; -The strategy of mono casing data before sorting does not preserve the case +The strategy of mono casing data before sorting does not preserve the case of the data and may not be acceptable for that reason. =head2 Convert, sort data, then re convert. @@ -1085,22 +904,22 @@ of the data and may not be acceptable for that reason. This is the most expensive proposition that does not employ a network connection. -=head2 Perform sorting on one type of platform only. +=head2 Perform sorting on one type of machine only. This strategy can employ a network connection. As such it would be computationally expensive. -=head1 TRANSFORMATION FORMATS +=head1 TRANFORMATION FORMATS -There are a variety of ways of transforming data with an intra character set -mapping that serve a variety of purposes. Sorting was discussed in the -previous section and a few of the other more popular mapping techniques are +There are a variety of ways of transforming data with an intra character set +mapping that serve a variety of purposes. Sorting was discussed in the +previous section and a few of the other more popular mapping techniques are discussed next. =head2 URL decoding and encoding Note that some URLs have hexadecimal ASCII code points in them in an -attempt to overcome character or protocol limitation issues. For example +attempt to overcome character or protocol limitation issues. For example the tilde character is not on every keyboard hence a URL of the form: http://www.pvhp.com/~pvhp/ @@ -1136,7 +955,7 @@ of decoding such a URL under CCSID 1047: ); $url =~ s/%([0-9a-fA-F]{2})/pack("c",$a2e_1047[hex($1)])/ge; -Conversely, here is a partial solution for the task of encoding such +Conversely, here is a partial solution for the task of encoding such a URL under the 1047 code page: $url = 'http://www.pvhp.com/~pvhp/'; @@ -1159,12 +978,11 @@ a URL under the 1047 code page: 92,247, 83, 84, 85, 86, 87, 88, 89, 90,178,212,214,210,211,213, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57,179,219,220,217,218,159 ); - # The following regular expression does not address the - # mappings for: ('.' => '%2E', '/' => '%2F', ':' => '%3A') - $url =~ s/([\t "#%&\(\),;<=>\?\@\[\\\]^`{|}~])/ - sprintf("%%%02X",$e2a_1047[ord($1)])/xge; + # The following regular expression does not address the + # mappings for: ('.' => '%2E', '/' => '%2F', ':' => '%3A') + $url =~ s/([\t "#%&\(\),;<=>\?\@\[\\\]^`{|}~])/sprintf("%%%02X",$e2a_1047[ord($1)])/ge; -where a more complete solution would split the URL into components +where a more complete solution would split the URL into components and apply a full s/// substitution only to the appropriate parts. In the remaining examples a @e2a or @a2e array may be employed @@ -1173,14 +991,14 @@ you could use the @a2e_1047 or @e2a_1047 arrays just shown. =head2 uu encoding and decoding -The C<u> template to pack() or unpack() will render EBCDIC data in EBCDIC -characters equivalent to their ASCII counterparts. For example, the +The C<u> template to pack() or unpack() will render EBCDIC data in EBCDIC +characters equivalent to their ASCII counterparts. For example, the following will print "Yes indeed\n" on either an ASCII or EBCDIC computer: $all_byte_chrs = ''; for (0..255) { $all_byte_chrs .= chr($_); } $uuencode_byte_chrs = pack('u', $all_byte_chrs); - ($uu = <<'ENDOFHEREDOC') =~ s/^\s*//gm; + ($uu = <<' ENDOFHEREDOC') =~ s/^\s*//gm; M``$"`P0%!@<("0H+#`T.#Q`1$A,4%187&!D:&QP='A\@(2(C)"4F)R@I*BLL M+2XO,#$R,S0U-C<X.3H[/#T^/T!!0D-$149'2$E*2TQ-3D]045)35%565UA9 M6EM<75Y?8&%B8V1E9F=H:6IK;&UN;W!Q<G-T=79W>'EZ>WQ]?G^`@8*#A(6& @@ -1217,14 +1035,14 @@ that the @e2a array is filled in appropriately: =head2 Quoted-Printable encoding and decoding -On ASCII-encoded platforms it is possible to strip characters outside of +On ASCII encoded machines it is possible to strip characters outside of the printable set using: # This QP encoder works on ASCII only $qp_string =~ s/([=\x00-\x1F\x80-\xFF])/sprintf("=%02X",ord($1))/ge; -Whereas a QP encoder that works on both ASCII and EBCDIC platforms -would look somewhat like the following (where the EBCDIC branch @e2a +Whereas a QP encoder that works on both ASCII and EBCDIC machines +would look somewhat like the following (where the EBCDIC branch @e2a array is omitted for brevity): if (ord('A') == 65) { # ASCII @@ -1236,11 +1054,10 @@ array is omitted for brevity): @e2a = # EBCDIC to ASCII map (as shown above) } $qp_string =~ - s/([^ !"\#\$%&'()*+,\-.\/0-9:;<>?\@A-Z[\\\]^_`a-z{|}~$delete])/ - sprintf("=%02X",$e2a[ord($1)])/xge; + s/([^ !"\#\$%&'()*+,\-.\/0-9:;<>?\@A-Z[\\\]^_`a-z{|}~$delete])/sprintf("=%02X",$e2a[ord($1)])/ge; (although in production code the substitutions might be done -in the EBCDIC branch with the @e2a array and separately in the +in the EBCDIC branch with the @e2a array and separately in the ASCII branch without the expense of the identity map). Such QP strings can be decoded with: @@ -1249,25 +1066,25 @@ Such QP strings can be decoded with: $string =~ s/=([0-9A-Fa-f][0-9A-Fa-f])/chr hex $1/ge; $string =~ s/=[\n\r]+$//; -Whereas a QP decoder that works on both ASCII and EBCDIC platforms +Whereas a QP decoder that works on both ASCII and EBCDIC machines would look somewhat like the following (where the @a2e array is omitted for brevity): $string =~ s/=([0-9A-Fa-f][0-9A-Fa-f])/chr $a2e[hex $1]/ge; $string =~ s/=[\n\r]+$//; -=head2 Caesarean ciphers +=head2 Caesarian cyphers The practice of shifting an alphabet one or more characters for encipherment dates back thousands of years and was explicitly detailed by Gaius Julius -Caesar in his B<Gallic Wars> text. A single alphabet shift is sometimes +Caesar in his B<Gallic Wars> text. A single alphabet shift is sometimes referred to as a rotation and the shift amount is given as a number $n after -the string 'rot' or "rot$n". Rot0 and rot26 would designate identity maps -on the 26-letter English version of the Latin alphabet. Rot13 has the -interesting property that alternate subsequent invocations are identity maps -(thus rot13 is its own non-trivial inverse in the group of 26 alphabet -rotations). Hence the following is a rot13 encoder and decoder that will -work on ASCII and EBCDIC platforms: +the string 'rot' or "rot$n". Rot0 and rot26 would designate identity maps +on the 26 letter English version of the Latin alphabet. Rot13 has the +interesting property that alternate subsequent invocations are identity maps +(thus rot13 is its own non-trivial inverse in the group of 26 alphabet +rotations). Hence the following is a rot13 encoder and decoder that will +work on ASCII and EBCDIC machines: #!/usr/local/bin/perl @@ -1283,39 +1100,28 @@ In one-liner form: =head1 Hashing order and checksums -To the extent that it is possible to write code that depends on -hashing order there may be differences between hashes as stored -on an ASCII-based platform and hashes stored on an EBCDIC-based platform. XXX =head1 I18N AND L10N -Internationalization (I18N) and localization (L10N) are supported at least -in principle even on EBCDIC platforms. The details are system-dependent +Internationalization(I18N) and localization(L10N) are supported at least +in principle even on EBCDIC machines. The details are system dependent and discussed under the L<perlebcdic/OS ISSUES> section below. -=head1 MULTI-OCTET CHARACTER SETS +=head1 MULTI OCTET CHARACTER SETS -Perl may work with an internal UTF-EBCDIC encoding form for wide characters -on EBCDIC platforms in a manner analogous to the way that it works with -the UTF-8 internal encoding form on ASCII based platforms. - -Legacy multi byte EBCDIC code pages XXX. +Multi byte EBCDIC code pages; Unicode, UTF-8, UTF-EBCDIC, XXX. =head1 OS ISSUES -There may be a few system-dependent issues +There may be a few system dependent issues of concern to EBCDIC Perl programmers. -=head2 OS/400 - -=over 8 +=head2 OS/400 -=item PASE +The PASE environment. -The PASE environment is a runtime environment for OS/400 that can run -executables built for PowerPC AIX in OS/400; see L<perlos400>. PASE -is ASCII-based, not EBCDIC-based as the ILE. +=over 8 =item IFS access @@ -1323,7 +1129,7 @@ XXX. =back -=head2 OS/390, z/OS +=head2 OS/390 Perl runs under Unix Systems Services or USS. @@ -1331,8 +1137,8 @@ Perl runs under Unix Systems Services or USS. =item chcp -B<chcp> is supported as a shell utility for displaying and changing -one's code page. See also L<chcp(1)>. +B<chcp> is supported as a shell utility for displaying and changing +one's code page. See also L<chcp>. =item dataset access @@ -1346,60 +1152,68 @@ or: See also the OS390::Stdio module on CPAN. -=item OS/390, z/OS iconv +=item OS/390 iconv B<iconv> is supported as both a shell utility and a C RTL routine. See also the iconv(1) and iconv(3) manual pages. =item locales -On OS/390 or z/OS see L<locale> for information on locales. The L10N files -are in F</usr/nls/locale>. $Config{d_setlocale} is 'define' on OS/390 -or z/OS. +On OS/390 see L<locale> for information on locales. The L10N files +are in F</usr/nls/locale>. $Config{d_setlocale} is 'define' on OS/390. =back +=head2 VM/ESA? + +XXX. + =head2 POSIX-BC? XXX. =head1 BUGS -This pod document contains literal Latin 1 characters and may encounter -translation difficulties. In particular one popular nroff implementation -was known to strip accented characters to their unaccented counterparts -while attempting to view this document through the B<pod2man> program -(for example, you may see a plain C<y> rather than one with a diaeresis -as in E<yuml>). Another nroff truncated the resultant manpage at -the first occurrence of 8 bit characters. +This pod document contains literal Latin 1 characters and may encounter +translation difficulties. In particular one popular nroff implementation +was known to strip accented characters to their unaccented counterparts +while attempting to view this document through the B<pod2man> program +(for example, you may see a plain C<y> rather than one with a diaeresis +as in E<yuml>). Another nroff truncated the resultant man page at +the first occurence of 8 bit characters. Not all shells will allow multiple C<-e> string arguments to perl to -be concatenated together properly as recipes 0, 2, 4, 5, and 6 might -seem to imply. +be concatenated together properly as recipes 2, 3, and 4 might seem +to imply. + +Perl does not yet work with any Unicode features on EBCDIC platforms. =head1 SEE ALSO -L<perllocale>, L<perlfunc>, L<perlunicode>, L<utf8>. +L<perllocale>, L<perlfunc>. =head1 REFERENCES -L<http://anubis.dkuug.dk/i18n/charmaps> +http://anubis.dkuug.dk/i18n/charmaps -L<http://www.unicode.org/> +http://www.unicode.org/ -L<http://www.unicode.org/unicode/reports/tr16/> +http://www.unicode.org/unicode/reports/tr16/ -L<http://www.wps.com/projects/codes/> +http://www.wps.com/texts/codes/ B<ASCII: American Standard Code for Information Infiltration> Tom Jennings, September 1999. -B<The Unicode Standard, Version 3.0> The Unicode Consortium, Lisa Moore ed., -ISBN 0-201-61633-5, Addison Wesley Developers Press, February 2000. +B<The Unicode Standard Version 2.0> The Unicode Consortium, +ISBN 0-201-48345-9, Addison Wesley Developers Press, July 1996. -B<CDRA: IBM - Character Data Representation Architecture - -Reference and Registry>, IBM SC09-2190-00, December 1996. +B<The Unicode Standard Version 3.0> The Unicode Consortium, Lisa Moore ed., +ISBN 0-201-61633-5, Addison Wesley Developers Press, February 2000. -"Demystifying Character Sets", Andrea Vine, Multilingual Computing +B<CDRA: IBM - Character Data Representation Architecture - +Reference and Registry>, IBM SC09-2190-00, December 1996. + +"Demystifying Character Sets", Andrea Vine, Multilingual Computing & Technology, B<#26 Vol. 10 Issue 4>, August/September 1999; ISSN 1523-0309; Multilingual Computing Inc. Sandpoint ID, USA. @@ -1407,20 +1221,15 @@ B<Codes, Ciphers, and Other Cryptic and Clandestine Communication> Fred B. Wrixon, ISBN 1-57912-040-7, Black Dog & Leventhal Publishers, 1998. -L<http://www.bobbemer.com/P-BIT.HTM> -B<IBM - EBCDIC and the P-bit; The biggest Computer Goof Ever> Robert Bemer. - -=head1 HISTORY - -15 April 2001: added UTF-8 and UTF-EBCDIC to main table, pvhp. - =head1 AUTHOR -Peter Prymmer pvhp@best.com wrote this in 1999 and 2000 -with CCSID 0819 and 0037 help from Chris Leach and -AndrE<eacute> Pirard A.Pirard@ulg.ac.be as well as POSIX-BC +Peter Prymmer pvhp@best.com wrote this in 1999 and 2000 +with CCSID 0819 and 0037 help from Chris Leach and +AndrE<eacute> Pirard A.Pirard@ulg.ac.be as well as POSIX-BC help from Thomas Dorner Thomas.Dorner@start.de. -Thanks also to Vickie Cooper, Philip Newton, William Raffloer, and -Joe Smith. Trademarks, registered trademarks, service marks and -registered service marks used in this document are the property of +Thanks also to Vickie Cooper, Philip Newton, William Raffloer, and +Joe Smith. Trademarks, registered trademarks, service marks and +registered service marks used in this document are the property of their respective owners. + + diff --git a/gnu/usr.bin/perl/pod/perlmodlib.PL b/gnu/usr.bin/perl/pod/perlmodlib.PL index c6b9a701113..0cdadb76c79 100644 --- a/gnu/usr.bin/perl/pod/perlmodlib.PL +++ b/gnu/usr.bin/perl/pod/perlmodlib.PL @@ -1,120 +1,74 @@ #!../miniperl -use strict; -use warnings; - -$ENV{LC_ALL} = 'C'; - -my $Quiet; -@ARGV = grep { not($_ eq '-q' and $Quiet = 1) } @ARGV; - -if (@ARGV) { - my $workdir = shift; - chdir $workdir - or die "Couldn't chdir to '$workdir': $!"; -} -require 'regen/regen_lib.pl'; - -# MANIFEST itself is Unix style filenames, so we have to assume that Unix style -# filenames will work. - -open MANIFEST, '<', 'MANIFEST' - or die "Can't open MANIFEST: $!"; -my @files = - grep !m#/perl.*\.pod#, - grep m#(?:\.pm|\.pod|_pm\.PL)#, - map {s/\s.*//s; $_} - grep { m#^(lib|ext|dist|cpan)/# && !m#/(?:t|demo)/# } - <MANIFEST>; -close MANIFEST - or die "$0: failed to close MANIFEST: $!"; - -my $out = open_new('pod/perlmodlib.pod', undef, - {by => "$0 extracting documentation", - from => 'the Perl source files'}, 1); - -my %exceptions = ( - 'abbrev' => 'Text::Abbrev', - 'carp' => 'Carp', - 'getopt' => 'Getopt::Std', - 'B<CGI::Carp>' => 'CGI::Carp', - 'ModuleInfo' => 'Module::Build::ModuleInfo', - '$notes_name' => 'Module::Build::Notes', - 'Encode::MIME::NAME' => 'Encode::MIME::Name', - 'libnetFAQ' => 'Net::libnetFAQ', -); - +open (OUT, ">perlmodlib.tmp") or die $!; my (@pragma, @mod); - -for my $filename (@files) { - unless (open MOD, '<', $filename) { - warn "Couldn't open $filename: $!"; - next; - } - - my ($name, $thing); - my $foundit = 0; - { - local $/ = ""; - while (<MOD>) { - next unless /^=head1 NAME/; - $foundit++; - last; - } - } - unless ($foundit) { - warn "$filename missing =head1 NAME (OK if respective .pod exists)\n" - unless $Quiet; - next; - } - my $title = <MOD>; - chomp $title; - close MOD - or die "Error closing $filename: $!"; - - ($name, $thing) = split / --? /, $title, 2; - - unless ($name and $thing) { - warn "$filename missing name\n" unless $name; - warn "$filename missing thing\n" unless $thing or $Quiet; - next; - } - - $name =~ s/[^A-Za-z0-9_:\$<>].*//; - $name = $exceptions{$name} || $name; - $thing =~ s/^perl pragma to //i; - $thing = ucfirst $thing; - $title = "=item $name\n\n$thing\n\n"; - - if ($name =~ /[A-Z]/) { - push @mod, $title; - } else { - push @pragma, $title; - } +open (MANIFEST, "../MANIFEST") or die $!; + +while (<MANIFEST>) { + my $filename; + next unless s|^lib/|| or m|^ext/|; + ($filename) = /(\S+)/; + $filename =~ s|^[^/]+/|| if $filename =~ s|^ext/||; + next unless $filename =~ /\.p(m|od)$/; + next unless open (MOD, "../lib/$filename"); + + my ($name, $thing); + my $foundit=0; + { + local $/=""; + while (<MOD>) { + next unless /^=head1 NAME/; + $foundit++; + last; + } + } + unless ($foundit) { + warn "$filename missing head1\n"; + next; + } + my $title = <MOD>; + chomp($title); + close MOD; + + my $perlname = $filename; + $perlname =~ s!\.p(m|od)$!!; + $perlname =~ s!/!::!g; + + ($name, $thing) = split / --? /, $title, 2; + + unless ($name and $thing) { + warn "$filename missing name\n" unless $name; + warn "$filename missing thing\n" unless $thing; + next; + } + + $thing =~ s/^perl pragma to //i; + $thing = ucfirst($thing); + $title = "=item $perlname\n\n$thing\n\n"; + + # print "$perlname $thing\n"; + + if ($filename=~/[A-Z]/) { + push @mod, $title; + } else { + push @pragma, $title; + } } -# Much easier to special case it like this than special case the depending on -# and parsing lib/Config.pod, or special case opening configpm and finding its -# =head1 (which is not found with the $/="" above) -push @mod, "=item Config\n\nAccess Perl configuration information\n\n"; - +print OUT <<'EOF'; +# Generated by perlmodlib.PL DO NOT EDIT! -# The intent of using =cut as the heredoc terminator is to make the whole file -# parse as (reasonably) sane Pod as-is to anything that attempts to -# brute-force treat it as such. The content is already useful - this just -# makes it tidier, by stopping anything doing this mistaking the rest of the -# Perl code for Pod. eg http://search.cpan.org/dist/perl/pod/perlmodlib.PL - -print $out <<'=cut'; =head1 NAME perlmodlib - constructing new Perl modules and finding existing ones +=head1 DESCRIPTION + =head1 THE PERL MODULE LIBRARY -Many modules are included in the Perl distribution. These are described +Many modules are included the Perl distribution. These are described below, and all end in F<.pm>. You may discover compiled library -files (usually ending in F<.so>) or small pieces of modules to be +file (usually ending in F<.so>) or small pieces of modules to be autoloaded (ending in F<.al>); these were automatically generated by the installation process. You may also discover files in the library directory that end in either F<.pl> or F<.ph>. These are @@ -152,12 +106,11 @@ The following pragmas are defined (and have their own documentation). =over 12 -=cut +EOF -print $out $_ for sort @pragma; - -print $out <<'=cut'; +print OUT $_ for (sort @pragma); +print OUT <<EOF; =back =head2 Standard Modules @@ -166,30 +119,21 @@ Standard, bundled modules are all expected to behave in a well-defined manner with respect to namespace pollution because they use the Exporter module. See their own documentation for details. -It's possible that not all modules listed below are installed on your -system. For example, the GDBM_File module will not be installed if you -don't have the gdbm library. - =over 12 -=cut +EOF -print $out $_ for sort @mod; - -print $out <<'=cut', "=cut\n"; +print OUT $_ for (sort @mod); +print OUT <<'EOF'; =back To find out I<all> modules installed on your system, including those without documentation or outside the standard release, -just use the following command (under the default win32 shell, -double quotes should be used instead of single quotes). +just do this: - % perl -MFile::Find=find -MFile::Spec::Functions -Tlwe \ - 'find { wanted => sub { print canonpath $_ if /\.pm\z/ }, - no_chdir => 1 }, @INC' + % find `perl -e 'print "@INC"'` -name '*.pm' -print -(The -T is here to prevent '.' from being listed in @INC.) They should all have their own documentation installed and accessible via your system man(1) command. If you do not have a B<find> program, you can use the Perl B<find2perl> program instead, which @@ -198,16 +142,11 @@ have a B<man> program but it doesn't find your modules, you'll have to fix your manpath. See L<perl> for details. If you have no system B<man> command, you might try the B<perldoc> program. -Note also that the command C<perldoc perllocal> gives you a (possibly -incomplete) list of the modules that have been further installed on -your system. (The perllocal.pod file is updated by the standard MakeMaker -install process.) - =head2 Extension Modules Extension modules are written in C (or a mix of Perl and C). They are usually dynamically loaded into Perl if and when you need them, -but may also be linked in statically. Supported extension modules +but may also be be linked in statically. Supported extension modules include Socket, Fcntl, and POSIX. Many popular C extension modules do not come bundled (at least, not @@ -215,7 +154,7 @@ completely) due to their sizes, volatility, or simply lack of time for adequate testing and configuration across the multitude of platforms on which Perl was beta-tested. You are encouraged to look for them on CPAN (described below), or using web search engines -like Alta Vista or Google. +like Alta Vista or Deja News. =head1 CPAN @@ -223,7 +162,8 @@ CPAN stands for Comprehensive Perl Archive Network; it's a globally replicated trove of Perl materials, including documentation, style guides, tricks and traps, alternate ports to non-Unix systems and occasional binary distributions for these. Search engines for -CPAN can be found at http://www.cpan.org/ +CPAN can be found at http://cpan.perl.com/ and at +http://theory.uwinnipeg.ca/mod_perl/cpan-search.pl . Most importantly, CPAN includes around a thousand unbundled modules, some of which require a C compiler to build. Major categories of @@ -317,38 +257,21 @@ Miscellaneous Modules =back -The list of the registered CPAN sites follows. -Please note that the sorting order is alphabetical on fields: - -Continent - | - |-->Country - | - |-->[state/province] - | - |-->ftp - | - |-->[http] - -and thus the North American servers happen to be listed between the -European and the South American sites. - -Registered CPAN sites - -=for maintainers -Generated by Porting/make_modlib_cpan.pl +Registered CPAN sites as of this writing include the following. +You should try to choose one close to you: =head2 Africa =over 4 -=item South Africa +=item * - http://cpan.mirror.ac.za/ - ftp://cpan.mirror.ac.za/ - http://mirror.is.co.za/pub/cpan/ - ftp://ftp.is.co.za/pub/cpan/ - ftp://ftp.saix.net/pub/CPAN/ +South Africa + + ftp://ftp.is.co.za/programming/perl/CPAN/ + ftp://ftp.saix.net/pub/CPAN/ + ftp://ftpza.co.za/pub/mirrors/cpan/ + ftp://ftp.sun.ac.za/CPAN/ =back @@ -356,92 +279,92 @@ Generated by Porting/make_modlib_cpan.pl =over 4 -=item China +=item * + +China - http://cpan.wenzk.com/ + ftp://freesoft.cei.gov.cn/pub/languages/perl/CPAN/ + http://www2.linuxforum.net/mirror/CPAN/ + http://cpan.shellhung.org/ + ftp://ftp.shellhung.org/pub/CPAN -=item Hong Kong +=item * - http://ftp.cuhk.edu.hk/pub/packages/perl/CPAN/ - ftp://ftp.cuhk.edu.hk/pub/packages/perl/CPAN/ - http://mirrors.geoexpat.com/cpan/ +Hong Kong -=item India + http://CPAN.pacific.net.hk/ + ftp://ftp.pacific.net.hk/pub/mirror/CPAN/ - http://perlmirror.indialinks.com/ +=item * -=item Indonesia +Indonesia - http://cpan.biz.net.id/ - http://komo.vlsm.org/CPAN/ - ftp://komo.vlsm.org/CPAN/ - http://cpan.cermin.lipi.go.id/ - ftp://cermin.lipi.go.id/pub/CPAN/ - http://cpan.pesat.net.id/ + http://piksi.itb.ac.id/CPAN/ + ftp://mirrors.piksi.itb.ac.id/CPAN/ + http://CPAN.mweb.co.id/ + ftp://ftp.mweb.co.id/pub/languages/perl/CPAN/ -=item Japan +=item * - ftp://ftp.u-aizu.ac.jp/pub/CPAN - ftp://ftp.kddilabs.jp/CPAN/ - http://ftp.nara.wide.ad.jp/pub/CPAN/ - ftp://ftp.nara.wide.ad.jp/pub/CPAN/ - http://ftp.jaist.ac.jp/pub/CPAN/ - ftp://ftp.jaist.ac.jp/pub/CPAN/ - ftp://ftp.dti.ad.jp/pub/lang/CPAN/ - ftp://ftp.ring.gr.jp/pub/lang/perl/CPAN/ - http://ftp.riken.jp/lang/CPAN/ - ftp://ftp.riken.jp/lang/CPAN/ - http://ftp.yz.yamagata-u.ac.jp/pub/lang/cpan/ - ftp://ftp.yz.yamagata-u.ac.jp/pub/lang/cpan/ +Israel -=item Republic of Korea + http://www.iglu.org.il:/pub/CPAN/ + ftp://ftp.iglu.org.il/pub/CPAN/ + http://bioinfo.weizmann.ac.il/pub/software/perl/CPAN/ + ftp://bioinfo.weizmann.ac.il/pub/software/perl/CPAN/ - http://ftp.kaist.ac.kr/pub/CPAN - ftp://ftp.kaist.ac.kr/pub/CPAN - http://cpan.mirror.cdnetworks.com/ - ftp://cpan.mirror.cdnetworks.com/CPAN/ - http://cpan.sarang.net/ - ftp://cpan.sarang.net/CPAN/ +=item * -=item Russia +Japan - http://cpan.tomsk.ru/ - ftp://cpan.tomsk.ru/ + ftp://ftp.u-aizu.ac.jp/pub/lang/perl/CPAN/ + ftp://ftp.kddlabs.co.jp/CPAN/ + http://mirror.nucba.ac.jp/mirror/Perl/ + ftp://mirror.nucba.ac.jp/mirror/Perl/ + ftp://ftp.meisei-u.ac.jp/pub/CPAN/ + ftp://ftp.jaist.ac.jp/pub/lang/perl/CPAN/ + ftp://ftp.dti.ad.jp/pub/lang/CPAN/ + ftp://ftp.ring.gr.jp/pub/lang/perl/CPAN/ -=item Singapore +=item * - http://mirror.averse.net/pub/CPAN - ftp://mirror.averse.net/pub/CPAN - http://cpan.mirror.choon.net/ - http://cpan.oss.eznetsols.org - ftp://ftp.oss.eznetsols.org/cpan +Saudi Arabia -=item Taiwan + ftp://ftp.isu.net.sa/pub/CPAN/ - http://ftp.cse.yzu.edu.tw/pub/CPAN/ - ftp://ftp.cse.yzu.edu.tw/pub/CPAN/ - http://cpan.nctu.edu.tw/ - ftp://cpan.nctu.edu.tw/ - ftp://ftp.ncu.edu.tw/CPAN/ - http://cpan.cdpa.nsysu.edu.tw/ - ftp://cpan.cdpa.nsysu.edu.tw/Unix/Lang/CPAN/ - http://cpan.stu.edu.tw - ftp://ftp.stu.edu.tw/CPAN - http://ftp.stu.edu.tw/CPAN - ftp://ftp.stu.edu.tw/pub/CPAN - http://cpan.cs.pu.edu.tw/ - ftp://cpan.cs.pu.edu.tw/pub/CPAN +=item * -=item Thailand +Singapore - http://mirrors.issp.co.th/cpan/ - ftp://mirrors.issp.co.th/cpan/ - http://mirror.yourconnect.com/CPAN/ - ftp://mirror.yourconnect.com/CPAN/ + http://cpan.hjc.edu.sg + http://ftp.nus.edu.sg/unix/perl/CPAN/ + ftp://ftp.nus.edu.sg/pub/unix/perl/CPAN/ -=item Turkey +=item * - http://cpan.gazi.edu.tr/ +South Korea + + http://CPAN.bora.net/ + ftp://ftp.bora.net/pub/CPAN/ + http://ftp.kornet.net/CPAN/ + ftp://ftp.kornet.net/pub/CPAN/ + ftp://ftp.nuri.net/pub/CPAN/ + +=item * + +Taiwan + + ftp://coda.nctu.edu.tw/UNIX/perl/CPAN + ftp://ftp.ee.ncku.edu.tw/pub/perl/CPAN/ + ftp://ftp1.sinica.edu.tw/pub1/perl/CPAN/ + +=item * + +Thailand + + http://download.nectec.or.th/CPAN/ + ftp://ftp.nectec.or.th/pub/languages/CPAN/ + ftp://ftp.cs.riubon.ac.th/pub/mirrors/CPAN/ =back @@ -449,10 +372,13 @@ Generated by Porting/make_modlib_cpan.pl =over 4 -=item Costa Rica +=item * + +Costa Rica - http://mirrors.ucr.ac.cr/CPAN/ - ftp://mirrors.ucr.ac.cr/CPAN/ + ftp://ftp.linux.co.cr/mirrors/CPAN/ + http://ftp.ucr.ac.cr/Unix/CPAN/ + ftp://ftp.ucr.ac.cr/pub/Unix/CPAN/ =back @@ -460,276 +386,250 @@ Generated by Porting/make_modlib_cpan.pl =over 4 -=item Austria - - http://cpan.inode.at/ - ftp://cpan.inode.at - http://gd.tuwien.ac.at/languages/perl/CPAN/ - ftp://gd.tuwien.ac.at/pub/CPAN/ - -=item Belgium - - http://ftp.belnet.be/mirror/ftp.cpan.org/ - ftp://ftp.belnet.be/mirror/ftp.cpan.org/ - http://ftp.easynet.be/pub/CPAN/ - http://cpan.weepee.org/ - -=item Bosnia and Herzegovina - - http://cpan.blic.net/ - -=item Bulgaria - - http://cpan.cbox.biz/ - ftp://cpan.cbox.biz/cpan/ - http://cpan.digsys.bg/ - ftp://ftp.digsys.bg/pub/CPAN - -=item Croatia - - http://ftp.carnet.hr/pub/CPAN/ - ftp://ftp.carnet.hr/pub/CPAN/ - -=item Czech Republic - - ftp://ftp.fi.muni.cz/pub/CPAN/ - http://archive.cpan.cz/ - -=item Denmark +=item * - http://mirrors.dotsrc.org/cpan - ftp://mirrors.dotsrc.org/cpan/ - http://www.cpan.dk/ - http://mirror.uni-c.dk/pub/CPAN/ +Austria -=item Finland + ftp://ftp.tuwien.ac.at/pub/languages/perl/CPAN/ - ftp://ftp.funet.fi/pub/languages/perl/CPAN/ - http://mirror.eunet.fi/CPAN +=item * -=item France +Belgium - http://cpan.enstimac.fr/ - ftp://ftp.inria.fr/pub/CPAN/ - http://distrib-coffee.ipsl.jussieu.fr/pub/mirrors/cpan/ - ftp://distrib-coffee.ipsl.jussieu.fr/pub/mirrors/cpan/ - ftp://ftp.lip6.fr/pub/perl/CPAN/ - http://mir2.ovh.net/ftp.cpan.org - ftp://mir1.ovh.net/ftp.cpan.org - ftp://ftp.oleane.net/pub/CPAN/ - http://ftp.crihan.fr/mirrors/ftp.cpan.org/ - ftp://ftp.crihan.fr/mirrors/ftp.cpan.org/ - http://ftp.u-strasbg.fr/CPAN - ftp://ftp.u-strasbg.fr/CPAN - http://cpan.cict.fr/ - ftp://cpan.cict.fr/pub/CPAN/ - -=item Germany + http://ftp.easynet.be/CPAN/ + ftp://ftp.easynet.be/CPAN/ + ftp://ftp.kulnet.kuleuven.ac.be/pub/mirror/CPAN/ - ftp://ftp.fu-berlin.de/unix/languages/perl/ - http://mirrors.softliste.de/cpan/ - ftp://ftp.rub.de/pub/CPAN/ - http://www.planet-elektronik.de/CPAN/ - http://ftp.hosteurope.de/pub/CPAN/ - ftp://ftp.hosteurope.de/pub/CPAN/ - http://www.mirrorspace.org/cpan/ - http://mirror.netcologne.de/cpan/ - ftp://mirror.netcologne.de/cpan/ - ftp://ftp.freenet.de/pub/ftp.cpan.org/pub/CPAN/ - http://ftp-stud.hs-esslingen.de/pub/Mirrors/CPAN/ - ftp://ftp-stud.hs-esslingen.de/pub/Mirrors/CPAN/ - http://mirrors.zerg.biz/cpan/ - http://ftp.gwdg.de/pub/languages/perl/CPAN/ - ftp://ftp.gwdg.de/pub/languages/perl/CPAN/ - http://dl.ambiweb.de/mirrors/ftp.cpan.org/ - http://cpan.mirror.clusters.kg/ - http://cpan.mirror.iphh.net/ - ftp://cpan.mirror.iphh.net/pub/CPAN/ - http://cpan.mirroring.de/ - http://mirror.informatik.uni-mannheim.de/pub/mirrors/CPAN/ - ftp://mirror.informatik.uni-mannheim.de/pub/mirrors/CPAN/ - http://www.chemmedia.de/mirrors/CPAN/ - http://ftp.cw.net/pub/CPAN/ - ftp://ftp.cw.net/pub/CPAN/ - http://cpan.cpantesters.org/ - ftp://cpan.cpantesters.org/CPAN/ - http://cpan.mirrored.de/ - ftp://mirror.petamem.com/CPAN/ - http://cpan.noris.de/ - ftp://cpan.noris.de/pub/CPAN/ - ftp://ftp.mpi-sb.mpg.de/pub/perl/CPAN/ - ftp://ftp.gmd.de/mirrors/CPAN/ - -=item Greece - - ftp://ftp.forthnet.gr/pub/languages/perl/CPAN - ftp://ftp.ntua.gr/pub/lang/perl/ - http://cpan.cc.uoc.gr/ - ftp://ftp.cc.uoc.gr/mirrors/CPAN/ - -=item Hungary - - http://cpan.mirrors.enexis.hu/ - ftp://cpan.mirrors.enexis.hu/mirrors/cpan/ - http://cpan.hu/ - -=item Iceland - - http://ftp.rhnet.is/pub/CPAN/ - ftp://ftp.rhnet.is/pub/CPAN/ - -=item Ireland - - http://ftp.esat.net/pub/languages/perl/CPAN/ - ftp://ftp.esat.net/pub/languages/perl/CPAN/ - http://ftp.heanet.ie/mirrors/ftp.perl.org/pub/CPAN - ftp://ftp.heanet.ie/mirrors/ftp.perl.org/pub/CPAN - -=item Italy - - http://bo.mirror.garr.it/mirrors/CPAN/ - http://cpan.panu.it/ - ftp://ftp.panu.it/pub/mirrors/perl/CPAN/ - -=item Latvia - - http://kvin.lv/pub/CPAN/ - -=item Lithuania - - http://ftp.litnet.lt/pub/CPAN/ - ftp://ftp.litnet.lt/pub/CPAN/ - -=item Malta - - http://cpan.waldonet.net.mt/ - -=item Netherlands - - ftp://ftp.quicknet.nl/pub/CPAN/ - http://mirror.hostfuss.com/CPAN/ - ftp://mirror.hostfuss.com/CPAN/ - http://mirrors3.kernel.org/cpan/ - ftp://mirrors3.kernel.org/pub/CPAN/ - http://cpan.mirror.versatel.nl/ - ftp://ftp.mirror.versatel.nl/cpan/ - ftp://download.xs4all.nl/pub/mirror/CPAN/ - http://mirror.leaseweb.com/CPAN/ - ftp://mirror.leaseweb.com/CPAN/ - ftp://ftp.cpan.nl/pub/CPAN/ - http://archive.cs.uu.nl/mirror/CPAN/ - ftp://ftp.cs.uu.nl/mirror/CPAN/ - http://luxitude.net/cpan/ - -=item Norway - - ftp://ftp.uninett.no/pub/languages/perl/CPAN - ftp://ftp.uit.no/pub/languages/perl/cpan/ - -=item Poland - - http://piotrkosoft.net/pub/mirrors/CPAN/ - ftp://ftp.piotrkosoft.net/pub/mirrors/CPAN/ - http://ftp.man.poznan.pl/pub/CPAN - ftp://ftp.man.poznan.pl/pub/CPAN - ftp://ftp.ps.pl/pub/CPAN/ - ftp://sunsite.icm.edu.pl/pub/CPAN/ - ftp://ftp.tpnet.pl/d4/CPAN/ - -=item Portugal - - http://cpan.dei.uc.pt/ - ftp://ftp.dei.uc.pt/pub/CPAN - ftp://ftp.ist.utl.pt/pub/CPAN/ - http://cpan.perl.pt/ - http://cpan.ip.pt/ - ftp://cpan.ip.pt/pub/cpan/ - http://mirrors.nfsi.pt/CPAN/ - ftp://mirrors.nfsi.pt/pub/CPAN/ - http://cpan.dcc.fc.up.pt/ - -=item Romania - - http://ftp.astral.ro/pub/CPAN/ - ftp://ftp.astral.ro/pub/CPAN/ - ftp://ftp.lug.ro/CPAN - http://mirrors.xservers.ro/CPAN/ - http://mirrors.hostingromania.ro/ftp.cpan.org/ - ftp://ftp.hostingromania.ro/mirrors/ftp.cpan.org/ - ftp://ftp.iasi.roedu.net/pub/mirrors/ftp.cpan.org/ +=item * -=item Russia +Bulgaria - ftp://ftp.aha.ru/CPAN/ - http://cpan.rinet.ru/ - ftp://cpan.rinet.ru/pub/mirror/CPAN/ - ftp://ftp.SpringDaemons.com/pub/CPAN/ - http://mirror.rol.ru/CPAN/ - http://ftp.silvernet.ru/CPAN/ - http://ftp.spbu.ru/CPAN/ - ftp://ftp.spbu.ru/CPAN/ + ftp://ftp.ntrl.net/pub/mirrors/CPAN/ -=item Slovakia - - http://cpan.fyxm.net/ +=item * + +Croatia + + ftp://ftp.linux.hr/pub/CPAN/ + +=item * + +Czech Republic + + http://www.fi.muni.cz/pub/perl/ + ftp://ftp.fi.muni.cz/pub/perl/ + ftp://sunsite.mff.cuni.cz/MIRRORS/ftp.funet.fi/pub/languages/perl/CPAN/ + +=item * + +Denmark + + ftp://sunsite.auc.dk/pub/languages/perl/CPAN/ + http://www.cpan.dk/CPAN/ + ftp://www.cpan.dk/ftp.cpan.org/CPAN/ + +=item * -=item Slovenia - - http://www.klevze.si/cpan - -=item Spain - - http://osl.ugr.es/CPAN/ - ftp://ftp.rediris.es/mirror/CPAN/ - http://ftp.gui.uva.es/sites/cpan.org/ - ftp://ftp.gui.uva.es/sites/cpan.org/ - -=item Sweden - - http://mirrors4.kernel.org/cpan/ - ftp://mirrors4.kernel.org/pub/CPAN/ - -=item Switzerland - - http://cpan.mirror.solnet.ch/ - ftp://ftp.solnet.ch/mirror/CPAN/ - ftp://ftp.adwired.ch/CPAN/ - http://mirror.switch.ch/ftp/mirror/CPAN/ - ftp://mirror.switch.ch/mirror/CPAN/ - -=item Ukraine - - http://cpan.makeperl.org/ - ftp://cpan.makeperl.org/pub/CPAN - http://cpan.org.ua/ - http://cpan.gafol.net/ - ftp://ftp.gafol.net/pub/cpan/ - -=item United Kingdom - - http://www.mirrorservice.org/sites/ftp.funet.fi/pub/languages/perl/CPAN/ - ftp://ftp.mirrorservice.org/sites/ftp.funet.fi/pub/languages/perl/CPAN/ - http://mirror.tje.me.uk/pub/mirrors/ftp.cpan.org/ - ftp://mirror.tje.me.uk/pub/mirrors/ftp.cpan.org/ - http://www.mirror.8086.net/sites/CPAN/ - ftp://ftp.mirror.8086.net/sites/CPAN/ - http://cpan.mirror.anlx.net/ - ftp://ftp.mirror.anlx.net/CPAN/ - http://mirror.bytemark.co.uk/CPAN/ - ftp://mirror.bytemark.co.uk/CPAN/ - http://cpan.etla.org/ - ftp://cpan.etla.org/pub/CPAN - ftp://ftp.demon.co.uk/pub/CPAN/ - http://mirror.sov.uk.goscomb.net/CPAN/ - ftp://mirror.sov.uk.goscomb.net/pub/CPAN/ - http://ftp.plig.net/pub/CPAN/ - ftp://ftp.plig.net/pub/CPAN/ - http://ftp.ticklers.org/pub/CPAN/ - ftp://ftp.ticklers.org/pub/CPAN/ - http://cpan.mirrors.uk2.net/ - ftp://mirrors.uk2.net/pub/CPAN/ - http://mirror.ox.ac.uk/sites/www.cpan.org/ - ftp://mirror.ox.ac.uk/sites/www.cpan.org/ +England + + http://www.mirror.ac.uk/sites/ftp.funet.fi/pub/languages/perl/CPAN + ftp://ftp.mirror.ac.uk/sites/ftp.funet.fi/pub/languages/perl/CPAN/ + ftp://ftp.demon.co.uk/pub/mirrors/perl/CPAN/ + ftp://ftp.flirble.org/pub/languages/perl/CPAN/ + ftp://ftp.plig.org/pub/CPAN/ + ftp://sunsite.doc.ic.ac.uk/packages/CPAN/ + http://mirror.uklinux.net/CPAN/ + ftp://mirror.uklinux.net/pub/CPAN/ + ftp://usit.shef.ac.uk/pub/packages/CPAN/ + +=item * + +Estonia + + ftp://ftp.ut.ee/pub/languages/perl/CPAN/ + +=item * + +Finland + + ftp://ftp.funet.fi/pub/languages/perl/CPAN/ + +=item * + +France + + ftp://cpan.ftp.worldonline.fr/pub/CPAN/ + ftp://ftp.club-internet.fr/pub/perl/CPAN/ + ftp://ftp.lip6.fr/pub/perl/CPAN/ + ftp://ftp.oleane.net/pub/mirrors/CPAN/ + ftp://ftp.pasteur.fr/pub/computing/CPAN/ + ftp://cpan.cict.fr/pub/CPAN/ + ftp://ftp.uvsq.fr/pub/perl/CPAN/ + +=item * + +Germany + + ftp://ftp.rz.ruhr-uni-bochum.de/pub/CPAN/ + ftp://ftp.freenet.de/pub/ftp.cpan.org/pub/CPAN/ + ftp://ftp.uni-erlangen.de/pub/source/CPAN/ + ftp://ftp-stud.fht-esslingen.de/pub/Mirrors/CPAN + ftp://ftp.gigabell.net/pub/CPAN/ + http://ftp.gwdg.de/pub/languages/perl/CPAN/ + ftp://ftp.gwdg.de/pub/languages/perl/CPAN/ + ftp://ftp.uni-hamburg.de/pub/soft/lang/perl/CPAN/ + ftp://ftp.leo.org/pub/comp/general/programming/languages/script/perl/CPAN/ + ftp://ftp.mpi-sb.mpg.de/pub/perl/CPAN/ + ftp://ftp.gmd.de/mirrors/CPAN/ + +=item * + +Greece + + ftp://ftp.forthnet.gr/pub/languages/perl/CPAN + ftp://ftp.ntua.gr/pub/lang/perl/ + +=item * + +Hungary + + http://cpan.artifact.hu/ + ftp://cpan.artifact.hu/CPAN/ + ftp://ftp.kfki.hu/pub/packages/perl/CPAN/ + +=item * + +Iceland + + http://cpan.gm.is/ + ftp://ftp.gm.is/pub/CPAN/ + +=item * + +Ireland + + http://cpan.indigo.ie/ + ftp://cpan.indigo.ie/pub/CPAN/ + http://sunsite.compapp.dcu.ie/pub/perl/ + ftp://sunsite.compapp.dcu.ie/pub/perl/ + +=item * + +Italy + + http://cpan.nettuno.it/ + http://gusp.dyndns.org/CPAN/ + ftp://gusp.dyndns.org/pub/CPAN + http://softcity.iol.it/cpan + ftp://softcity.iol.it/pub/cpan + ftp://ftp.unina.it/pub/Other/CPAN/ + ftp://ftp.unipi.it/pub/mirror/perl/CPAN/ + ftp://cis.uniRoma2.it/CPAN/ + ftp://ftp.edisontel.it/pub/CPAN_Mirror/ + ftp://ftp.flashnet.it/pub/CPAN/ + +=item * + +Latvia + + http://kvin.lv/pub/CPAN/ + +=item * + +Netherlands + + ftp://download.xs4all.nl/pub/mirror/CPAN/ + ftp://ftp.nl.uu.net/pub/CPAN/ + ftp://ftp.nluug.nl/pub/languages/perl/CPAN/ + ftp://ftp.cpan.nl/pub/CPAN/ + http://www.cs.uu.nl/mirror/CPAN/ + ftp://ftp.cs.uu.nl/mirror/CPAN/ + +=item * + +Norway + + ftp://sunsite.uio.no/pub/languages/perl/CPAN/ + ftp://ftp.uit.no/pub/languages/perl/cpan/ + +=item * + +Poland + + ftp://ftp.pk.edu.pl/pub/lang/perl/CPAN/ + ftp://ftp.mega.net.pl/pub/mirrors/ftp.perl.com/ + ftp://ftp.man.torun.pl/pub/doc/CPAN/ + ftp://sunsite.icm.edu.pl/pub/CPAN/ + +=item * + +Portugal + + ftp://ftp.ua.pt/pub/CPAN/ + ftp://perl.di.uminho.pt/pub/CPAN/ + ftp://ftp.ist.utl.pt/pub/CPAN/ + ftp://ftp.netc.pt/pub/CPAN/ + +=item * + +Romania + + ftp://archive.logicnet.ro/mirrors/ftp.cpan.org/CPAN/ + ftp://ftp.kappa.ro/pub/mirrors/ftp.perl.org/pub/CPAN/ + ftp://ftp.dntis.ro/pub/cpan/ + ftp://ftp.opsynet.com/cpan/ + ftp://ftp.dnttm.ro/pub/CPAN/ + ftp://ftp.timisoara.roedu.net/mirrors/CPAN/ + +=item * + +Russia + + ftp://ftp.chg.ru/pub/lang/perl/CPAN/ + http://cpan.rinet.ru/ + ftp://cpan.rinet.ru/pub/mirror/CPAN/ + ftp://ftp.aha.ru/pub/CPAN/ + ftp://ftp.sai.msu.su/pub/lang/perl/CPAN/ + +=item * + +Slovakia + + ftp://ftp.entry.sk/pub/languages/perl/CPAN/ + +=item * + +Slovenia + + ftp://ftp.arnes.si/software/perl/CPAN/ + +=item * + +Spain + + ftp://ftp.rediris.es/mirror/CPAN/ + ftp://ftp.etse.urv.es/pub/perl/ + +=item * + +Sweden + + http://ftp.du.se/CPAN/ + ftp://ftp.du.se/pub/CPAN/ + ftp://ftp.sunet.se/pub/lang/perl/CPAN/ + +=item * + +Switzerland + + ftp://ftp.danyk.ch/CPAN/ + ftp://sunsite.cnlab-switch.ch/mirror/CPAN/ + +=item * + +Turkey + + ftp://sunsite.bilkent.edu.tr/pub/languages/CPAN/ =back @@ -737,176 +637,213 @@ Generated by Porting/make_modlib_cpan.pl =over 4 -=item Bahamas +=item * + +Canada + +=over 8 + +=item * + +Alberta + + http://sunsite.ualberta.ca/pub/Mirror/CPAN/ + ftp://sunsite.ualberta.ca/pub/Mirror/CPAN/ + +=item * + +Manitoba + + http://theoryx5.uwinnipeg.ca/pub/CPAN/ + ftp://theoryx5.uwinnipeg.ca/pub/CPAN/ + +=item * + +Nova Scotia + + ftp://cpan.chebucto.ns.ca/pub/CPAN/ + +=item * + +Ontario - http://www.securehost.com/mirror/CPAN/ + ftp://ftp.crc.ca/pub/packages/lang/perl/CPAN/ -=item Canada +=item * + +Mexico - http://cpan.arcticnetwork.ca - ftp://mirror.arcticnetwork.ca/pub/CPAN - http://cpan.sunsite.ualberta.ca/ - ftp://cpan.sunsite.ualberta.ca/pub/CPAN/ - http://theoryx5.uwinnipeg.ca/pub/CPAN/ - ftp://theoryx5.uwinnipeg.ca/pub/CPAN/ - http://arwen.cs.dal.ca/mirror/CPAN/ - ftp://arwen.cs.dal.ca/pub/mirror/CPAN/ - http://CPAN.mirror.rafal.ca/ - ftp://CPAN.mirror.rafal.ca/pub/CPAN/ - ftp://ftp.nrc.ca/pub/CPAN/ - http://mirror.csclub.uwaterloo.ca/pub/CPAN/ - ftp://mirror.csclub.uwaterloo.ca/pub/CPAN/ + http://www.msg.com.mx/CPAN/ + ftp://ftp.msg.com.mx/pub/CPAN/ -=item Mexico +=back - http://www.msg.com.mx/CPAN/ - ftp://ftp.msg.com.mx/pub/CPAN/ +=item * -=item United States +United States =over 8 -=item Alabama +=item * + +Alabama + + http://mirror.hiwaay.net/CPAN/ + ftp://mirror.hiwaay.net/CPAN/ + +=item * + +California + + http://www.cpan.org/ + ftp://ftp.cpan.org/CPAN/ + ftp://cpan.nas.nasa.gov/pub/perl/CPAN/ + ftp://ftp.digital.com/pub/plan/perl/CPAN/ + http://www.kernel.org/pub/mirrors/cpan/ + ftp://ftp.kernel.org/pub/mirrors/cpan/ + http://www.perl.com/CPAN/ + http://download.sourceforge.net/mirrors/CPAN/ + +=item * + +Colorado + + ftp://ftp.cs.colorado.edu/pub/perl/CPAN/ + +=item * + +Florida + + ftp://ftp.cise.ufl.edu/pub/perl/CPAN/ + +=item * + +Georgia + + ftp://ftp.twoguys.org/CPAN/ + +=item * + +Illinois + + http://www.neurogames.com/mirrors/CPAN + http://uiarchive.uiuc.edu/mirrors/ftp/ftp.cpan.org/pub/CPAN/ + ftp://uiarchive.uiuc.edu/mirrors/ftp/ftp.cpan.org/pub/CPAN/ - http://mirror.hiwaay.net/CPAN/ - ftp://mirror.hiwaay.net/CPAN/ +=item * -=item Arizona +Indiana - http://cpan.ezarticleinformation.com/ + ftp://ftp.uwsg.indiana.edu/pub/perl/CPAN/ + http://cpan.nitco.com/ + ftp://cpan.nitco.com/pub/CPAN/ + ftp://cpan.in-span.net/ + http://csociety-ftp.ecn.purdue.edu/pub/CPAN + ftp://csociety-ftp.ecn.purdue.edu/pub/CPAN -=item California +=item * - http://cpan.knowledgematters.net/ - http://cpan.binkerton.com/ - http://cpan.develooper.com/ - http://mirrors.gossamer-threads.com/CPAN - http://cpan.schatt.com/ - http://mirrors.kernel.org/cpan/ - ftp://mirrors.kernel.org/pub/CPAN - http://mirrors2.kernel.org/cpan/ - ftp://mirrors2.kernel.org/pub/CPAN/ - http://cpan.mirror.facebook.net/ - http://mirrors1.kernel.org/cpan/ - ftp://mirrors1.kernel.org/pub/CPAN/ - http://cpan-sj.viaverio.com/ - ftp://cpan-sj.viaverio.com/pub/CPAN/ - http://www.perl.com/CPAN/ +Kentucky -=item Florida + http://cpan.uky.edu/ + ftp://cpan.uky.edu/pub/CPAN/ - ftp://ftp.cise.ufl.edu/pub/mirrors/CPAN/ - http://mirror.atlantic.net/pub/CPAN/ - ftp://mirror.atlantic.net/pub/CPAN/ +=item * -=item Idaho +Massachusetts - http://mirror.its.uidaho.edu/pub/cpan/ - ftp://mirror.its.uidaho.edu/cpan/ + ftp://ftp.ccs.neu.edu/net/mirrors/ftp.funet.fi/pub/languages/perl/CPAN/ + ftp://ftp.iguide.com/pub/mirrors/packages/perl/CPAN/ -=item Illinois +=item * - http://cpan.mirrors.hoobly.com/ - http://cpan.uchicago.edu/pub/CPAN/ - ftp://cpan.uchicago.edu/pub/CPAN/ - http://mirrors.servercentral.net/CPAN/ - http://www.stathy.com/CPAN/ - ftp://www.stathy.com/CPAN/ +New Jersey -=item Indiana + ftp://ftp.cpanel.net/pub/CPAN/ - ftp://ftp.uwsg.iu.edu/pub/perl/CPAN/ - http://cpan.netnitco.net/ - ftp://cpan.netnitco.net/pub/mirrors/CPAN/ - http://ftp.ndlug.nd.edu/pub/perl/ - ftp://ftp.ndlug.nd.edu/pub/perl/ +=item * -=item Massachusetts +New York - http://mirrors.ccs.neu.edu/CPAN/ + ftp://ftp.freesoftware.com/pub/perl/CPAN/ + http://www.deao.net/mirrors/CPAN/ + ftp://ftp.deao.net/pub/CPAN/ + ftp://ftp.stealth.net/pub/mirrors/ftp.cpan.org/pub/CPAN/ + http://mirror.nyc.anidea.com/CPAN/ + ftp://mirror.nyc.anidea.com/pub/CPAN/ + http://www.rge.com/pub/languages/perl/ + ftp://ftp.rge.com/pub/languages/perl/ + ftp://mirrors.cloud9.net/pub/mirrors/CPAN/ -=item Michigan +=item * - http://ftp.wayne.edu/cpan/ - ftp://ftp.wayne.edu/cpan/ +North Carolina -=item Minnesota + ftp://ftp.duke.edu/pub/perl/ - http://cpan.msi.umn.edu/ +=item * -=item New Jersey +Ohio - http://mirror.datapipe.net/CPAN/ - ftp://mirror.datapipe.net/pub/CPAN/ + ftp://ftp.loaded.net/pub/CPAN/ -=item New York +=item * - http://mirrors.24-7-solutions.net/pub/CPAN/ - ftp://mirrors.24-7-solutions.net/pub/CPAN/ - http://mirror.cc.columbia.edu/pub/software/cpan/ - ftp://mirror.cc.columbia.edu/pub/software/cpan/ - http://cpan.belfry.net/ - http://cpan.erlbaum.net/ - ftp://cpan.erlbaum.net/CPAN/ - http://cpan.hexten.net/ - ftp://cpan.hexten.net/ - ftp://mirror.nyi.net/CPAN/ - http://mirror.rit.edu/CPAN/ - ftp://mirror.rit.edu/CPAN/ +Oklahoma -=item North Carolina + ftp://ftp.ou.edu/mirrors/CPAN/ - http://www.ibiblio.org/pub/mirrors/CPAN - ftp://ftp.ncsu.edu/pub/mirror/CPAN/ +=item * -=item Oregon +Oregon - http://ftp.osuosl.org/pub/CPAN/ - ftp://ftp.osuosl.org/pub/CPAN/ + ftp://ftp.orst.edu/pub/packages/CPAN/ -=item Pennsylvania +=item * - http://ftp.epix.net/CPAN/ - ftp://ftp.epix.net/pub/languages/perl/ - http://cpan.pair.com/ - ftp://cpan.pair.com/pub/CPAN/ +Pennsylvania -=item South Carolina + http://ftp.epix.net/CPAN/ + ftp://ftp.epix.net/pub/languages/perl/ + ftp://carroll.cac.psu.edu/pub/CPAN/ - http://cpan.mirror.clemson.edu/ +=item * -=item Tennessee +Tennessee - http://mira.sunsite.utk.edu/CPAN/ + ftp://ftp.sunsite.utk.edu/pub/CPAN/ -=item Texas +=item * - http://mirror.uta.edu/CPAN +Texas -=item Utah + http://ftp.sedl.org/pub/mirrors/CPAN/ + http://jhcloos.com/pub/mirror/CPAN/ + ftp://jhcloos.com/pub/mirror/CPAN/ - ftp://mirror.xmission.com/CPAN/ +=item * -=item Virginia +Utah - http://cpan-du.viaverio.com/ - ftp://cpan-du.viaverio.com/pub/CPAN/ - http://perl.secsup.org/ - ftp://perl.secsup.org/pub/perl/ - ftp://mirror.cogentco.com/pub/CPAN/ + ftp://mirror.xmission.com/CPAN/ -=item Washington +=item * - http://cpan.llarian.net/ - ftp://cpan.llarian.net/pub/CPAN/ - ftp://ftp-mirror.internap.com/pub/CPAN/ +Virginia -=item Wisconsin + http://mirrors.rcn.net/pub/lang/CPAN/ + ftp://mirrors.rcn.net/pub/lang/CPAN/ + ftp://ruff.cs.jmu.edu/pub/CPAN/ + http://perl.Liquidation.com/CPAN/ - http://cpan.mirrors.tds.net - ftp://cpan.mirrors.tds.net/pub/CPAN - http://mirror.sit.wisc.edu/pub/CPAN/ - ftp://mirror.sit.wisc.edu/pub/CPAN/ +=item * + +Washington + + http://cpan.llarian.net/ + ftp://cpan.llarian.net/pub/CPAN/ + ftp://ftp-mirror.internap.com/pub/CPAN/ + ftp://ftp.spu.edu/pub/CPAN/ =back @@ -916,21 +853,20 @@ Generated by Porting/make_modlib_cpan.pl =over 4 -=item Australia +=item * - http://mirror.internode.on.net/pub/cpan/ - ftp://mirror.internode.on.net/pub/cpan/ - http://cpan.mirror.aussiehq.net.au/ - http://mirror.as24220.net/cpan/ - ftp://mirror.as24220.net/cpan/ +Australia + + http://ftp.planetmirror.com/pub/CPAN/ + ftp://ftp.planetmirror.com/pub/CPAN/ + ftp://mirror.aarnet.edu.au/pub/perl/CPAN/ + ftp://cpan.topend.com.au/pub/CPAN/ + +=item * -=item New Zealand +New Zealand - ftp://ftp.auckland.ac.nz/pub/perl/CPAN/ - http://cpan.inspire.net.nz - ftp://cpan.inspire.net.nz/cpan - http://cpan.catalyst.net.nz/CPAN/ - ftp://cpan.catalyst.net.nz/pub/CPAN/ + ftp://ftp.auckland.ac.nz/pub/perl/CPAN/ =back @@ -938,107 +874,29 @@ Generated by Porting/make_modlib_cpan.pl =over 4 -=item Argentina +=item * + +Argentina - http://cpan.patan.com.ar/ - http://cpan.localhost.net.ar - ftp://mirrors.localhost.net.ar/pub/mirrors/CPAN + ftp://mirrors.bannerlandia.com.ar/mirrors/CPAN/ -=item Brazil +=item * - ftp://cpan.pop-mg.com.br/pub/CPAN/ - http://ftp.pucpr.br/CPAN - ftp://ftp.pucpr.br/CPAN - http://cpan.kinghost.net/ +Brazil -=item Chile + ftp://cpan.pop-mg.com.br/pub/CPAN/ + ftp://ftp.matrix.com.br/pub/perl/ + ftp://cpan.if.usp.br/pub/mirror/CPAN/ - http://cpan.dcc.uchile.cl/ - ftp://cpan.dcc.uchile.cl/pub/lang/cpan/ +=item * -=item Colombia +Chile - http://www.laqee.unal.edu.co/CPAN/ + ftp://ftp.psinet.cl/pub/programming/perl/CPAN/ + ftp://sunsite.dcc.uchile.cl/pub/lang/perl/ =back -=head2 RSYNC Mirrors - - mirror.as24220.net::cpan - cpan.inode.at::CPAN - gd.tuwien.ac.at::CPAN - ftp.belnet.be::packages/cpan - rsync.linorg.usp.br::CPAN - rsync.arcticnetwork.ca::CPAN - CPAN.mirror.rafal.ca::CPAN - mirror.csclub.uwaterloo.ca::CPAN - theoryx5.uwinnipeg.ca::CPAN - www.laqee.unal.edu.co::CPAN - mirror.uni-c.dk::CPAN - rsync.nic.funet.fi::CPAN - rsync://distrib-coffee.ipsl.jussieu.fr/pub/mirrors/cpan/ - mir1.ovh.net::CPAN - miroir-francais.fr::cpan - ftp.crihan.fr::CPAN - rsync://mirror.cict.fr/cpan/ - rsync://mirror.netcologne.de/cpan/ - ftp-stud.hs-esslingen.de::CPAN/ - ftp.gwdg.de::FTP/languages/perl/CPAN/ - cpan.mirror.iphh.net::CPAN - cpan.cpantesters.org::cpan - cpan.hu::CPAN - komo.vlsm.org::CPAN - mirror.unej.ac.id::cpan - ftp.esat.net::/pub/languages/perl/CPAN - ftp.heanet.ie::mirrors/ftp.perl.org/pub/CPAN - rsync.panu.it::CPAN - cpan.fastbull.org::CPAN - ftp.kddilabs.jp::cpan - ftp.nara.wide.ad.jp::cpan/ - rsync://ftp.jaist.ac.jp/pub/CPAN/ - rsync://ftp.riken.jp/cpan/ - mirror.linuxiso.kz::CPAN - rsync://mirrors3.kernel.org/mirrors/CPAN/ - rsync://rsync.osmirror.nl/cpan/ - mirror.leaseweb.com::CPAN - cpan.nautile.nc::CPAN - mirror.icis.pcz.pl::CPAN - piotrkosoft.net::mirrors/CPAN - rsync://cpan.perl.pt/ - ftp.kaist.ac.kr::cpan - cpan.sarang.net::CPAN - mirror.averse.net::cpan - rsync.oss.eznetsols.org - mirror.ac.za::cpan - ftp.is.co.za::IS-Mirror/ftp.cpan.org/ - rsync://ftp.gui.uva.es/cpan/ - rsync://mirrors4.kernel.org/mirrors/CPAN/ - ftp.solnet.ch::CPAN - ftp.ulak.net.tr::CPAN - gafol.net::cpan - rsync.mirrorservice.org::ftp.funet.fi/pub/ - rsync://rsync.mirror.8086.net/CPAN/ - rsync.mirror.anlx.net::CPAN - mirror.bytemark.co.uk::CPAN - ftp.plig.net::CPAN - rsync://ftp.ticklers.org:CPAN/ - mirrors.ibiblio.org::CPAN - cpan-du.viaverio.com::CPAN - mirror.hiwaay.net::CPAN - rsync://mira.sunsite.utk.edu/CPAN/ - cpan.mirrors.tds.net::CPAN - mirror.its.uidaho.edu::cpan - rsync://mirror.cc.columbia.edu::cpan/ - ftp.fxcorporate.com::CPAN - rsync.atlantic.net::CPAN - mirrors.kernel.org::mirrors/CPAN - rsync://mirrors2.kernel.org/mirrors/CPAN/ - cpan.pair.com::CPAN - rsync://mirror.rit.edu/CPAN/ - rsync://mirror.facebook.net/cpan/ - rsync://mirrors1.kernel.org/mirrors/CPAN/ - cpan-sj.viaverio.com::CPAN - For an up-to-date listing of CPAN sites, see http://www.cpan.org/SITES or ftp://www.cpan.org/SITES . @@ -1061,7 +919,7 @@ its methods by loading dynamic C or C++ objects, but that should be totally transparent to the user of the module. Likewise, the module might set up an AUTOLOAD function to slurp in subroutine definitions on demand, but this is also transparent. Only the F<.pm> file is required to -exist. See L<perlsub>, L<perlobj>, and L<AutoLoader> for details about +exist. See L<perlsub>, L<perltoot>, and L<AutoLoader> for details about the AUTOLOAD mechanism. =head2 Guidelines for Module Creation @@ -1121,7 +979,7 @@ Let the objects look after themselves! Generally, avoid hard-wired class names as far as possible. Avoid C<< $r->Class::func() >> where using C<@ISA=qw(... Class ...)> and -C<< $r->func() >> would work. +C<< $r->func() >> would work (see L<perlbot> for more details). Use autosplit so little used or newly added functions won't be a burden to programs that don't use them. Add test functions to @@ -1132,8 +990,8 @@ the module after __END__ either using AutoSplit or by saying: Does your module pass the 'empty subclass' test? If you say C<@SUBCLASS::ISA = qw(YOURCLASS);> your applications should be able to use SUBCLASS in exactly the same way as YOURCLASS. For example, -does your application still work if you change: C<< $obj = YOURCLASS->new(); >> -into: C<< $obj = SUBCLASS->new(); >> ? +does your application still work if you change: C<$obj = new YOURCLASS;> +into: C<$obj = new SUBCLASS;> ? Avoid keeping any state information in your packages. It makes it difficult for multiple other packages to use yours. Keep state @@ -1147,7 +1005,7 @@ of code that need less strictness. Always use B<-w>. -Follow the guidelines in L<perlstyle>. +Follow the guidelines in the perlstyle(1) manual. Always use B<-w>. @@ -1315,18 +1173,16 @@ Copying, ToDo etc. =over 4 -=item * +=item Adding a Copyright Notice. -Adding a Copyright Notice. How you choose to license your work is a personal decision. The general mechanism is to assert your Copyright and then make a declaration of how others may copy/use/modify your work. -Perl, for example, is supplied with two types of licence: The GNU GPL -and The Artistic Licence (see the files README, Copying, and Artistic, -or L<perlgpl> and L<perlartistic>). Larry has good reasons for NOT -just using the GNU GPL. +Perl, for example, is supplied with two types of licence: The GNU +GPL and The Artistic Licence (see the files README, Copying, and +Artistic). Larry has good reasons for NOT just using the GNU GPL. My personal recommendation, out of respect for Larry, Perl, and the Perl community at large is to state something simply like: @@ -1345,7 +1201,7 @@ Give the module a version/issue/release number. To be fully compatible with the Exporter and MakeMaker modules you should store your module's version number in a non-my package -variable called $VERSION. This should be a positive floating point +variable called $VERSION. This should be a floating point number with at least two digits after the decimal (i.e., hundredths, e.g, C<$VERSION = "0.01">). Don't use a "1.3.2" style version. See L<Exporter> for details. @@ -1386,7 +1242,7 @@ Follow the instructions and links on: or upload to one of these sites: https://pause.kbx.de/pause/ - http://pause.perl.org/ + http://pause.perl.org/pause/ and notify <modules@perl.org>. @@ -1520,7 +1376,8 @@ that a module doesn't pollute any namespace it wasn't asked to. The written contract for the module (A.K.A. documentation) may make other provisions. But then you know when you C<use RedefineTheWorld> that you're redefining the world and willing to take the consequences. +EOF -=cut +close MANIFEST or warn "$0: failed to close MANIFEST (../MANIFEST): $!"; +close OUT or warn "$0: failed to close OUT (perlmodlib.tmp): $!"; -read_only_bottom_close_and_rename($out); diff --git a/gnu/usr.bin/perl/pod/perlnewmod.pod b/gnu/usr.bin/perl/pod/perlnewmod.pod index 26c4c139796..ace8d85130f 100644 --- a/gnu/usr.bin/perl/pod/perlnewmod.pod +++ b/gnu/usr.bin/perl/pod/perlnewmod.pod @@ -23,9 +23,6 @@ trying to do, and you've had to write the code yourself, consider packaging up the solution into a module and uploading it to CPAN so that others can benefit. -You should also take a look at L<perlmodstyle> for best practices in -making a module. - =head2 Warning We're going to primarily concentrate on Perl-only modules here, rather @@ -76,10 +73,9 @@ want to do in advance. Dig into a bunch of modules to see how they're written. I'd suggest starting with L<Text::Tabs|Text::Tabs>, since it's in the standard -library and is nice and simple, and then looking at something a little -more complex like L<File::Copy|File::Copy>. For object oriented -code, C<WWW::Mechanize> or the C<Email::*> modules provide some good -examples. +library and is nice and simple, and then looking at something like +L<Time::Zone|Time::Zone>, L<File::Copy|File::Copy> and then some of the +C<Mail::*> modules if you're planning on writing object oriented code. These should give you an overall feel for how modules are laid out and written. @@ -88,14 +84,14 @@ written. There are a lot of modules on CPAN, and it's easy to miss one that's similar to what you're planning on contributing. Have a good plough -through the L<http://search.cpan.org> and make sure you're not the one -reinventing the wheel! +through the modules list and the F<by-module> directories, and make sure +you're not the one reinventing the wheel! =item Discuss the need You might love it. You might feel that everyone else needs it. But there might not actually be any real demand for it out there. If you're unsure -about the demand your module will have, consider sending out feelers +about the demand you're module will have, consider sending out feelers on the C<comp.lang.perl.modules> newsgroup, or as a last resort, ask the modules list at C<modules@perl.org>. Remember that this is a closed list with a very long turn-around time - be prepared to wait a good while for @@ -123,29 +119,18 @@ wanted and not currently available, it's time to start coding. =over 3 -=item Start with F<module-starter> or F<h2xs> - -The F<module-starter> utility is distributed as part of the -L<Module::Starter|Module::Starter> CPAN package. It creates a directory -with stubs of all the necessary files to start a new module, according -to recent "best practice" for module development, and is invoked from -the command line, thus: - - module-starter --module=Foo::Bar \ - --author="Your Name" --email=yourname@cpan.org +=item Start with F<h2xs> -If you do not wish to install the L<Module::Starter|Module::Starter> -package from CPAN, F<h2xs> is an older tool, originally intended for the -development of XS modules, which comes packaged with the Perl -distribution. +Originally a utility to convert C header files into XS modules, +L<h2xs|h2xs> has become a useful utility for churning out skeletons for +Perl-only modules as well. If you don't want to use the +L<Autoloader|Autoloader> which splits up big modules into smaller +subroutine-sized chunks, you'll say something like this: -A typical invocation of L<h2xs|h2xs> for a pure Perl module is: + h2xs -AX -n Net::Acme - h2xs -AX --skip-exporter --use-new-tests -n Foo::Bar - -The C<-A> omits the Autoloader code, C<-X> omits XS elements, -C<--skip-exporter> omits the Exporter code, C<--use-new-tests> sets up a -modern testing environment, and C<-n> specifies the name of the module. +The C<-A> omits the Autoloader code, C<-X> omits XS elements, and C<-n> +specifies the name of the module. =item Use L<strict|strict> and L<warnings|warnings> @@ -179,9 +164,10 @@ your module at fault. =item Use L<Exporter|Exporter> - wisely! -L<Exporter|Exporter> gives you a standard way of exporting symbols and -subroutines from your module into the caller's namespace. For instance, -saying C<use Net::Acme qw(&frob)> would import the C<frob> subroutine. +C<h2xs> provides stubs for L<Exporter|Exporter>, which gives you a +standard way of exporting symbols and subroutines from your module into +the caller's namespace. For instance, saying C<use Net::Acme qw(&frob)> +would import the C<frob> subroutine. The package variable C<@EXPORT> will determine which symbols will get exported when the caller simply says C<use Net::Acme> - you will hardly @@ -194,23 +180,21 @@ export set - look at L<Exporter> for more details. The work isn't over until the paperwork is done, and you're going to need to put in some time writing some documentation for your module. -C<module-starter> or C<h2xs> will provide a stub for you to fill in; if -you're not sure about the format, look at L<perlpod> for an -introduction. Provide a good synopsis of how your module is used in -code, a description, and then notes on the syntax and function of the -individual subroutines or methods. Use Perl comments for developer notes -and POD for end-user notes. +C<h2xs> will provide a stub for you to fill in; if you're not sure about +the format, look at L<perlpod> for an introduction. Provide a good +synopsis of how your module is used in code, a description, and then +notes on the syntax and function of the individual subroutines or +methods. Use Perl comments for developer notes and POD for end-user +notes. =item Write tests You're encouraged to create self-tests for your module to ensure it's working as intended on the myriad platforms Perl supports; if you upload your module to CPAN, a host of testers will build your module and send -you the results of the tests. Again, C<module-starter> and C<h2xs> -provide a test framework which you can extend - you should do something -more than just checking your module will compile. -L<Test::Simple|Test::Simple> and L<Test::More|Test::More> are good -places to start when writing a test suite. +you the results of the tests. Again, C<h2xs> provides a test framework +which you can extend - you should do something more than just checking +your module will compile. =item Write the README @@ -228,15 +212,15 @@ does in detail, and the user-visible changes since the last release. =item Get a CPAN user ID -Every developer publishing modules on CPAN needs a CPAN ID. Visit -C<http://pause.perl.org/>, select "Request PAUSE Account", and wait for -your request to be approved by the PAUSE administrators. +Every developer publishing modules on CPAN needs a CPAN ID. See the +instructions at C<http://www.cpan.org/modules/04pause.html> (or +equivalent on your nearest mirror) to find out how to do this. =item C<perl Makefile.PL; make test; make dist> -Once again, C<module-starter> or C<h2xs> has done all the work for you. -They produce the standard C<Makefile.PL> you see when you download and -install modules, and this produces a Makefile with a C<dist> target. +Once again, C<h2xs> has done all the work for you. It produces the +standard C<Makefile.PL> you'll have seen when you downloaded and +installs modules, and this produces a Makefile with a C<dist> target. Once you've ensured that your module passes its own tests - always a good thing to make sure - you can C<make dist>, and the Makefile will @@ -251,9 +235,28 @@ you can upload your module to CPAN. =item Announce to the modules list Once uploaded, it'll sit unnoticed in your author directory. If you want -it connected to the rest of the CPAN, you'll need to go to "Register -Namespace" on PAUSE. Once registered, your module will appear in the -by-module and by-category listings on CPAN. +it connected to the rest of the CPAN, you'll need to tell the modules +list about it. The best way to do this is to email them a line in the +style of the modules list, like this: + + Net::Acme bdpO Interface to Acme Frobnicator servers FOOBAR + ^ ^^^^ ^ ^ + | |||| Module description Your ID + | |||| + | |||\- Interface: (O)OP, (r)eferences, (h)ybrid, (f)unctions + | ||| + | ||\-- Language: (p)ure Perl, C(+)+, (h)ybrid, (C), (o)ther + | || + Module |\--- Support: (d)eveloper, (m)ailing list, (u)senet, (n)one + Name | + \---- Maturity: (i)dea, (c)onstructions, (a)lpha, (b)eta, + (R)eleased, (M)ature, (S)tandard + +plus a description of the module and why you think it should be +included. If you hear nothing back, that means your module will +probably appear on the modules list at the next update. Don't try +subscribing to C<modules@perl.org>; it's not another mailing list. Just +have patience. =item Announce to clpa @@ -272,12 +275,8 @@ maintaining a software project... Simon Cozens, C<simon@cpan.org> -Updated by Kirrily "Skud" Robert, C<skud@cpan.org> - =head1 SEE ALSO L<perlmod>, L<perlmodlib>, L<perlmodinstall>, L<h2xs>, L<strict>, -L<Carp>, L<Exporter>, L<perlpod>, L<Test::Simple>, L<Test::More> -L<ExtUtils::MakeMaker>, L<Module::Build>, L<Module::Starter> -http://www.cpan.org/ , Ken Williams's tutorial on building your own -module at http://mathforum.org/~ken/perl_modules.html +L<Carp>, L<Exporter>, L<perlpod>, L<Test>, L<ExtUtils::MakeMaker>, +http://www.cpan.org/ diff --git a/gnu/usr.bin/perl/pod/perlrequick.pod b/gnu/usr.bin/perl/pod/perlrequick.pod index 008ef339fe2..5b72a35187f 100644 --- a/gnu/usr.bin/perl/pod/perlrequick.pod +++ b/gnu/usr.bin/perl/pod/perlrequick.pod @@ -19,7 +19,7 @@ contains that word: "Hello World" =~ /World/; # matches In this statement, C<World> is a regex and the C<//> enclosing -C</World/> tells Perl to search a string for a match. The operator +C</World/> tells perl to search a string for a match. The operator C<=~> associates the string with the regex match and produces a true value if the regex matched, or false if the regex did not match. In our case, C<World> matches the second word in C<"Hello World">, so the @@ -58,7 +58,7 @@ statement to be true: "Hello World" =~ /o W/; # matches, ' ' is an ordinary char "Hello World" =~ /World /; # doesn't match, no ' ' at end -Perl will always match at the earliest possible point in the string: +perl will always match at the earliest possible point in the string: "Hello World" =~ /o/; # matches 'o' in 'Hello' "That hat is red" =~ /hat/; # matches 'hat' in 'That' @@ -74,7 +74,7 @@ A metacharacter can be matched by putting a backslash before it: "2+2=4" =~ /2+2/; # doesn't match, + is a metacharacter "2+2=4" =~ /2\+2/; # matches, \+ is treated like an ordinary + 'C:\WIN32' =~ /C:\\WIN/; # matches - "/usr/bin/perl" =~ /\/usr\/bin\/perl/; # matches + "/usr/bin/perl" =~ /\/usr\/local\/bin\/perl/; # matches In the last regex, the forward slash C<'/'> is also backslashed, because it is used to delimit the regex. @@ -85,11 +85,10 @@ for a carriage return. Arbitrary bytes are represented by octal escape sequences, e.g., C<\033>, or hexadecimal escape sequences, e.g., C<\x1B>: - "1000\t2000" =~ m(0\t2) # matches - "cat" =~ /\143\x61\x74/ # matches in ASCII, but - # a weird way to spell cat + "1000\t2000" =~ m(0\t2) # matches + "cat" =~ /\143\x61\x74/ # matches, but a weird way to spell cat -Regexes are treated mostly as double-quoted strings, so variable +Regexes are treated mostly as double quoted strings, so variable substitution works: $foo = 'house'; @@ -162,48 +161,33 @@ character, or the match fails. Then /[^0-9]/; # matches a non-numeric character /[a^]at/; # matches 'aat' or '^at'; here '^' is ordinary -Perl has several abbreviations for common character classes. (These -definitions are those that Perl uses in ASCII-safe mode with the C</a> modifier. -Otherwise they could match many more non-ASCII Unicode characters as -well. See L<perlrecharclass/Backslash sequences> for details.) +Perl has several abbreviations for common character classes: =over 4 =item * -\d is a digit and represents - - [0-9] +\d is a digit and represents [0-9] =item * -\s is a whitespace character and represents - - [\ \t\r\n\f] +\s is a whitespace character and represents [\ \t\r\n\f] =item * -\w is a word character (alphanumeric or _) and represents - - [0-9a-zA-Z_] +\w is a word character (alphanumeric or _) and represents [0-9a-zA-Z_] =item * -\D is a negated \d; it represents any character but a digit - - [^0-9] +\D is a negated \d; it represents any character but a digit [^0-9] =item * -\S is a negated \s; it represents any non-whitespace character - - [^\s] +\S is a negated \s; it represents any non-whitespace character [^\s] =item * -\W is a negated \w; it represents any non-word character - - [^\w] +\W is a negated \w; it represents any non-word character [^\w] =item * @@ -235,13 +219,13 @@ boundary. =head2 Matching this or that -We can match different character strings with the B<alternation> +We can match match different character strings with the B<alternation> metacharacter C<'|'>. To match C<dog> or C<cat>, we form the regex -C<dog|cat>. As before, Perl will try to match the regex at the +C<dog|cat>. As before, perl will try to match the regex at the earliest possible point in the string. At each character position, -Perl will first try to match the first alternative, C<dog>. If -C<dog> doesn't match, Perl will then try the next alternative, C<cat>. -If C<cat> doesn't match either, then the match fails and Perl moves to +perl will first try to match the the first alternative, C<dog>. If +C<dog> doesn't match, perl will then try the next alternative, C<cat>. +If C<cat> doesn't match either, then the match fails and perl moves to the next position in the string. Some examples: "cats and dogs" =~ /cat|dog|bird/; # matches "cat" @@ -254,8 +238,8 @@ C<cat> is able to match earlier in the string. "cats" =~ /cats|cat|ca|c/; # matches "cats" At a given character position, the first alternative that allows the -regex match to succeed will be the one that matches. Here, all the -alternatives match at the first string position, so the first matches. +regex match to succeed wil be the one that matches. Here, all the +alternatives match at the first string position, so th first matches. =head2 Grouping things and hierarchical matching @@ -302,13 +286,13 @@ indicated below it: 1 2 34 Associated with the matching variables C<$1>, C<$2>, ... are -the B<backreferences> C<\g1>, C<\g2>, ... Backreferences are +the B<backreferences> C<\1>, C<\2>, ... Backreferences are matching variables that can be used I<inside> a regex: - /(\w\w\w)\s\g1/; # find sequences like 'the the' in string + /(\w\w\w)\s\1/; # find sequences like 'the the' in string -C<$1>, C<$2>, ... should only be used outside of a regex, and C<\g1>, -C<\g2>, ... only inside a regex. +C<$1>, C<$2>, ... should only be used outside of a regex, and C<\1>, +C<\2>, ... only inside a regex. =head2 Matching repetitions @@ -351,10 +335,10 @@ Here are some examples: /[a-z]+\s+\d*/; # match a lowercase word, at least some space, and # any number of digits - /(\w+)\s+\g1/; # match doubled words of arbitrary length - $year =~ /^\d{2,4}$/; # make sure year is at least 2 but not more - # than 4 digits - $year =~ /^\d{4}$|^\d{2}$/; # better match; throw out 3 digit dates + /(\w+)\s+\1/; # match doubled words of arbitrary length + $year =~ /\d{2,4}/; # make sure year is at least 2 but not more + # than 4 digits + $year =~ /\d{4}|\d{2}/; # better match; throw out 3 digit dates These quantifiers will try to match as much of the string as possible, while still allowing the regex to match. So we have @@ -372,7 +356,21 @@ no string left to it, so it matches 0 times. =head2 More matching There are a few more things you might want to know about matching -operators. +operators. In the code + + $pattern = 'Seuss'; + while (<>) { + print if /$pattern/; + } + +perl has to re-evaluate C<$pattern> each time through the loop. If +C<$pattern> won't be changing, use the C<//o> modifier, to only +perform variable substitutions once. If you don't want any +substitutions at all, use the special delimiter C<m''>: + + $pattern = 'Seuss'; + m'$pattern'; # matches '$pattern', not 'Seuss' + The global modifier C<//g> allows the matching operator to match within a string as many times as possible. In scalar context, successive matches against a string will have C<//g> jump from match @@ -406,11 +404,11 @@ there are no groupings, a list of matches to the whole regex. So =head2 Search and replace Search and replace is performed using C<s/regex/replacement/modifiers>. -The C<replacement> is a Perl double-quoted string that replaces in the +The C<replacement> is a Perl double quoted string that replaces in the string whatever is matched with the C<regex>. The operator C<=~> is also used here to associate a string with C<s///>. If matching -against C<$_>, the S<C<$_ =~>> can be dropped. If there is a match, -C<s///> returns the number of substitutions made; otherwise it returns +against C<$_>, the S<C<$_ =~> > can be dropped. If there is a match, +C<s///> returns the number of substitutions made, otherwise it returns false. Here are a few examples: $x = "Time to feed the cat!"; @@ -429,22 +427,6 @@ of the regex in the string: $x = "I batted 4 for 4"; $x =~ s/4/four/g; # $x contains "I batted four for four" -The non-destructive modifier C<s///r> causes the result of the substitution -to be returned instead of modifying C<$_> (or whatever variable the -substitute was bound to with C<=~>): - - $x = "I like dogs."; - $y = $x =~ s/dogs/cats/r; - print "$x $y\n"; # prints "I like dogs. I like cats." - - $x = "Cats are great."; - print $x =~ s/Cats/Dogs/r =~ s/Dogs/Frogs/r =~ - s/Frogs/Hedgehogs/r, "\n"; - # prints "Hedgehogs are great." - - @foo = map { s/[a-z]/X/r } qw(a b c 1 2 3); - # @foo is now qw(X X X 1 2 3) - The evaluation modifier C<s///e> wraps an C<eval{...}> around the replacement string and the evaluated result is substituted for the matched substring. Some examples: @@ -459,7 +441,7 @@ matched substring. Some examples: The last example shows that C<s///> can use other delimiters, such as C<s!!!> and C<s{}{}>, and even C<s{}//>. If single quotes are used -C<s'''>, then the regex and replacement are treated as single-quoted +C<s'''>, then the regex and replacement are treated as single quoted strings. =head2 The split operator @@ -482,7 +464,7 @@ To extract a comma-delimited list of numbers, use # $const[2] = '3.142' If the empty regex C<//> is used, the string is split into individual -characters. If the regex has groupings, then the list produced contains +characters. If the regex has groupings, then list produced contains the matched substrings from the groupings as well: $x = "/usr/bin"; diff --git a/gnu/usr.bin/perl/pod/perlretut.pod b/gnu/usr.bin/perl/pod/perlretut.pod index 79400fc19d1..fa6479c0c45 100644 --- a/gnu/usr.bin/perl/pod/perlretut.pod +++ b/gnu/usr.bin/perl/pod/perlretut.pod @@ -41,7 +41,7 @@ you master the first part, you will have all the tools needed to solve about 98% of your needs. The second part of the tutorial is for those comfortable with the basics and hungry for more power tools. It discusses the more advanced regular expression operators and -introduces the latest cutting-edge innovations. +introduces the latest cutting edge innovations in 5.6.0. A note: to save time, 'regular expression' is often abbreviated as regexp or regex. Regexp is a more natural abbreviation than regex, but @@ -59,9 +59,9 @@ contains that word: "Hello World" =~ /World/; # matches -What is this Perl statement all about? C<"Hello World"> is a simple -double-quoted string. C<World> is the regular expression and the -C<//> enclosing C</World/> tells Perl to search a string for a match. +What is this perl statement all about? C<"Hello World"> is a simple +double quoted string. C<World> is the regular expression and the +C<//> enclosing C</World/> tells perl to search a string for a match. The operator C<=~> associates the string with the regexp match and produces a true value if the regexp matched, or false if the regexp did not match. In our case, C<World> matches the second word in @@ -76,7 +76,7 @@ are useful in conditionals: } There are useful variations on this theme. The sense of the match can -be reversed by using the C<!~> operator: +be reversed by using C<!~> operator: if ("Hello World" !~ /World/) { print "It doesn't match\n"; @@ -115,8 +115,8 @@ to arbitrary delimiters by putting an C<'m'> out front: # '/' becomes an ordinary char C</World/>, C<m!World!>, and C<m{World}> all represent the -same thing. When, e.g., the quote (C<">) is used as a delimiter, the forward -slash C<'/'> becomes an ordinary character and can be used in this regexp +same thing. When, e.g., C<""> is used as a delimiter, the forward +slash C<'/'> becomes an ordinary character and can be used in a regexp without trouble. Let's consider how different regexps would match C<"Hello World">: @@ -128,7 +128,7 @@ Let's consider how different regexps would match C<"Hello World">: The first regexp C<world> doesn't match because regexps are case-sensitive. The second regexp matches because the substring -S<C<'o W'>> occurs in the string S<C<"Hello World">>. The space +S<C<'o W'> > occurs in the string S<C<"Hello World"> >. The space character ' ' is treated like any other character in a regexp and is needed to match in this case. The lack of a space character is the reason the third regexp C<'oW'> doesn't match. The fourth regexp @@ -137,7 +137,7 @@ regexp, but not at the end of the string. The lesson here is that regexps must match a part of the string I<exactly> in order for the statement to be true. -If a regexp matches in more than one place in the string, Perl will +If a regexp matches in more than one place in the string, perl will always match at the earliest possible point in the string: "Hello World" =~ /o/; # matches 'o' in 'Hello' @@ -145,7 +145,7 @@ always match at the earliest possible point in the string: With respect to character matching, there are a few more points you need to know about. First of all, not all characters can be used 'as -is' in a match. Some characters, called I<metacharacters>, are reserved +is' in a match. Some characters, called B<metacharacters>, are reserved for use in regexp notation. The metacharacters are {}[]()^$.|*+?\ @@ -158,14 +158,13 @@ that a metacharacter can be matched by putting a backslash before it: "2+2=4" =~ /2\+2/; # matches, \+ is treated like an ordinary + "The interval is [0,1)." =~ /[0,1)./ # is a syntax error! "The interval is [0,1)." =~ /\[0,1\)\./ # matches - "#!/usr/bin/perl" =~ /#!\/usr\/bin\/perl/; # matches + "/usr/bin/perl" =~ /\/usr\/local\/bin\/perl/; # matches In the last regexp, the forward slash C<'/'> is also backslashed, because it is used to delimit the regexp. This can lead to LTS (leaning toothpick syndrome), however, and it is often more readable to change delimiters. - "#!/usr/bin/perl" =~ m!#\!/usr/bin/perl!; # easier to read The backslash character C<'\'> is a metacharacter itself and needs to be backslashed: @@ -174,9 +173,9 @@ be backslashed: In addition to the metacharacters, there are some ASCII characters which don't have printable character equivalents and are instead -represented by I<escape sequences>. Common examples are C<\t> for a +represented by B<escape sequences>. Common examples are C<\t> for a tab, C<\n> for a newline, C<\r> for a carriage return and C<\a> for a -bell (or alert). If your string is better thought of as a sequence of arbitrary +bell. If your string is better thought of as a sequence of arbitrary bytes, the octal escape sequence, e.g., C<\033>, or hexadecimal escape sequence, e.g., C<\x1B> may be a more natural representation for your bytes. Here are some examples of escapes: @@ -184,8 +183,7 @@ bytes. Here are some examples of escapes: "1000\t2000" =~ m(0\t2) # matches "1000\n2000" =~ /0\n20/ # matches "1000\t2000" =~ /\000\t2/ # doesn't match, "0" ne "\000" - "cat" =~ /\o{143}\x61\x74/ # matches in ASCII, but a weird way - # to spell cat + "cat" =~ /\143\x61\x74/ # matches, but a weird way to spell cat If you've been around Perl a while, all this talk of escape sequences may seem familiar. Similar escape sequences are used in double-quoted @@ -227,17 +225,17 @@ Here is a I<very simple> emulation of the Unix grep program: This program is easy to understand. C<#!/usr/bin/perl> is the standard way to invoke a perl program from the shell. -S<C<$regexp = shift;>> saves the first command line argument as the +S<C<$regexp = shift;> > saves the first command line argument as the regexp to be used, leaving the rest of the command line arguments to -be treated as files. S<C<< while (<>) >>> loops over all the lines in -all the files. For each line, S<C<print if /$regexp/;>> prints the +be treated as files. S<C<< while (<>) >> > loops over all the lines in +all the files. For each line, S<C<print if /$regexp/;> > prints the line if the regexp matches the line. In this line, both C<print> and C</$regexp/> use the default variable C<$_> implicitly. With all of the regexps above, if the regexp matched anywhere in the string, it was considered a match. Sometimes, however, we'd like to specify I<where> in the string the regexp should try to match. To do -this, we would use the I<anchor> metacharacters C<^> and C<$>. The +this, we would use the B<anchor> metacharacters C<^> and C<$>. The anchor C<^> means match at the beginning of the string and the anchor C<$> means match at the end of the string, or before a newline at the end of the string. Here is how they are used: @@ -277,7 +275,7 @@ bert, off in a string by himself: "bert" =~ /^bert$/; # matches, perfect Of course, in the case of a literal string, one could just as easily -use the string comparison S<C<$string eq 'bert'>> and it would be +use the string equivalence S<C<$string eq 'bert'> > and it would be more efficient. The C<^...$> regexp really becomes useful when we add in the more powerful regexp tools below. @@ -287,14 +285,13 @@ Although one can already do quite a lot with the literal string regexps above, we've only scratched the surface of regular expression technology. In this and subsequent sections we will introduce regexp concepts (and associated metacharacter notations) that will allow a -regexp to represent not just a single character sequence, but a I<whole +regexp to not just represent a single character sequence, but a I<whole class> of them. -One such concept is that of a I<character class>. A character class +One such concept is that of a B<character class>. A character class allows a set of possible characters, rather than just a single -character, to match at a particular point in a regexp. You can define -your own custom character classes. These -are denoted by brackets C<[...]>, with the set of characters +character, to match at a particular point in a regexp. Character +classes are denoted by brackets C<[...]>, with the set of characters to be possibly matched inside. Here are some examples: /cat/; # matches 'cat' @@ -309,11 +306,11 @@ string is the earliest point at which the regexp can match. /[yY][eE][sS]/; # match 'yes' in a case-insensitive way # 'yes', 'Yes', 'YES', etc. -This regexp displays a common task: perform a case-insensitive -match. Perl provides a way of avoiding all those brackets by simply +This regexp displays a common task: perform a a case-insensitive +match. Perl provides away of avoiding all those brackets by simply appending an C<'i'> to the end of the match. Then C</[yY][eE][sS]/;> can be rewritten as C</yes/i;>. The C<'i'> stands for -case-insensitive and is an example of a I<modifier> of the matching +case-insensitive and is an example of a B<modifier> of the matching operation. We will meet other modifiers later in the tutorial. We saw in the section above that there were ordinary characters, which @@ -321,9 +318,8 @@ represented themselves, and special characters, which needed a backslash C<\> to represent themselves. The same is true in a character class, but the sets of ordinary and special characters inside a character class are different than those outside a character -class. The special characters for a character class are C<-]\^$> (and -the pattern delimiter, whatever it is). -C<]> is special because it denotes the end of a character class. C<$> is +class. The special characters for a character class are C<-]\^$>. C<]> +is special because it denotes the end of a character class. C<$> is special because it denotes a scalar variable. C<\> is special because it is used in escape sequences, just like above. Here is how the special characters C<]$\> are handled: @@ -334,7 +330,7 @@ special characters C<]$\> are handled: /[\$x]at/; # matches '$at' or 'xat' /[\\$x]at/; # matches '\at', 'bat, 'cat', or 'rat' -The last two are a little tricky. In C<[\$x]>, the backslash protects +The last two are a little tricky. in C<[\$x]>, the backslash protects the dollar sign, so the character class has two members C<$> and C<x>. In C<[\\$x]>, the backslash is protected, so C<$x> is treated as a variable and substituted in double quote fashion. @@ -349,14 +345,14 @@ become the svelte C<[0-9]> and C<[a-z]>. Some examples are # 'baa', 'xaa', 'yaa', or 'zaa' /[0-9a-fA-F]/; # matches a hexadecimal digit /[0-9a-zA-Z_]/; # matches a "word" character, - # like those in a Perl variable name + # like those in a perl variable name If C<'-'> is the first or last character in a character class, it is treated as an ordinary character; C<[-ab]>, C<[ab-]> and C<[a\-b]> are all equivalent. The special character C<^> in the first position of a character class -denotes a I<negated character class>, which matches any character but +denotes a B<negated character class>, which matches any character but those in the brackets. Both C<[...]> and C<[^...]> must match a character, or the match fails. Then @@ -365,31 +361,27 @@ character, or the match fails. Then /[^0-9]/; # matches a non-numeric character /[a^]at/; # matches 'aat' or '^at'; here '^' is ordinary -Now, even C<[0-9]> can be a bother to write multiple times, so in the +Now, even C<[0-9]> can be a bother the write multiple times, so in the interest of saving keystrokes and making regexps more readable, Perl -has several abbreviations for common character classes, as shown below. -Since the introduction of Unicode, unless the C<//a> modifier is in -effect, these character classes match more than just a few characters in -the ASCII range. +has several abbreviations for common character classes: =over 4 =item * -\d matches a digit, not just [0-9] but also digits from non-roman scripts +\d is a digit and represents [0-9] =item * -\s matches a whitespace character, the set [\ \t\r\n\f] and others +\s is a whitespace character and represents [\ \t\r\n\f] =item * -\w matches a word character (alphanumeric or _), not just [0-9a-zA-Z_] -but also digits and characters from non-roman scripts +\w is a word character (alphanumeric or _) and represents [0-9a-zA-Z_] =item * -\D is a negated \d; it represents any other character than a digit, or [^\d] +\D is a negated \d; it represents any character but a digit [^0-9] =item * @@ -401,27 +393,12 @@ but also digits and characters from non-roman scripts =item * -The period '.' matches any character but "\n" (unless the modifier C<//s> is -in effect, as explained below). - -=item * - -\N, like the period, matches any character but "\n", but it does so -regardless of whether the modifier C<//s> is in effect. +The period '.' matches any character but "\n" =back -The C<//a> modifier, available starting in Perl 5.14, is used to -restrict the matches of \d, \s, and \w to just those in the ASCII range. -It is useful to keep your program from being needlessly exposed to full -Unicode (and its accompanying security considerations) when all you want -is to process English-like text. (The "a" may be doubled, C<//aa>, to -provide even more restrictions, preventing case-insensitive matching of -ASCII with non-ASCII characters; otherwise a Unicode "Kelvin Sign" -would caselessly match a "k" or "K".) - The C<\d\s\w\D\S\W> abbreviations can be used both inside and outside -of bracketed character classes. Here are some in use: +of character classes. Here are some in use: /\d\d:\d\d:\d\d/; # matches a hh:mm:ss time format /[\d\s]/; # matches any digit or whitespace character @@ -437,12 +414,7 @@ of characters, it is incorrect to think of C<[^\d\w]> as C<[\D\W]>; in fact C<[^\d\w]> is the same as C<[^\w]>, which is the same as C<[\W]>. Think DeMorgan's laws. -In actuality, the period and C<\d\s\w\D\S\W> abbreviations are -themselves types of character classes, so the ones surrounded by -brackets are just one type of character class. When we need to make a -distinction, we refer to them as "bracketed character classes." - -An anchor useful in basic regexps is the I<word anchor> +An anchor useful in basic regexps is the S<B<word anchor> > C<\b>. This matches a boundary between a word character and a non-word character C<\w\W> or C<\W\w>: @@ -459,16 +431,16 @@ You might wonder why C<'.'> matches everything but C<"\n"> - why not every character? The reason is that often one is matching against lines and would like to ignore the newline characters. For instance, while the string C<"\n"> represents one line, we would like to think -of it as empty. Then +of as empty. Then "" =~ /^$/; # matches - "\n" =~ /^$/; # matches, $ anchors before "\n" + "\n" =~ /^$/; # matches, "\n" is ignored "" =~ /./; # doesn't match; it needs a char "" =~ /^.$/; # doesn't match; it needs a char "\n" =~ /^.$/; # doesn't match; it needs a char other than "\n" "a" =~ /^.$/; # matches - "a\n" =~ /^.$/; # matches, $ anchors before "\n" + "a\n" =~ /^.$/; # matches, ignores the "\n" This behavior is convenient, because we usually want to ignore newlines when we count and match characters in a line. Sometimes, @@ -527,9 +499,9 @@ Here are examples of C<//s> and C<//m> in action: $x =~ /girl.Who/m; # doesn't match, "." doesn't match "\n" $x =~ /girl.Who/sm; # matches, "." matches "\n" -Most of the time, the default behavior is what is wanted, but C<//s> and +Most of the time, the default behavior is what is want, but C<//s> and C<//m> are occasionally very useful. If C<//m> is being used, the start -of the string can still be matched with C<\A> and the end of the string +of the string can still be matched with C<\A> and the end of string can still be matched with the anchors C<\Z> (matches both the end and the newline before, like C<$>), and C<\z> (matches only the end): @@ -548,15 +520,15 @@ choices are described in the next section. =head2 Matching this or that -Sometimes we would like our regexp to be able to match different +Sometimes we would like to our regexp to be able to match different possible words or character strings. This is accomplished by using -the I<alternation> metacharacter C<|>. To match C<dog> or C<cat>, we -form the regexp C<dog|cat>. As before, Perl will try to match the +the B<alternation> metacharacter C<|>. To match C<dog> or C<cat>, we +form the regexp C<dog|cat>. As before, perl will try to match the regexp at the earliest possible point in the string. At each -character position, Perl will first try to match the first -alternative, C<dog>. If C<dog> doesn't match, Perl will then try the +character position, perl will first try to match the first +alternative, C<dog>. If C<dog> doesn't match, perl will then try the next alternative, C<cat>. If C<cat> doesn't match either, then the -match fails and Perl moves to the next position in the string. Some +match fails and perl moves to the next position in the string. Some examples: "cats and dogs" =~ /cat|dog|bird/; # matches "cat" @@ -578,21 +550,21 @@ to give them a chance to match. The last example points out that character classes are like alternations of characters. At a given character position, the first -alternative that allows the regexp match to succeed will be the one +alternative that allows the regexp match to succeed wil be the one that matches. =head2 Grouping things and hierarchical matching Alternation allows a regexp to choose among alternatives, but by -itself it is unsatisfying. The reason is that each alternative is a whole +itself it unsatisfying. The reason is that each alternative is a whole regexp, but sometime we want alternatives for just part of a regexp. For instance, suppose we want to search for housecats or housekeepers. The regexp C<housecat|housekeeper> fits the bill, but is inefficient because we had to type C<house> twice. It would be nice to -have parts of the regexp be constant, like C<house>, and some +have parts of the regexp be constant, like C<house>, and and some parts have alternatives, like C<cat|keeper>. -The I<grouping> metacharacters C<()> solve this problem. Grouping +The B<grouping> metacharacters C<()> solve this problem. Grouping allows parts of a regexp to be treated as a single unit. Parts of a regexp are grouped by enclosing them in parentheses. Thus we could solve the C<housecat|housekeeper> by forming the regexp as @@ -615,16 +587,15 @@ are Alternations behave the same way in groups as out of them: at a given string position, the leftmost alternative that allows the regexp to -match is taken. So in the last example at the first string position, +match is taken. So in the last example at tth first string position, C<"20"> matches the second alternative, but there is nothing left over -to match the next two digits C<\d\d>. So Perl moves on to the next +to match the next two digits C<\d\d>. So perl moves on to the next alternative, which is the null alternative and that works, since C<"20"> is two digits. The process of trying one alternative, seeing if it matches, and -moving on to the next alternative, while going back in the string -from where the previous alternative was tried, if it doesn't, is called -I<backtracking>. The term 'backtracking' comes from the idea that +moving on to the next alternative if it doesn't, is called +B<backtracking>. The term 'backtracking' comes from the idea that matching a regexp is like a walk in the woods. Successfully matching a regexp is like arriving at a destination. There are many possible trailheads, one for each string position, and each one is tried in @@ -636,56 +607,56 @@ destination, you stop immediately and forget about trying all the other trails. You are persistent, and only if you have tried all the trails from all the trailheads and not arrived at your destination, do you declare failure. To be concrete, here is a step-by-step analysis -of what Perl does when it tries to match the regexp +of what perl does when it tries to match the regexp "abcde" =~ /(abd|abc)(df|d|de)/; =over 4 -=item Z<>0 +=item 0 Start with the first letter in the string 'a'. -=item Z<>1 +=item 1 Try the first alternative in the first group 'abd'. -=item Z<>2 +=item 2 Match 'a' followed by 'b'. So far so good. -=item Z<>3 +=item 3 'd' in the regexp doesn't match 'c' in the string - a dead end. So backtrack two characters and pick the second alternative in the first group 'abc'. -=item Z<>4 +=item 4 Match 'a' followed by 'b' followed by 'c'. We are on a roll and have satisfied the first group. Set $1 to 'abc'. -=item Z<>5 +=item 5 Move on to the second group and pick the first alternative 'df'. -=item Z<>6 +=item 6 Match the 'd'. -=item Z<>7 +=item 7 'f' in the regexp doesn't match 'e' in the string, so a dead end. Backtrack one character and pick the second alternative in the second group 'd'. -=item Z<>8 +=item 8 'd' matches. The second grouping is satisfied, so set $2 to 'd'. -=item Z<>9 +=item 9 We are at the end of the regexp, so we are done! We have matched 'abcd' out of the string "abcde". @@ -697,16 +668,16 @@ third alternative in the second group 'de' also allows a match, but we stopped before we got to it - at a given character position, leftmost wins. Second, we were able to get a match at the first character position of the string 'a'. If there were no matches at the first -position, Perl would move to the second character position 'b' and +position, perl would move to the second character position 'b' and attempt the match all over again. Only when all possible paths at all -possible character positions have been exhausted does Perl give -up and declare S<C<$string =~ /(abd|abc)(df|d|de)/;>> to be false. +possible character positions have been exhausted does perl give give +up and declare S<C<$string =~ /(abd|abc)(df|d|de)/;> > to be false. Even with all this work, regexp matching happens remarkably fast. To -speed things up, Perl compiles the regexp into a compact sequence of -opcodes that can often fit inside a processor cache. When the code is -executed, these opcodes can then run at full throttle and search very -quickly. +speed things up, during compilation stage, perl compiles the regexp +into a compact sequence of opcodes that can often fit inside a +processor cache. When the code is executed, these opcodes can then run +at full throttle and search very quickly. =head2 Extracting matches @@ -718,14 +689,13 @@ inside goes into the special variables C<$1>, C<$2>, etc. They can be used just as ordinary variables: # extract hours, minutes, seconds - if ($time =~ /(\d\d):(\d\d):(\d\d)/) { # match hh:mm:ss format - $hours = $1; - $minutes = $2; - $seconds = $3; - } + $time =~ /(\d\d):(\d\d):(\d\d)/; # match hh:mm:ss format + $hours = $1; + $minutes = $2; + $seconds = $3; Now, we know that in scalar context, -S<C<$time =~ /(\d\d):(\d\d):(\d\d)/>> returns a true or false +S<C<$time =~ /(\d\d):(\d\d):(\d\d)/> > returns a true or false value. In list context, however, it returns the list of matched values C<($1,$2,$3)>. So we could write the code more compactly as @@ -734,41 +704,30 @@ C<($1,$2,$3)>. So we could write the code more compactly as If the groupings in a regexp are nested, C<$1> gets the group with the leftmost opening parenthesis, C<$2> the next opening parenthesis, -etc. Here is a regexp with nested groups: +etc. For example, here is a complex regexp and the matching variables +indicated below it: /(ab(cd|ef)((gi)|j))/; 1 2 34 -If this regexp matches, C<$1> contains a string starting with -C<'ab'>, C<$2> is either set to C<'cd'> or C<'ef'>, C<$3> equals either -C<'gi'> or C<'j'>, and C<$4> is either set to C<'gi'>, just like C<$3>, -or it remains undefined. - -For convenience, Perl sets C<$+> to the string held by the highest numbered -C<$1>, C<$2>,... that got assigned (and, somewhat related, C<$^N> to the -value of the C<$1>, C<$2>,... most-recently assigned; i.e. the C<$1>, -C<$2>,... associated with the rightmost closing parenthesis used in the -match). - - -=head2 Backreferences +so that if the regexp matched, e.g., C<$2> would contain 'cd' or 'ef'. +For convenience, perl sets C<$+> to the highest numbered C<$1>, C<$2>, +... that got assigned. Closely associated with the matching variables C<$1>, C<$2>, ... are -the I<backreferences> C<\g1>, C<\g2>,... Backreferences are simply +the B<backreferences> C<\1>, C<\2>, ... . Backreferences are simply matching variables that can be used I<inside> a regexp. This is a -really nice feature; what matches later in a regexp is made to depend on +really nice feature - what matches later in a regexp can depend on what matched earlier in the regexp. Suppose we wanted to look -for doubled words in a text, like 'the the'. The following regexp finds +for doubled words in text, like 'the the'. The following regexp finds all 3-letter doubles with a space in between: - /\b(\w\w\w)\s\g1\b/; + /(\w\w\w)\s\1/; -The grouping assigns a value to \g1, so that the same 3-letter sequence -is used for both parts. +The grouping assigns a value to \1, so that the same 3 letter sequence +is used for both parts. Here are some words with repeated parts: -A similar task is to find words consisting of two identical parts: - - % simple_grep '^(\w\w\w\w|\w\w\w|\w\w|\w)\g1$' /usr/dict/words + % simple_grep '^(\w\w\w\w|\w\w\w|\w\w|\w)\1$' /usr/dict/words beriberi booboo coco @@ -777,106 +736,14 @@ A similar task is to find words consisting of two identical parts: papa The regexp has a single grouping which considers 4-letter -combinations, then 3-letter combinations, etc., and uses C<\g1> to look for -a repeat. Although C<$1> and C<\g1> represent the same thing, care should be -taken to use matched variables C<$1>, C<$2>,... only I<outside> a regexp -and backreferences C<\g1>, C<\g2>,... only I<inside> a regexp; not doing -so may lead to surprising and unsatisfactory results. - - -=head2 Relative backreferences - -Counting the opening parentheses to get the correct number for a -backreference is error-prone as soon as there is more than one -capturing group. A more convenient technique became available -with Perl 5.10: relative backreferences. To refer to the immediately -preceding capture group one now may write C<\g{-1}>, the next but -last is available via C<\g{-2}>, and so on. - -Another good reason in addition to readability and maintainability -for using relative backreferences is illustrated by the following example, -where a simple pattern for matching peculiar strings is used: - - $a99a = '([a-z])(\d)\g2\g1'; # matches a11a, g22g, x33x, etc. - -Now that we have this pattern stored as a handy string, we might feel -tempted to use it as a part of some other pattern: - - $line = "code=e99e"; - if ($line =~ /^(\w+)=$a99a$/){ # unexpected behavior! - print "$1 is valid\n"; - } else { - print "bad line: '$line'\n"; - } - -But this doesn't match, at least not the way one might expect. Only -after inserting the interpolated C<$a99a> and looking at the resulting -full text of the regexp is it obvious that the backreferences have -backfired. The subexpression C<(\w+)> has snatched number 1 and -demoted the groups in C<$a99a> by one rank. This can be avoided by -using relative backreferences: - - $a99a = '([a-z])(\d)\g{-1}\g{-2}'; # safe for being interpolated - - -=head2 Named backreferences - -Perl 5.10 also introduced named capture groups and named backreferences. -To attach a name to a capturing group, you write either -C<< (?<name>...) >> or C<< (?'name'...) >>. The backreference may -then be written as C<\g{name}>. It is permissible to attach the -same name to more than one group, but then only the leftmost one of the -eponymous set can be referenced. Outside of the pattern a named -capture group is accessible through the C<%+> hash. - -Assuming that we have to match calendar dates which may be given in one -of the three formats yyyy-mm-dd, mm/dd/yyyy or dd.mm.yyyy, we can write -three suitable patterns where we use 'd', 'm' and 'y' respectively as the -names of the groups capturing the pertaining components of a date. The -matching operation combines the three patterns as alternatives: - - $fmt1 = '(?<y>\d\d\d\d)-(?<m>\d\d)-(?<d>\d\d)'; - $fmt2 = '(?<m>\d\d)/(?<d>\d\d)/(?<y>\d\d\d\d)'; - $fmt3 = '(?<d>\d\d)\.(?<m>\d\d)\.(?<y>\d\d\d\d)'; - for my $d qw( 2006-10-21 15.01.2007 10/31/2005 ){ - if ( $d =~ m{$fmt1|$fmt2|$fmt3} ){ - print "day=$+{d} month=$+{m} year=$+{y}\n"; - } - } - -If any of the alternatives matches, the hash C<%+> is bound to contain the -three key-value pairs. - - -=head2 Alternative capture group numbering - -Yet another capturing group numbering technique (also as from Perl 5.10) -deals with the problem of referring to groups within a set of alternatives. -Consider a pattern for matching a time of the day, civil or military style: - - if ( $time =~ /(\d\d|\d):(\d\d)|(\d\d)(\d\d)/ ){ - # process hour and minute - } - -Processing the results requires an additional if statement to determine -whether C<$1> and C<$2> or C<$3> and C<$4> contain the goodies. It would -be easier if we could use group numbers 1 and 2 in second alternative as -well, and this is exactly what the parenthesized construct C<(?|...)>, -set around an alternative achieves. Here is an extended version of the -previous pattern: - - if($time =~ /(?|(\d\d|\d):(\d\d)|(\d\d)(\d\d))\s+([A-Z][A-Z][A-Z])/){ - print "hour=$1 minute=$2 zone=$3\n"; - } - -Within the alternative numbering group, group numbers start at the same -position for each alternative. After the group, numbering continues -with one higher than the maximum reached across all the alternatives. - -=head2 Position information - -In addition to what was matched, Perl also provides the -positions of what was matched as contents of the C<@-> and C<@+> +combinations, then 3-letter combinations, etc. and uses C<\1> to look for +a repeat. Although C<$1> and C<\1> represent the same thing, care should be +taken to use matched variables C<$1>, C<$2>, ... only outside a regexp +and backreferences C<\1>, C<\2>, ... only inside a regexp; not doing +so may lead to surprising and/or undefined results. + +In addition to what was matched, Perl 5.6.0 also provides the +positions of what was matched with the C<@-> and C<@+> arrays. C<$-[0]> is the position of the start of the entire match and C<$+[0]> is the position of the end. Similarly, C<$-[n]> is the position of the start of the C<$n> match and C<$+[n]> is the position @@ -885,8 +752,8 @@ this code $x = "Mmm...donut, thought Homer"; $x =~ /^(Mmm|Yech)\.\.\.(donut|peas)/; # matches - foreach $exp (1..$#-) { - print "Match $exp: '${$exp}' at position ($-[$exp],$+[$exp])\n"; + foreach $expr (1..$#-) { + print "Match $expr: '${$expr}' at position ($-[$expr],$+[$expr])\n"; } prints @@ -895,7 +762,7 @@ prints Match 2: 'donut' at position (6,11) Even if there are no groupings in a regexp, it is still possible to -find out what exactly matched in a string. If you use them, Perl +find out what exactly matched in a string. If you use them, perl will set C<$`> to the part of the string before the match, will set C<$&> to the part of the string that matched, and will set C<$'> to the part of the string after the match. An example: @@ -904,72 +771,30 @@ of the string after the match. An example: $x =~ /cat/; # $` = 'the ', $& = 'cat', $' = ' caught the mouse' $x =~ /the/; # $` = '', $& = 'the', $' = ' cat caught the mouse' -In the second match, C<$`> equals C<''> because the regexp matched at the -first character position in the string and stopped; it never saw the -second 'the'. - -If your code is to run on Perl versions earlier than -5.20, it is worthwhile to note that using C<$`> and C<$'> -slows down regexp matching quite a bit, while C<$&> slows it down to a +In the second match, S<C<$` = ''> > because the regexp matched at the +first character position in the string and stopped, it never saw the +second 'the'. It is important to note that using C<$`> and C<$'> +slows down regexp matching quite a bit, and C< $& > slows it down to a lesser extent, because if they are used in one regexp in a program, -they are generated for I<all> regexps in the program. So if raw +they are generated for <all> regexps in the program. So if raw performance is a goal of your application, they should be avoided. -If you need to extract the corresponding substrings, use C<@-> and -C<@+> instead: +If you need them, use C<@-> and C<@+> instead: $` is the same as substr( $x, 0, $-[0] ) $& is the same as substr( $x, $-[0], $+[0]-$-[0] ) $' is the same as substr( $x, $+[0] ) -As of Perl 5.10, the C<${^PREMATCH}>, C<${^MATCH}> and C<${^POSTMATCH}> -variables may be used. These are only set if the C</p> modifier is -present. Consequently they do not penalize the rest of the program. In -Perl 5.20, C<${^PREMATCH}>, C<${^MATCH}> and C<${^POSTMATCH}> are available -whether the C</p> has been used or not (the modifier is ignored), and -C<$`>, C<$'> and C<$&> do not cause any speed difference. - -=head2 Non-capturing groupings - -A group that is required to bundle a set of alternatives may or may not be -useful as a capturing group. If it isn't, it just creates a superfluous -addition to the set of available capture group values, inside as well as -outside the regexp. Non-capturing groupings, denoted by C<(?:regexp)>, -still allow the regexp to be treated as a single unit, but don't establish -a capturing group at the same time. Both capturing and non-capturing -groupings are allowed to co-exist in the same regexp. Because there is -no extraction, non-capturing groupings are faster than capturing -groupings. Non-capturing groupings are also handy for choosing exactly -which parts of a regexp are to be extracted to matching variables: - - # match a number, $1-$4 are set, but we only want $1 - /([+-]?\ *(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?)/; - - # match a number faster , only $1 is set - /([+-]?\ *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)/; - - # match a number, get $1 = whole number, $2 = exponent - /([+-]?\ *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE]([+-]?\d+))?)/; - -Non-capturing groupings are also useful for removing nuisance -elements gathered from a split operation where parentheses are -required for some reason: - - $x = '12aba34ba5'; - @num = split /(a|b)+/, $x; # @num = ('12','a','34','a','5') - @num = split /(?:a|b)+/, $x; # @num = ('12','34','5') - - =head2 Matching repetitions The examples in the previous section display an annoying weakness. We -were only matching 3-letter words, or chunks of words of 4 letters or -less. We'd like to be able to match words or, more generally, strings -of any length, without writing out tedious alternatives like +were only matching 3-letter words, or syllables of 4 letters or +less. We'd like to be able to match words or syllables of any length, +without writing out tedious alternatives like C<\w\w\w\w|\w\w\w|\w\w|\w>. -This is exactly the problem the I<quantifier> metacharacters C<?>, -C<*>, C<+>, and C<{}> were created for. They allow us to delimit the -number of repeats for a portion of a regexp we consider to be a +This is exactly the problem the B<quantifier> metacharacters C<?>, +C<*>, C<+>, and C<{}> were created for. They allow us to determine the +number of repeats of a portion of a regexp we consider to be a match. Quantifiers are put immediately after the character, character class, or grouping that we want to specify. They have the following meanings: @@ -978,45 +803,44 @@ meanings: =item * -C<a?> means: match 'a' 1 or 0 times +C<a?> = match 'a' 1 or 0 times =item * -C<a*> means: match 'a' 0 or more times, i.e., any number of times +C<a*> = match 'a' 0 or more times, i.e., any number of times =item * -C<a+> means: match 'a' 1 or more times, i.e., at least once +C<a+> = match 'a' 1 or more times, i.e., at least once =item * -C<a{n,m}> means: match at least C<n> times, but not more than C<m> +C<a{n,m}> = match at least C<n> times, but not more than C<m> times. =item * -C<a{n,}> means: match at least C<n> or more times +C<a{n,}> = match at least C<n> or more times =item * -C<a{n}> means: match exactly C<n> times +C<a{n}> = match exactly C<n> times =back Here are some examples: - /[a-z]+\s+\d*/; # match a lowercase word, at least one space, and + /[a-z]+\s+\d*/; # match a lowercase word, at least some space, and # any number of digits - /(\w+)\s+\g1/; # match doubled words of arbitrary length + /(\w+)\s+\1/; # match doubled words of arbitrary length /y(es)?/i; # matches 'y', 'Y', or a case-insensitive 'yes' - $year =~ /^\d{2,4}$/; # make sure year is at least 2 but not more - # than 4 digits - $year =~ /^\d{4}$|^\d{2}$/; # better match; throw out 3-digit dates - $year =~ /^\d{2}(\d{2})?$/; # same thing written differently. - # However, this captures the last two - # digits in $1 and the other does not. - - % simple_grep '^(\w+)\g1$' /usr/dict/words # isn't this easier? + $year =~ /\d{2,4}/; # make sure year is at least 2 but not more + # than 4 digits + $year =~ /\d{4}|\d{2}/; # better match; throw out 3 digit dates + $year =~ /\d{2}(\d{2})?/; # same thing written differently. However, + # this produces $1 and the other does not. + + % simple_grep '^(\w+)\1$' /usr/dict/words # isn't this easier? beriberi booboo coco @@ -1024,10 +848,10 @@ Here are some examples: murmur papa -For all of these quantifiers, Perl will try to match as much of the +For all of these quantifiers, perl will try to match as much of the string as possible, while still allowing the regexp to succeed. Thus -with C</a?.../>, Perl will first try to match the regexp with the C<a> -present; if that fails, Perl will try to match the regexp without the +with C</a?.../>, perl will first try to match the regexp with the C<a> +present; if that fails, perl will try to match the regexp without the C<a> present. For the quantifier C<*>, we get the following: $x = "the cat in the hat"; @@ -1042,21 +866,21 @@ string and locks onto it. Consider, however, this regexp: $x =~ /^(.*)(at)(.*)$/; # matches, # $1 = 'the cat in the h' # $2 = 'at' - # $3 = '' (0 characters match) + # $3 = '' (0 matches) -One might initially guess that Perl would find the C<at> in C<cat> and +One might initially guess that perl would find the C<at> in C<cat> and stop there, but that wouldn't give the longest possible string to the first quantifier C<.*>. Instead, the first quantifier C<.*> grabs as much of the string as possible while still having the regexp match. In this example, that means having the C<at> sequence with the final C<at> -in the string. The other important principle illustrated here is that, +in the string. The other important principle illustrated here is that when there are two or more elements in a regexp, the I<leftmost> -quantifier, if there is one, gets to grab as much of the string as +quantifier, if there is one, gets to grab as much the string as possible, leaving the rest of the regexp to fight over scraps. Thus in our example, the first quantifier C<.*> grabs most of the string, while the second quantifier C<.*> gets the empty string. Quantifiers that -grab as much of the string as possible are called I<maximal match> or -I<greedy> quantifiers. +grab as much of the string as possible are called B<maximal match> or +B<greedy> quantifiers. When a regexp can match a string in several different ways, we can use the principles above to predict which way the regexp will match: @@ -1091,7 +915,7 @@ satisfied. =back -As we have seen above, Principle 0 overrides the others. The regexp +As we have seen above, Principle 0 overrides the others - the regexp will be matched as early as possible, with the other principles determining how the regexp matches at that earliest character position. @@ -1141,8 +965,8 @@ C<X+>, not C<X*>. Sometimes greed is not good. At times, we would like quantifiers to match a I<minimal> piece of string, rather than a maximal piece. For -this purpose, Larry Wall created the I<minimal match> or -I<non-greedy> quantifiers C<??>, C<*?>, C<+?>, and C<{}?>. These are +this purpose, Larry Wall created the S<B<minimal match> > or +B<non-greedy> quantifiers C<??>,C<*?>, C<+?>, and C<{}?>. These are the usual quantifiers with a C<?> appended to them. They have the following meanings: @@ -1150,31 +974,31 @@ following meanings: =item * -C<a??> means: match 'a' 0 or 1 times. Try 0 first, then 1. +C<a??> = match 'a' 0 or 1 times. Try 0 first, then 1. =item * -C<a*?> means: match 'a' 0 or more times, i.e., any number of times, +C<a*?> = match 'a' 0 or more times, i.e., any number of times, but as few times as possible =item * -C<a+?> means: match 'a' 1 or more times, i.e., at least once, but +C<a+?> = match 'a' 1 or more times, i.e., at least once, but as few times as possible =item * -C<a{n,m}?> means: match at least C<n> times, not more than C<m> +C<a{n,m}?> = match at least C<n> times, not more than C<m> times, as few times as possible =item * -C<a{n,}?> means: match at least C<n> times, but as few times as +C<a{n,}?> = match at least C<n> times, but as few times as possible =item * -C<a{n}?> means: match exactly C<n> times. Because we match exactly +C<a{n}?> = match exactly C<n> times. Because we match exactly C<n> times, C<a{n}?> is equivalent to C<a{n}> and is just there for notational consistency. @@ -1255,42 +1079,42 @@ backtracking. Here is a step-by-step analysis of the example =over 4 -=item Z<>0 +=item 0 Start with the first letter in the string 't'. -=item Z<>1 +=item 1 The first quantifier '.*' starts out by matching the whole string 'the cat in the hat'. -=item Z<>2 +=item 2 'a' in the regexp element 'at' doesn't match the end of the string. Backtrack one character. -=item Z<>3 +=item 3 'a' in the regexp element 'at' still doesn't match the last letter of the string 't', so backtrack one more character. -=item Z<>4 +=item 4 Now we can match the 'a' and the 't'. -=item Z<>5 +=item 5 Move on to the third element '.*'. Since we are at the end of the string and '.*' can match 0 times, assign it the empty string. -=item Z<>6 +=item 6 We are done! =back Most of the time, all this moving forward and backtracking happens -quickly and searching is fast. There are some pathological regexps, +quickly and searching is fast. There are some pathological regexps, however, whose execution time exponentially grows with the size of the string. A typical structure that blows up in your face is of the form @@ -1301,71 +1125,13 @@ different ways of partitioning a string of length n between the C<+> and C<*>: one repetition with C<b+> of length n, two repetitions with the first C<b+> length k and the second with length n-k, m repetitions whose bits add up to length n, etc. In fact there are an exponential -number of ways to partition a string as a function of its length. A +number of ways to partition a string as a function of length. A regexp may get lucky and match early in the process, but if there is -no match, Perl will try I<every> possibility before giving up. So be +no match, perl will try I<every> possibility before giving up. So be careful with nested C<*>'s, C<{n,m}>'s, and C<+>'s. The book -I<Mastering Regular Expressions> by Jeffrey Friedl gives a wonderful +I<Mastering regular expressions> by Jeffrey Friedl gives a wonderful discussion of this and other efficiency issues. - -=head2 Possessive quantifiers - -Backtracking during the relentless search for a match may be a waste -of time, particularly when the match is bound to fail. Consider -the simple pattern - - /^\w+\s+\w+$/; # a word, spaces, a word - -Whenever this is applied to a string which doesn't quite meet the -pattern's expectations such as S<C<"abc ">> or S<C<"abc def ">>, -the regex engine will backtrack, approximately once for each character -in the string. But we know that there is no way around taking I<all> -of the initial word characters to match the first repetition, that I<all> -spaces must be eaten by the middle part, and the same goes for the second -word. - -With the introduction of the I<possessive quantifiers> in Perl 5.10, we -have a way of instructing the regex engine not to backtrack, with the -usual quantifiers with a C<+> appended to them. This makes them greedy as -well as stingy; once they succeed they won't give anything back to permit -another solution. They have the following meanings: - -=over 4 - -=item * - -C<a{n,m}+> means: match at least C<n> times, not more than C<m> times, -as many times as possible, and don't give anything up. C<a?+> is short -for C<a{0,1}+> - -=item * - -C<a{n,}+> means: match at least C<n> times, but as many times as possible, -and don't give anything up. C<a*+> is short for C<a{0,}+> and C<a++> is -short for C<a{1,}+>. - -=item * - -C<a{n}+> means: match exactly C<n> times. It is just there for -notational consistency. - -=back - -These possessive quantifiers represent a special case of a more general -concept, the I<independent subexpression>, see below. - -As an example where a possessive quantifier is suitable we consider -matching a quoted string, as it appears in several programming languages. -The backslash is used as an escape character that indicates that the -next character is to be taken literally, as another character for the -string. Therefore, after the opening quote, we expect a (possibly -empty) sequence of alternatives: either some character except an -unescaped quote or backslash or an escaped character. - - /"(?:[^"\\]++|\\.)*+"/; - - =head2 Building a regexp At this point, we have all the basic regexp concepts covered, so let's @@ -1397,7 +1163,7 @@ see that if there is no exponent, floating point numbers must have a decimal point, otherwise they are integers. We might be tempted to model these with C<\d*\.\d*>, but this would also match just a single decimal point, which is not a number. So the three cases of floating -point number without exponent are +point number sans exponent are /[+-]?\d+\./; # 1., 321., etc. /[+-]?\.\d+/; # .1, .234, etc. @@ -1448,9 +1214,9 @@ we can rewrite our 'extended' regexp in the more pleasing form If whitespace is mostly irrelevant, how does one include space characters in an extended regexp? The answer is to backslash it -S<C<'\ '>> or put it in a character class S<C<[ ]>>. The same thing -goes for pound signs: use C<\#> or C<[#]>. For instance, Perl allows -a space between the sign and the mantissa or integer, and we could add +S<C<'\ '> > or put it in a character class S<C<[ ]> >. The same thing +goes for pound signs, use C<\#> or C<[#]>. For instance, Perl allows +a space between the sign and the mantissa/integer, and we could add this to our regexp as follows: /^ @@ -1512,7 +1278,7 @@ and optimizing the final combined regexp. These are also the typical steps involved in writing a computer program. This makes perfect sense, because regular expressions are -essentially programs written in a little computer language that specifies +essentially programs written a little computer language that specifies patterns. =head2 Using regular expressions in Perl @@ -1525,36 +1291,52 @@ C</regexp/> and arbitrary delimiter C<m!regexp!> forms. We have used the binding operator C<=~> and its negation C<!~> to test for string matches. Associated with the matching operator, we have discussed the single line C<//s>, multi-line C<//m>, case-insensitive C<//i> and -extended C<//x> modifiers. There are a few more things you might -want to know about matching operators. +extended C<//x> modifiers. + +There are a few more things you might want to know about matching +operators. First, we pointed out earlier that variables in regexps are +substituted before the regexp is evaluated: + + $pattern = 'Seuss'; + while (<>) { + print if /$pattern/; + } -=head3 Prohibiting substitution +This will print any lines containing the word C<Seuss>. It is not as +efficient as it could be, however, because perl has to re-evaluate +C<$pattern> each time through the loop. If C<$pattern> won't be +changing over the lifetime of the script, we can add the C<//o> +modifier, which directs perl to only perform variable substitutions +once: -If you change C<$pattern> after the first substitution happens, Perl + #!/usr/bin/perl + # Improved simple_grep + $regexp = shift; + while (<>) { + print if /$regexp/o; # a good deal faster + } + +If you change C<$pattern> after the first substitution happens, perl will ignore it. If you don't want any substitutions at all, use the special delimiter C<m''>: - @pattern = ('Seuss'); + $pattern = 'Seuss'; while (<>) { - print if m'@pattern'; # matches literal '@pattern', not 'Seuss' + print if m'$pattern'; # matches '$pattern', not 'Seuss' } -Similar to strings, C<m''> acts like apostrophes on a regexp; all other -C<m> delimiters act like quotes. If the regexp evaluates to the empty string, +C<m''> acts like single quotes on a regexp; all other C<m> delimiters +act like double quotes. If the regexp evaluates to the empty string, the regexp in the I<last successful match> is used instead. So we have "dog" =~ /d/; # 'd' matches "dogbert =~ //; # this matches the 'd' regexp used before - -=head3 Global matching - -The final two modifiers we will discuss here, -C<//g> and C<//c>, concern multiple matches. -The modifier C<//g> stands for global matching and allows the +The final two modifiers C<//g> and C<//c> concern multiple matches. +The modifier C<//g> stands for global matching and allows the the matching operator to match within a string as many times as possible. In scalar context, successive invocations against a string will have -C<//g> jump from match to match, keeping track of position in the +`C<//g> jump from match to match, keeping track of position in the string as it goes along. You can get or set the position with the C<pos()> function. @@ -1595,9 +1377,9 @@ there are no groupings, a list of matches to the whole regexp. So if we wanted just the words, we could use @words = ($x =~ /(\w+)/g); # matches, - # $words[0] = 'cat' - # $words[1] = 'dog' - # $words[2] = 'house' + # $word[0] = 'cat' + # $word[1] = 'dog' + # $word[2] = 'house' Closely associated with the C<//g> modifier is the C<\G> anchor. The C<\G> anchor matches at the point where the previous C<//g> match left @@ -1618,10 +1400,8 @@ off. C<\G> allows us to easily do context-sensitive matching: The combination of C<//g> and C<\G> allows us to process the string a bit at a time and use arbitrary Perl logic to decide what to do next. -Currently, the C<\G> anchor is only fully supported when used to anchor -to the start of the pattern. -C<\G> is also invaluable in processing fixed-length records with +C<\G> is also invaluable in processing fixed length records with regexps. Suppose we have a snippet of coding region DNA, encoded as base pair letters C<ATCGTTGAAT...> and we want to find all the stop codons C<TGA>. In a coding region, codons are 3-letter sequences, so @@ -1632,9 +1412,9 @@ naive regexp $dna = "ATCGTTGAATGCAAATGACATGAC"; $dna =~ /TGA/; -doesn't work; it may match a C<TGA>, but there is no guarantee that +doesn't work; it may match an C<TGA>, but there is no guarantee that the match is aligned with codon boundaries, e.g., the substring -S<C<GTT GAA>> gives a match. A better solution is +S<C<GTT GAA> > gives a match. A better solution is while ($dna =~ /(\w\w\w)*?TGA/g) { # note the minimal *? print "Got a TGA stop codon at position ", pos $dna, "\n"; @@ -1665,22 +1445,18 @@ which is the correct answer. This example illustrates that it is important not only to match what is desired, but to reject what is not desired. -(There are other regexp modifiers that are available, such as -C<//o>, but their specialized uses are beyond the -scope of this introduction. ) - -=head3 Search and replace +B<search and replace> -Regular expressions also play a big role in I<search and replace> +Regular expressions also play a big role in B<search and replace> operations in Perl. Search and replace is accomplished with the C<s///> operator. The general form is C<s/regexp/replacement/modifiers>, with everything we know about regexps and modifiers applying in this case as well. The -C<replacement> is a Perl double-quoted string that replaces in the +C<replacement> is a Perl double quoted string that replaces in the string whatever is matched with the C<regexp>. The operator C<=~> is also used here to associate a string with C<s///>. If matching -against C<$_>, the S<C<$_ =~>> can be dropped. If there is a match, -C<s///> returns the number of substitutions made; otherwise it returns +against C<$_>, the S<C<$_ =~> > can be dropped. If there is a match, +C<s///> returns the number of substitutions made, otherwise it returns false. Here are a few examples: $x = "Time to feed the cat!"; @@ -1694,7 +1470,7 @@ false. Here are a few examples: In the last example, the whole string was matched, but only the part inside the single quotes was grouped. With the C<s///> operator, the -matched variables C<$1>, C<$2>, etc. are immediately available for use +matched variables C<$1>, C<$2>, etc. are immediately available for use in the replacement expression, so we use C<$1> to replace the quoted string with just what was quoted. With the global modifier, C<s///g> will search and replace all occurrences of the regexp in the string: @@ -1714,7 +1490,7 @@ the following program to replace it: $regexp = shift; $replacement = shift; while (<>) { - s/$regexp/$replacement/g; + s/$regexp/$replacement/go; print; } ^D @@ -1722,48 +1498,19 @@ the following program to replace it: % simple_replace regexp regex perlretut.pod In C<simple_replace> we used the C<s///g> modifier to replace all -occurrences of the regexp on each line. (Even though the regular -expression appears in a loop, Perl is smart enough to compile it -only once.) As with C<simple_grep>, both the -C<print> and the C<s/$regexp/$replacement/g> use C<$_> implicitly. - -If you don't want C<s///> to change your original variable you can use -the non-destructive substitute modifier, C<s///r>. This changes the -behavior so that C<s///r> returns the final substituted string -(instead of the number of substitutions): - - $x = "I like dogs."; - $y = $x =~ s/dogs/cats/r; - print "$x $y\n"; - -That example will print "I like dogs. I like cats". Notice the original -C<$x> variable has not been affected. The overall -result of the substitution is instead stored in C<$y>. If the -substitution doesn't affect anything then the original string is -returned: - - $x = "I like dogs."; - $y = $x =~ s/elephants/cougars/r; - print "$x $y\n"; # prints "I like dogs. I like dogs." - -One other interesting thing that the C<s///r> flag allows is chaining -substitutions: - - $x = "Cats are great."; - print $x =~ s/Cats/Dogs/r =~ s/Dogs/Frogs/r =~ - s/Frogs/Hedgehogs/r, "\n"; - # prints "Hedgehogs are great." +occurrences of the regexp on each line and the C<s///o> modifier to +compile the regexp only once. As with C<simple_grep>, both the +C<print> and the C<s/$regexp/$replacement/go> use C<$_> implicitly. A modifier available specifically to search and replace is the -C<s///e> evaluation modifier. C<s///e> treats the -replacement text as Perl code, rather than a double-quoted -string. The value that the code returns is substituted for the +C<s///e> evaluation modifier. C<s///e> wraps an C<eval{...}> around +the replacement string and the evaluated result is substituted for the matched substring. C<s///e> is useful if you need to do a bit of computation in the process of replacing text. This example counts character frequencies in a line: $x = "Bill the cat"; - $x =~ s/(.)/$chars{$1}++;$1/eg; # final $1 replaces char with itself + $x =~ s/(.)/$chars{$1}++;$1/eg; # final $1 replaces char with itself print "frequency of '$_' is $chars{$_}\n" foreach (sort {$chars{$b} <=> $chars{$a}} keys %chars); @@ -1781,20 +1528,20 @@ This prints As with the match C<m//> operator, C<s///> can use other delimiters, such as C<s!!!> and C<s{}{}>, and even C<s{}//>. If single quotes are -used C<s'''>, then the regexp and replacement are -treated as single-quoted strings and there are no -variable substitutions. C<s///> in list context +used C<s'''>, then the regexp and replacement are treated as single +quoted strings and there are no substitutions. C<s///> in list context returns the same thing as in scalar context, i.e., the number of matches. -=head3 The split function +B<The split operator> -The C<split()> function is another place where a regexp is used. -C<split /regexp/, string, limit> separates the C<string> operand into -a list of substrings and returns that list. The regexp must be designed -to match whatever constitutes the separators for the desired substrings. -The C<limit>, if present, constrains splitting into no more than C<limit> -number of strings. For example, to split a string into words, use +The B<C<split> > function can also optionally use a matching operator +C<m//> to split a string. C<split /regexp/, string, limit> splits +C<string> into a list of substrings and returns that list. The regexp +is used to match the character sequence that the C<string> is split +with respect to. The C<limit>, if present, constrains splitting into +no more than C<limit> number of strings. For example, to split a +string into words, use $x = "Calvin and Hobbes"; @words = split /\s+/, $x; # $word[0] = 'Calvin' @@ -1803,7 +1550,7 @@ number of strings. For example, to split a string into words, use If the empty regexp C<//> is used, the regexp always matches and the string is split into individual characters. If the regexp has -groupings, then the resulting list contains the matched substrings from the +groupings, then list produced contains the matched substrings from the groupings as well. For instance, $x = "/usr/bin/perl"; @@ -1825,7 +1572,7 @@ an empty initial element to the list. If you have read this far, congratulations! You now have all the basic tools needed to use regular expressions to solve a wide range of text processing problems. If this is your first time through the tutorial, -why not stop here and play around with regexps a while.... S<Part 2> +why not stop here and play around with regexps a while... S<Part 2> concerns the more esoteric aspects of regular expressions and those concepts certainly aren't needed right at the start. @@ -1835,12 +1582,12 @@ OK, you know the basics of regexps and you want to know more. If matching regular expressions is analogous to a walk in the woods, then the tools discussed in Part 1 are analogous to topo maps and a compass, basic tools we use all the time. Most of the tools in part 2 -are analogous to flare guns and satellite phones. They aren't used +are are analogous to flare guns and satellite phones. They aren't used too often on a hike, but when we are stuck, they can be invaluable. What follows are the more advanced, less used, or sometimes esoteric -capabilities of Perl regexps. In Part 2, we will assume you are -comfortable with the basics and concentrate on the advanced features. +capabilities of perl regexps. In Part 2, we will assume you are +comfortable with the basics and concentrate on the new features. =head2 More on characters, strings, and character classes @@ -1848,17 +1595,16 @@ There are a number of escape sequences and character classes that we haven't covered yet. There are several escape sequences that convert characters or strings -between upper and lower case, and they are also available within -patterns. C<\l> and C<\u> convert the next character to lower or -upper case, respectively: +between upper and lower case. C<\l> and C<\u> convert the next +character to lower or upper case, respectively: $x = "perl"; $string =~ /\u$x/; # matches 'Perl' in $string $x = "M(rs?|s)\\."; # note the double backslash $string =~ /\l$x/; # matches 'mr.', 'mrs.', and 'ms.', -A C<\L> or C<\U> indicates a lasting conversion of case, until -terminated by C<\E> or thrown over by another C<\U> or C<\L>: +C<\L> and C<\U> converts a whole substring, delimited by C<\L> or +C<\U> and C<\E>, to lower or upper case: $x = "This word is in lower case:\L SHOUT\E"; $x =~ /shout/; # matches @@ -1881,149 +1627,157 @@ instance, It does not protect C<$> or C<@>, so that variables can still be substituted. -C<\Q>, C<\L>, C<\l>, C<\U>, C<\u> and C<\E> are actually part of -double-quotish syntax, and not part of regexp syntax proper. They will -work if they appear in a regular expression embedded directly in a -program, but not when contained in a string that is interpolated in a -pattern. - -Perl regexps can handle more than just the -standard ASCII character set. Perl supports I<Unicode>, a standard -for representing the alphabets from virtually all of the world's written -languages, and a host of symbols. Perl's text strings are Unicode strings, so -they can contain characters with a value (codepoint or character number) higher -than 255. +With the advent of 5.6.0, perl regexps can handle more than just the +standard ASCII character set. Perl now supports B<Unicode>, a standard +for encoding the character sets from many of the world's written +languages. Unicode does this by allowing characters to be more than +one byte wide. Perl uses the UTF-8 encoding, in which ASCII characters +are still encoded as one byte, but characters greater than C<chr(127)> +may be stored as two or more bytes. What does this mean for regexps? Well, regexp users don't need to know -much about Perl's internal representation of strings. But they do need -to know 1) how to represent Unicode characters in a regexp and 2) that -a matching operation will treat the string to be searched as a sequence -of characters, not bytes. The answer to 1) is that Unicode characters -greater than C<chr(255)> are represented using the C<\x{hex}> notation, because -\x hex (without curly braces) doesn't go further than 255. (Starting in Perl -5.14, if you're an octal fan, you can also use C<\o{oct}>.) - +much about perl's internal representation of strings. But they do need +to know 1) how to represent Unicode characters in a regexp and 2) when +a matching operation will treat the string to be searched as a +sequence of bytes (the old way) or as a sequence of Unicode characters +(the new way). The answer to 1) is that Unicode characters greater +than C<chr(127)> may be represented using the C<\x{hex}> notation, +with C<hex> a hexadecimal integer: + + use utf8; # We will be doing Unicode processing /\x{263a}/; # match a Unicode smiley face :) -B<NOTE>: In Perl 5.6.0 it used to be that one needed to say C<use -utf8> to use any Unicode features. This is no more the case: for -almost all Unicode processing, the explicit C<utf8> pragma is not -needed. (The only case where it matters is if your Perl script is in -Unicode and encoded in UTF-8, then an explicit C<use utf8> is needed.) +Unicode characters in the range of 128-255 use two hexadecimal digits +with braces: C<\x{ab}>. Note that this is different than C<\xab>, +which is just a hexadecimal byte with no Unicode +significance. Figuring out the hexadecimal sequence of a Unicode character you want or deciphering someone else's hexadecimal Unicode regexp is about as much fun as programming in machine code. So another way to specify -Unicode characters is to use the I<named character> escape -sequence C<\N{I<name>}>. I<name> is a name for the Unicode character, as +Unicode characters is to use the S<B<named character> > escape +sequence C<\N{name}>. C<name> is a name for the Unicode character, as specified in the Unicode standard. For instance, if we wanted to represent or match the astrological sign for the planet Mercury, we could use + use utf8; # We will be doing Unicode processing + use charnames ":full"; # use named chars with Unicode full names $x = "abc\N{MERCURY}def"; $x =~ /\N{MERCURY}/; # matches -One can also use "short" names: +One can also use short names or restrict names to a certain alphabet: + + use utf8; # We will be doing Unicode processing + use charnames ':full'; print "\N{GREEK SMALL LETTER SIGMA} is called sigma.\n"; - print "\N{greek:Sigma} is an upper-case sigma.\n"; -You can also restrict names to a certain alphabet by specifying the -L<charnames> pragma: + use charnames ":short"; + print "\N{greek:Sigma} is an upper-case sigma.\n"; use charnames qw(greek); print "\N{sigma} is Greek sigma\n"; -An index of character names is available on-line from the Unicode -Consortium, L<http://www.unicode.org/charts/charindex.html>; explanatory -material with links to other resources at -L<http://www.unicode.org/standard/where>. - -The answer to requirement 2) is that a regexp (mostly) -uses Unicode characters. The "mostly" is for messy backward -compatibility reasons, but starting in Perl 5.14, any regex compiled in -the scope of a C<use feature 'unicode_strings'> (which is automatically -turned on within the scope of a C<use 5.012> or higher) will turn that -"mostly" into "always". If you want to handle Unicode properly, you -should ensure that C<'unicode_strings'> is turned on. -Internally, this is encoded to bytes using either UTF-8 or a native 8 -bit encoding, depending on the history of the string, but conceptually -it is a sequence of characters, not bytes. See L<perlunitut> for a -tutorial about that. - -Let us now discuss Unicode character classes, most usually called -"character properties". These are represented by the -C<\p{name}> escape sequence. Closely associated is the C<\P{name}> -property, which is the negation of the C<\p{name}> one. For -example, to match lower and uppercase characters, - +A list of full names is found in the file Names.txt in the +lib/perl5/5.6.0/unicode directory. + +The answer to requirement 2), as of 5.6.0, is that if a regexp +contains Unicode characters, the string is searched as a sequence of +Unicode characters. Otherwise, the string is searched as a sequence of +bytes. If the string is being searched as a sequence of Unicode +characters, but matching a single byte is required, we can use the C<\C> +escape sequence. C<\C> is a character class akin to C<.> except that +it matches I<any> byte 0-255. So + + use utf8; # We will be doing Unicode processing + use charnames ":full"; # use named chars with Unicode full names + $x = "a"; + $x =~ /\C/; # matches 'a', eats one byte + $x = ""; + $x =~ /\C/; # doesn't match, no bytes to match + $x = "\N{MERCURY}"; # two-byte Unicode character + $x =~ /\C/; # matches, but dangerous! + +The last regexp matches, but is dangerous because the string +I<character> position is no longer synchronized to the string I<byte> +position. This generates the warning 'Malformed UTF-8 +character'. C<\C> is best used for matching the binary data in strings +with binary data intermixed with Unicode characters. + +Let us now discuss the rest of the character classes. Just as with +Unicode characters, there are named Unicode character classes +represented by the C<\p{name}> escape sequence. Closely associated is +the C<\P{name}> character class, which is the negation of the +C<\p{name}> class. For example, to match lower and uppercase +characters, + + use utf8; # We will be doing Unicode processing + use charnames ":full"; # use named chars with Unicode full names $x = "BOB"; $x =~ /^\p{IsUpper}/; # matches, uppercase char class $x =~ /^\P{IsUpper}/; # doesn't match, char class sans uppercase $x =~ /^\p{IsLower}/; # doesn't match, lowercase char class $x =~ /^\P{IsLower}/; # matches, char class sans lowercase -(The "Is" is optional.) - -There are many, many Unicode character properties. For the full list -see L<perluniprops>. Most of them have synonyms with shorter names, -also listed there. Some synonyms are a single character. For these, -you can drop the braces. For instance, C<\pM> is the same thing as -C<\p{Mark}>, meaning things like accent marks. - -The Unicode C<\p{Script}> property is used to categorize every Unicode -character into the language script it is written in. For example, -English, French, and a bunch of other European languages are written in -the Latin script. But there is also the Greek script, the Thai script, -the Katakana script, etc. You can test whether a character is in a -particular script with, for example C<\p{Latin}>, C<\p{Greek}>, -or C<\p{Katakana}>. To test if it isn't in the Balinese script, you -would use C<\P{Balinese}>. - -What we have described so far is the single form of the C<\p{...}> character -classes. There is also a compound form which you may run into. These -look like C<\p{name=value}> or C<\p{name:value}> (the equals sign and colon -can be used interchangeably). These are more general than the single form, -and in fact most of the single forms are just Perl-defined shortcuts for common -compound forms. For example, the script examples in the previous paragraph -could be written equivalently as C<\p{Script=Latin}>, C<\p{Script:Greek}>, -C<\p{script=katakana}>, and C<\P{script=balinese}> (case is irrelevant -between the C<{}> braces). You may -never have to use the compound forms, but sometimes it is necessary, and their -use can make your code easier to understand. - -C<\X> is an abbreviation for a character class that comprises -a Unicode I<extended grapheme cluster>. This represents a "logical character": -what appears to be a single character, but may be represented internally by more -than one. As an example, using the Unicode full names, e.g., S<C<A + COMBINING -RING>> is a grapheme cluster with base character C<A> and combining character -S<C<COMBINING RING>>, which translates in Danish to A with the circle atop it, -as in the word E<Aring>ngstrom. - -For the full and latest information about Unicode see the latest -Unicode standard, or the Unicode Consortium's website L<http://www.unicode.org> - -As if all those classes weren't enough, Perl also defines POSIX-style +Here is the association between some Perl named classes and the +traditional Unicode classes: + + Perl class name Unicode class name or regular expression + + IsAlpha /^[LM]/ + IsAlnum /^[LMN]/ + IsASCII $code <= 127 + IsCntrl /^C/ + IsBlank $code =~ /^(0020|0009)$/ || /^Z[^lp]/ + IsDigit Nd + IsGraph /^([LMNPS]|Co)/ + IsLower Ll + IsPrint /^([LMNPS]|Co|Zs)/ + IsPunct /^P/ + IsSpace /^Z/ || ($code =~ /^(0009|000A|000B|000C|000D)$/ + IsSpacePerl /^Z/ || ($code =~ /^(0009|000A|000C|000D)$/ + IsUpper /^L[ut]/ + IsWord /^[LMN]/ || $code eq "005F" + IsXDigit $code =~ /^00(3[0-9]|[46][1-6])$/ + +You can also use the official Unicode class names with the C<\p> and +C<\P>, like C<\p{L}> for Unicode 'letters', or C<\p{Lu}> for uppercase +letters, or C<\P{Nd}> for non-digits. If a C<name> is just one +letter, the braces can be dropped. For instance, C<\pM> is the +character class of Unicode 'marks'. + +C<\X> is an abbreviation for a character class sequence that includes +the Unicode 'combining character sequences'. A 'combining character +sequence' is a base character followed by any number of combining +characters. An example of a combining character is an accent. Using +the Unicode full names, e.g., S<C<A + COMBINING RING> > is a combining +character sequence with base character C<A> and combining character +S<C<COMBINING RING> >, which translates in Danish to A with the circle +atop it, as in the word Angstrom. C<\X> is equivalent to C<\PM\pM*}>, +i.e., a non-mark followed by one or more marks. + +As if all those classes weren't enough, Perl also defines POSIX style character classes. These have the form C<[:name:]>, with C<name> the name of the POSIX class. The POSIX classes are C<alpha>, C<alnum>, C<ascii>, C<cntrl>, C<digit>, C<graph>, C<lower>, C<print>, C<punct>, C<space>, C<upper>, and C<xdigit>, and two extensions, C<word> (a Perl -extension to match C<\w>), and C<blank> (a GNU extension). The C<//a> -modifier restricts these to matching just in the ASCII range; otherwise -they can match the same as their corresponding Perl Unicode classes: -C<[:upper:]> is the same as C<\p{IsUpper}>, etc. (There are some -exceptions and gotchas with this; see L<perlrecharclass> for a full -discussion.) The C<[:digit:]>, C<[:word:]>, and +extension to match C<\w>), and C<blank> (a GNU extension). If C<utf8> +is being used, then these classes are defined the same as their +corresponding perl Unicode classes: C<[:upper:]> is the same as +C<\p{IsUpper}>, etc. The POSIX character classes, however, don't +require using C<utf8>. The C<[:digit:]>, C<[:word:]>, and C<[:space:]> correspond to the familiar C<\d>, C<\w>, and C<\s> character classes. To negate a POSIX class, put a C<^> in front of -the name, so that, e.g., C<[:^digit:]> corresponds to C<\D> and, under -Unicode, C<\P{IsDigit}>. The Unicode and POSIX character classes can -be used just like C<\d>, with the exception that POSIX character -classes can only be used inside of a character class: +the name, so that, e.g., C<[:^digit:]> corresponds to C<\D> and under +C<utf8>, C<\P{IsDigit}>. The Unicode and POSIX character classes can +be used just like C<\d>, both inside and outside of character classes: /\s+[abc[:digit:]xyz]\s*/; # match a,b,c,x,y,z, or a digit - /^=item\s[[:digit:]]/; # match '=item', + /^=item\s[:digit:]/; # match '=item', # followed by a space and a digit + use utf8; + use charnames ":full"; /\s+[abc\p{IsDigit}xyz]\s+/; # match a,b,c,x,y,z, or a digit /^=item\s\p{IsDigit}/; # match '=item', # followed by a space and a digit @@ -2032,8 +1786,8 @@ Whew! That is all the rest of the characters and character classes. =head2 Compiling and saving regular expressions -In Part 1 we mentioned that Perl compiles a regexp into a compact -sequence of opcodes. Thus, a compiled regexp is a data structure +In Part 1 we discussed the C<//o> modifier, which compiles a regexp +just once. This suggests that a compiled regexp is some data structure that can be stored once and used again and again. The regexp quote C<qr//> does exactly that: C<qr/string/> compiles the C<string> as a regexp and transforms the result into a form that can be assigned to a @@ -2052,118 +1806,49 @@ C<$reg> can also be interpolated into a larger regexp: $x =~ /(abc)?$reg/; # still matches As with the matching operator, the regexp quote can use different -delimiters, e.g., C<qr!!>, C<qr{}> or C<qr~~>. Apostrophes -as delimiters (C<qr''>) inhibit any interpolation. +delimiters, e.g., C<qr!!>, C<qr{}> and C<qr~~>. The single quote +delimiters C<qr''> prevent any interpolation from taking place. Pre-compiled regexps are useful for creating dynamic matches that don't need to be recompiled each time they are encountered. Using -pre-compiled regexps, we write a C<grep_step> program which greps -for a sequence of patterns, advancing to the next pattern as soon -as one has been satisfied. +pre-compiled regexps, C<simple_grep> program can be expanded into a +program that matches multiple patterns: - % cat > grep_step + % cat > multi_grep #!/usr/bin/perl - # grep_step - match <number> regexps, one after the other + # multi_grep - match any of <number> regexps # usage: multi_grep <number> regexp1 regexp2 ... file1 file2 ... $number = shift; $regexp[$_] = shift foreach (0..$number-1); @compiled = map qr/$_/, @regexp; while ($line = <>) { - if ($line =~ /$compiled[0]/) { - print $line; - shift @compiled; - last unless @compiled; + foreach $pattern (@compiled) { + if ($line =~ /$pattern/) { + print $line; + last; # we matched, so move onto the next line + } } } ^D - % grep_step 3 shift print last grep_step - $number = shift; - print $line; - last unless @compiled; + % multi_grep 2 last for multi_grep + $regexp[$_] = shift foreach (0..$number-1); + foreach $pattern (@compiled) { + last; Storing pre-compiled regexps in an array C<@compiled> allows us to simply loop through the regexps without any recompilation, thus gaining flexibility without sacrificing speed. - -=head2 Composing regular expressions at runtime - -Backtracking is more efficient than repeated tries with different regular -expressions. If there are several regular expressions and a match with -any of them is acceptable, then it is possible to combine them into a set -of alternatives. If the individual expressions are input data, this -can be done by programming a join operation. We'll exploit this idea in -an improved version of the C<simple_grep> program: a program that matches -multiple patterns: - - % cat > multi_grep - #!/usr/bin/perl - # multi_grep - match any of <number> regexps - # usage: multi_grep <number> regexp1 regexp2 ... file1 file2 ... - - $number = shift; - $regexp[$_] = shift foreach (0..$number-1); - $pattern = join '|', @regexp; - - while ($line = <>) { - print $line if $line =~ /$pattern/; - } - ^D - - % multi_grep 2 shift for multi_grep - $number = shift; - $regexp[$_] = shift foreach (0..$number-1); - -Sometimes it is advantageous to construct a pattern from the I<input> -that is to be analyzed and use the permissible values on the left -hand side of the matching operations. As an example for this somewhat -paradoxical situation, let's assume that our input contains a command -verb which should match one out of a set of available command verbs, -with the additional twist that commands may be abbreviated as long as -the given string is unique. The program below demonstrates the basic -algorithm. - - % cat > keymatch - #!/usr/bin/perl - $kwds = 'copy compare list print'; - while( $cmd = <> ){ - $cmd =~ s/^\s+|\s+$//g; # trim leading and trailing spaces - if( ( @matches = $kwds =~ /\b$cmd\w*/g ) == 1 ){ - print "command: '@matches'\n"; - } elsif( @matches == 0 ){ - print "no such command: '$cmd'\n"; - } else { - print "not unique: '$cmd' (could be one of: @matches)\n"; - } - } - ^D - - % keymatch - li - command: 'list' - co - not unique: 'co' (could be one of: copy compare) - printer - no such command: 'printer' - -Rather than trying to match the input against the keywords, we match the -combined set of keywords against the input. The pattern matching -operation S<C<$kwds =~ /\b($cmd\w*)/g>> does several things at the -same time. It makes sure that the given command begins where a keyword -begins (C<\b>). It tolerates abbreviations due to the added C<\w*>. It -tells us the number of matches (C<scalar @matches>) and all the keywords -that were actually matched. You could hardly ask for more. - =head2 Embedding comments and modifiers in a regular expression Starting with this section, we will be discussing Perl's set of -I<extended patterns>. These are extensions to the traditional regular +B<extended patterns>. These are extensions to the traditional regular expression syntax that provide powerful new tools for pattern matching. We have already seen extensions in the form of the minimal -matching constructs C<??>, C<*?>, C<+?>, C<{n,m}?>, and C<{n,}?>. Most -of the extensions below have the form C<(?char...)>, where the +matching constructs C<??>, C<*?>, C<+?>, C<{n,m}?>, and C<{n,}?>. The +rest of the extensions below have the form C<(?char...)>, where the C<char> is a character that determines the type of extension. The first extension is an embedded comment C<(?#text)>. This embeds a @@ -2176,8 +1861,7 @@ example is This style of commenting has been largely superseded by the raw, freeform commenting that is allowed with the C<//x> modifier. -Most modifiers, such as C<//i>, C<//m>, C<//s> and C<//x> (or any -combination thereof) can also be embedded in +The modifiers C<//i>, C<//m>, C<//s>, and C<//x> can also embedded in a regexp using C<(?i)>, C<(?m)>, C<(?s)>, and C<(?x)>. For instance, /(?i)yes/; # match 'yes' case insensitively @@ -2202,8 +1886,7 @@ that must have different modifiers: } } -The second advantage is that embedded modifiers (except C<//p>, which -modifies the entire regexp) only affect the regexp +The second advantage is that embedded modifiers only affect the regexp inside the group the embedded modifier is contained in. So grouping can be used to localize the modifier's effects: @@ -2214,11 +1897,39 @@ by using, e.g., C<(?-i)>. Modifiers can also be combined into a single expression, e.g., C<(?s-i)> turns on single line mode and turns off case insensitivity. -Embedded modifiers may also be added to a non-capturing grouping. +=head2 Non-capturing groupings + +We noted in Part 1 that groupings C<()> had two distinct functions: 1) +group regexp elements together as a single unit, and 2) extract, or +capture, substrings that matched the regexp in the +grouping. Non-capturing groupings, denoted by C<(?:regexp)>, allow the +regexp to be treated as a single unit, but don't extract substrings or +set matching variables C<$1>, etc. Both capturing and non-capturing +groupings are allowed to co-exist in the same regexp. Because there is +no extraction, non-capturing groupings are faster than capturing +groupings. Non-capturing groupings are also handy for choosing exactly +which parts of a regexp are to be extracted to matching variables: + + # match a number, $1-$4 are set, but we only want $1 + /([+-]?\ *(\d+(\.\d*)?|\.\d+)([eE][+-]?\d+)?)/; + + # match a number faster , only $1 is set + /([+-]?\ *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE][+-]?\d+)?)/; + + # match a number, get $1 = whole number, $2 = exponent + /([+-]?\ *(?:\d+(?:\.\d*)?|\.\d+)(?:[eE]([+-]?\d+))?)/; + +Non-capturing groupings are also useful for removing nuisance +elements gathered from a split operation: + + $x = '12a34b5'; + @num = split /(a|b)/, $x; # @num = ('12','a','34','b','5') + @num = split /(?:a|b)/, $x; # @num = ('12','34','5') + +Non-capturing groupings may also have embedded modifiers: C<(?i-m:regexp)> is a non-capturing grouping that matches C<regexp> case insensitively and turns off multi-line mode. - =head2 Looking ahead and looking behind This section concerns the lookahead and lookbehind assertions. First, @@ -2227,15 +1938,15 @@ a little background. In Perl regular expressions, most regexp elements 'eat up' a certain amount of string when they match. For instance, the regexp element C<[abc}]> eats up one character of the string when it matches, in the -sense that Perl moves to the next character position in the string +sense that perl moves to the next character position in the string after the match. There are some elements, however, that don't eat up characters (advance the character position) if they match. The examples we have seen so far are the anchors. The anchor C<^> matches the beginning of the line, but doesn't eat any characters. Similarly, the -word boundary anchor C<\b> matches wherever a character matching C<\w> -is next to a character that doesn't, but it doesn't eat up any -characters itself. Anchors are examples of I<zero-width assertions>: -zero-width, because they consume +word boundary anchor C<\b> matches, e.g., if the character to the left +is a word character and the character to the right is a non-word +character, but it doesn't eat up any characters itself. Anchors are +examples of 'zero-width assertions'. Zero-width, because they consume no characters, and assertions, because they test some property of the string. In the context of our walk in the woods analogy to regexp matching, most regexp elements move us along a trail, but anchors have @@ -2247,7 +1958,7 @@ Checking the environment entails either looking ahead on the trail, looking behind, or both. C<^> looks behind, to see that there are no characters before. C<$> looks ahead, to see that there are no characters after. C<\b> looks both ahead and behind, to see if the -characters on either side differ in their "word-ness". +characters on either side differ in their 'word'-ness. The lookahead and lookbehind assertions are generalizations of the anchor concept. Lookahead and lookbehind are zero-width assertions @@ -2256,7 +1967,7 @@ lookahead assertion is denoted by C<(?=regexp)> and the lookbehind assertion is denoted by C<< (?<=fixed-regexp) >>. Some examples are $x = "I catch the housecat 'Tom-cat' with catnip"; - $x =~ /cat(?=\s)/; # matches 'cat' in 'housecat' + $x =~ /cat(?=\s+)/; # matches 'cat' in 'housecat' @catwords = ($x =~ /(?<=\s)cat\w+/g); # matches, # $catwords[0] = 'catch' # $catwords[1] = 'catnip' @@ -2280,26 +1991,13 @@ They evaluate true if the regexps do I<not> match: $x =~ /foo(?!baz)/; # matches, 'baz' doesn't follow 'foo' $x =~ /(?<!\s)foo/; # matches, there is no \s before 'foo' -The C<\C> is unsupported in lookbehind, because the already -treacherous definition of C<\C> would become even more so -when going backwards. - -Here is an example where a string containing blank-separated words, -numbers and single dashes is to be split into its components. -Using C</\s+/> alone won't work, because spaces are not required between -dashes, or a word or a dash. Additional places for a split are established -by looking ahead and behind: - - $str = "one two - --6-8"; - @toks = split / \s+ # a run of spaces - | (?<=\S) (?=-) # any non-space followed by '-' - | (?<=-) (?=\S) # a '-' followed by any non-space - /x, $str; # @toks = qw(one two - - - 6 - 8) - - =head2 Using independent subexpressions to prevent backtracking -I<Independent subexpressions> are regular expressions, in the +The last few extended patterns in this tutorial are experimental as of +5.6.0. Play with them, use them in some code, but don't rely on them +just yet for production code. + +S<B<Independent subexpressions> > are regular expressions, in the context of a larger regular expression, that function independently of the larger regular expression. That is, they consume as much or as little of the string as they wish without regard for the ability of @@ -2323,7 +2021,7 @@ Contrast that with an independent subexpression: The independent subexpression C<< (?>a*) >> doesn't care about the rest of the regexp, so it sees an C<a> and grabs it. Then the rest of the regexp C<ab> cannot match. Because C<< (?>a*) >> is independent, there -is no backtracking and the independent subexpression does not give +is no backtracking and and the independent subexpression does not give up its C<a>. Thus the match of the regexp as a whole fails. A similar behavior occurs with completely independent regexps: @@ -2351,7 +2049,7 @@ the first alternative C<[^()]+> matching a substring with no parentheses and the second alternative C<\([^()]*\)> matching a substring delimited by parentheses. The problem with this regexp is that it is pathological: it has nested indeterminate quantifiers -of the form C<(a+|b)+>. We discussed in Part 1 how nested quantifiers + of the form C<(a+|b)+>. We discussed in Part 1 how nested quantifiers like this could take an exponentially long time to execute if there was no match possible. To prevent the exponential blowup, we need to prevent useless backtracking at some point. This can be done by @@ -2363,38 +2061,33 @@ Here, C<< (?>[^()]+) >> breaks the degeneracy of string partitioning by gobbling up as much of the string as possible and keeping it. Then match failures fail much more quickly. - =head2 Conditional expressions -A I<conditional expression> is a form of if-then-else statement +A S<B<conditional expression> > is a form of if-then-else statement that allows one to choose which patterns are to be matched, based on some condition. There are two types of conditional expression: C<(?(condition)yes-regexp)> and C<(?(condition)yes-regexp|no-regexp)>. C<(?(condition)yes-regexp)> is -like an S<C<'if () {}'>> statement in Perl. If the C<condition> is true, +like an S<C<'if () {}'> > statement in Perl. If the C<condition> is true, the C<yes-regexp> will be matched. If the C<condition> is false, the -C<yes-regexp> will be skipped and Perl will move onto the next regexp -element. The second form is like an S<C<'if () {} else {}'>> statement +C<yes-regexp> will be skipped and perl will move onto the next regexp +element. The second form is like an S<C<'if () {} else {}'> > statement in Perl. If the C<condition> is true, the C<yes-regexp> will be matched, otherwise the C<no-regexp> will be matched. -The C<condition> can have several forms. The first form is simply an +The C<condition> can have two forms. The first form is simply an integer in parentheses C<(integer)>. It is true if the corresponding -backreference C<\integer> matched earlier in the regexp. The same -thing can be done with a name associated with a capture group, written -as C<< (<name>) >> or C<< ('name') >>. The second form is a bare -zero-width assertion C<(?...)>, either a lookahead, a lookbehind, or a -code assertion (discussed in the next section). The third set of forms -provides tests that return true if the expression is executed within -a recursion (C<(R)>) or is being called from some capturing group, -referenced either by number (C<(R1)>, C<(R2)>,...) or by name -(C<(R&name)>). - -The integer or name form of the C<condition> allows us to choose, -with more flexibility, what to match based on what matched earlier in the -regexp. This searches for words of the form C<"$x$x"> or C<"$x$y$y$x">: - - % simple_grep '^(\w+)(\w+)?(?(2)\g2\g1|\g1)$' /usr/dict/words +backreference C<\integer> matched earlier in the regexp. The second +form is a bare zero width assertion C<(?...)>, either a +lookahead, a lookbehind, or a code assertion (discussed in the next +section). + +The integer form of the C<condition> allows us to choose, with more +flexibility, what to match based on what matched earlier in the +regexp. This searches for words of the form C<"$x$x"> or +C<"$x$y$y$x">: + + % simple_grep '^(\w+)(\w+)?(?(2)\2\1|\1)$' /usr/dict/words beriberi coco couscous @@ -2416,80 +2109,14 @@ C<< (?(?<=AA)G|C) >> and not C<< (?((?<=AA))G|C) >>; for the lookahead, lookbehind or code assertions, the parentheses around the conditional are not needed. - -=head2 Defining named patterns - -Some regular expressions use identical subpatterns in several places. -Starting with Perl 5.10, it is possible to define named subpatterns in -a section of the pattern so that they can be called up by name -anywhere in the pattern. This syntactic pattern for this definition -group is C<< (?(DEFINE)(?<name>pattern)...) >>. An insertion -of a named pattern is written as C<(?&name)>. - -The example below illustrates this feature using the pattern for -floating point numbers that was presented earlier on. The three -subpatterns that are used more than once are the optional sign, the -digit sequence for an integer and the decimal fraction. The DEFINE -group at the end of the pattern contains their definition. Notice -that the decimal fraction pattern is the first place where we can -reuse the integer pattern. - - /^ (?&osg)\ * ( (?&int)(?&dec)? | (?&dec) ) - (?: [eE](?&osg)(?&int) )? - $ - (?(DEFINE) - (?<osg>[-+]?) # optional sign - (?<int>\d++) # integer - (?<dec>\.(?&int)) # decimal fraction - )/x - - -=head2 Recursive patterns - -This feature (introduced in Perl 5.10) significantly extends the -power of Perl's pattern matching. By referring to some other -capture group anywhere in the pattern with the construct -C<(?group-ref)>, the I<pattern> within the referenced group is used -as an independent subpattern in place of the group reference itself. -Because the group reference may be contained I<within> the group it -refers to, it is now possible to apply pattern matching to tasks that -hitherto required a recursive parser. - -To illustrate this feature, we'll design a pattern that matches if -a string contains a palindrome. (This is a word or a sentence that, -while ignoring spaces, interpunctuation and case, reads the same backwards -as forwards. We begin by observing that the empty string or a string -containing just one word character is a palindrome. Otherwise it must -have a word character up front and the same at its end, with another -palindrome in between. - - /(?: (\w) (?...Here be a palindrome...) \g{-1} | \w? )/x - -Adding C<\W*> at either end to eliminate what is to be ignored, we already -have the full pattern: - - my $pp = qr/^(\W* (?: (\w) (?1) \g{-1} | \w? ) \W*)$/ix; - for $s ( "saippuakauppias", "A man, a plan, a canal: Panama!" ){ - print "'$s' is a palindrome\n" if $s =~ /$pp/; - } - -In C<(?...)> both absolute and relative backreferences may be used. -The entire pattern can be reinserted with C<(?R)> or C<(?0)>. -If you prefer to name your groups, you can use C<(?&name)> to -recurse into that group. - - =head2 A bit of magic: executing Perl code in a regular expression Normally, regexps are a part of Perl expressions. -I<Code evaluation> expressions turn that around by allowing -arbitrary Perl code to be a part of a regexp. A code evaluation -expression is denoted C<(?{code})>, with I<code> a string of Perl +S<B<Code evaluation> > expressions turn that around by allowing +arbitrary Perl code to be a part of of a regexp. A code evaluation +expression is denoted C<(?{code})>, with C<code> a string of Perl statements. -Be warned that this feature is considered experimental, and may be -changed without notice. - Code expressions are zero-width assertions, and the value they return depends on their environment. There are two possibilities: either the code expression is used as a conditional in a conditional expression @@ -2517,20 +2144,20 @@ At first glance, you'd think that it shouldn't print, because obviously the C<ddd> isn't going to match the target string. But look at this example: - $x =~ /abc(?{print "Hi Mom!";})[dD]dd/; # doesn't match, - # but _does_ print + $x =~ /abc(?{print "Hi Mom!";})[d]dd/; # doesn't match, + # but _does_ print Hmm. What happened here? If you've been following along, you know that -the above pattern should be effectively (almost) the same as the last one; -enclosing the C<d> in a character class isn't going to change what it +the above pattern should be effectively the same as the last one -- +enclosing the d in a character class isn't going to change what it matches. So why does the first not print while the second one does? -The answer lies in the optimizations the regex engine makes. In the first +The answer lies in the optimizations the REx engine makes. In the first case, all the engine sees are plain old characters (aside from the C<?{}> construct). It's smart enough to realize that the string 'ddd' doesn't occur in our target string before actually running the pattern through. But in the second case, we've tricked it into thinking that our -pattern is more complicated. It takes a look, sees our +pattern is more complicated than it is. It takes a look, sees our character class, and decides that it will have to actually run the pattern to determine whether or not it matches, and in the process of running it hits the print statement before it discovers that we don't @@ -2571,8 +2198,8 @@ This prints 'a' count is 2, $c variable is 'bob' -If we replace the S<C< (?{local $c = $c + 1;})>> with -S<C< (?{$c = $c + 1;})>>, the variable changes are I<not> undone +If we replace the S<C< (?{local $c = $c + 1;})> > with +S<C< (?{$c = $c + 1;})> >, the variable changes are I<not> undone during backtracking, and we get 'a' count is 4, $c variable is 'bob' @@ -2593,8 +2220,8 @@ produces The result C<$^R> is automatically localized, so that it will behave properly in the presence of backtracking. -This example uses a code expression in a conditional to match a -definite article, either 'the' in English or 'der|die|das' in German: +This example uses a code expression in a conditional to match the +article 'the' in either English or German: $lang = 'DE'; # use German ... @@ -2604,7 +2231,7 @@ definite article, either 'the' in English or 'der|die|das' in German: $lang eq 'EN'; # is the language English? }) the | # if so, then match 'the' - (der|die|das) # else, match 'der|die|das' + (die|das|der) # else, match 'die|das|der' ) /xi; @@ -2613,23 +2240,23 @@ C<(?((?{...}))yes-regexp|no-regexp)>. In other words, in the case of a code expression, we don't need the extra parentheses around the conditional. -If you try to use code expressions where the code text is contained within -an interpolated variable, rather than appearing literally in the pattern, -Perl may surprise you: +If you try to use code expressions with interpolating variables, perl +may surprise you: $bar = 5; $pat = '(?{ 1 })'; /foo(?{ $bar })bar/; # compiles ok, $bar not interpolated - /foo(?{ 1 })$bar/; # compiles ok, $bar interpolated + /foo(?{ 1 })$bar/; # compile error! /foo${pat}bar/; # compile error! $pat = qr/(?{ $foo = 1 })/; # precompile code regexp /foo${pat}bar/; # compiles ok -If a regexp has a variable that interpolates a code expression, Perl -treats the regexp as an error. If the code expression is precompiled into -a variable, however, interpolating is ok. The question is, why is this an -error? +If a regexp has (1) code expressions and interpolating variables,or +(2) a variable that interpolates a code expression, perl treats the +regexp as an error. If the code expression is precompiled into a +variable, however, interpolating is ok. The question is, why is this +an error? The reason is that variable interpolation and code expressions together pose a security risk. The combination is dangerous because @@ -2642,19 +2269,20 @@ plug it directly into a regexp: If the C<$regexp> variable contains a code expression, the user could then execute arbitrary Perl code. For instance, some joker could -search for S<C<system('rm -rf *');>> to erase your files. In this -sense, the combination of interpolation and code expressions I<taints> +search for S<C<system('rm -rf *');> > to erase your files. In this +sense, the combination of interpolation and code expressions B<taints> your regexp. So by default, using both interpolation and code expressions in the same regexp is not allowed. If you're not concerned about malicious users, it is possible to bypass this -security check by invoking S<C<use re 'eval'>>: +security check by invoking S<C<use re 'eval'> >: use re 'eval'; # throw caution out the door $bar = 5; $pat = '(?{ 1 })'; + /foo(?{ 1 })$bar/; # compiles ok /foo${pat}bar/; # compiles ok -Another form of code expression is the I<pattern code expression>. +Another form of code expression is the S<B<pattern code expression> >. The pattern code expression is like a regular code expression, except that the result of the code evaluation is treated as a regular expression and matched immediately. A simple example is @@ -2666,90 +2294,52 @@ expression and matched immediately. A simple example is This final example contains both ordinary and pattern code -expressions. It detects whether a binary string C<1101010010001...> has a +expressions. It detects if a binary string C<1101010010001...> has a Fibonacci spacing 0,1,1,2,3,5,... of the C<1>'s: + $s0 = 0; $s1 = 1; # initial conditions $x = "1101010010001000001"; - $z0 = ''; $z1 = '0'; # initial conditions print "It is a Fibonacci sequence\n" if $x =~ /^1 # match an initial '1' - (?: - ((??{ $z0 })) # match some '0' - 1 # and then a '1' - (?{ $z0 = $z1; $z1 .= $^N; }) + ( + (??{'0' x $s0}) # match $s0 of '0' + 1 # and then a '1' + (?{ + $largest = $s0; # largest seq so far + $s2 = $s1 + $s0; # compute next term + $s0 = $s1; # in Fibonacci sequence + $s1 = $s2; + }) )+ # repeat as needed $ # that is all there is /x; - printf "Largest sequence matched was %d\n", length($z1)-length($z0); + print "Largest sequence matched was $largest\n"; -Remember that C<$^N> is set to whatever was matched by the last -completed capture group. This prints +This prints It is a Fibonacci sequence Largest sequence matched was 5 Ha! Try that with your garden variety regexp package... -Note that the variables C<$z0> and C<$z1> are not substituted when the +Note that the variables C<$s0> and C<$s1> are not substituted when the regexp is compiled, as happens for ordinary variables outside a code -expression. Rather, the whole code block is parsed as perl code at the -same time as perl is compiling the code containing the literal regexp -pattern. +expression. Rather, the code expressions are evaluated when perl +encounters them during the search for a match. The regexp without the C<//x> modifier is - /^1(?:((??{ $z0 }))1(?{ $z0 = $z1; $z1 .= $^N; }))+$/ - -which shows that spaces are still possible in the code parts. Nevertheless, -when working with code and conditional expressions, the extended form of -regexps is almost necessary in creating and debugging regexps. - - -=head2 Backtracking control verbs - -Perl 5.10 introduced a number of control verbs intended to provide -detailed control over the backtracking process, by directly influencing -the regexp engine and by providing monitoring techniques. As all -the features in this group are experimental and subject to change or -removal in a future version of Perl, the interested reader is -referred to L<perlre/"Special Backtracking Control Verbs"> for a -detailed description. - -Below is just one example, illustrating the control verb C<(*FAIL)>, -which may be abbreviated as C<(*F)>. If this is inserted in a regexp -it will cause it to fail, just as it would at some -mismatch between the pattern and the string. Processing -of the regexp continues as it would after any "normal" -failure, so that, for instance, the next position in the string or another -alternative will be tried. As failing to match doesn't preserve capture -groups or produce results, it may be necessary to use this in -combination with embedded code. - - %count = (); - "supercalifragilisticexpialidocious" =~ - /([aeiou])(?{ $count{$1}++; })(*FAIL)/i; - printf "%3d '%s'\n", $count{$_}, $_ for (sort keys %count); - -The pattern begins with a class matching a subset of letters. Whenever -this matches, a statement like C<$count{'a'}++;> is executed, incrementing -the letter's counter. Then C<(*FAIL)> does what it says, and -the regexp engine proceeds according to the book: as long as the end of -the string hasn't been reached, the position is advanced before looking -for another vowel. Thus, match or no match makes no difference, and the -regexp engine proceeds until the entire string has been inspected. -(It's remarkable that an alternative solution using something like - - $count{lc($_)}++ for split('', "supercalifragilisticexpialidocious"); - printf "%3d '%s'\n", $count2{$_}, $_ for ( qw{ a e i o u } ); - -is considerably slower.) + /^1((??{'0'x$s0})1(?{$largest=$s0;$s2=$s1+$s0$s0=$s1;$s1=$s2;}))+$/; +and is a great start on an Obfuscated Perl entry :-) When working with +code and conditional expressions, the extended form of regexps is +almost necessary in creating and debugging regexps. =head2 Pragmas and debugging Speaking of debugging, there are several pragmas available to control and debug regexps in Perl. We have already encountered one pragma in -the previous section, S<C<use re 'eval';>>, that allows variable +the previous section, S<C<use re 'eval';> >, that allows variable interpolation and code expressions to coexist in a regexp. The other pragmas are @@ -2765,15 +2355,6 @@ performing some other processing. Both C<taint> and C<eval> pragmas are lexically scoped, which means they are in effect only until the end of the block enclosing the pragmas. - use re '/m'; # or any other flags - $multiline_string =~ /^foo/; # /m is implied - -The C<re '/flags'> pragma (introduced in Perl -5.14) turns on the given regular expression flags -until the end of the lexical scope. See -L<re/"'E<sol>flags' mode"> for more -detail. - use re 'debug'; /^(.*)$/s; # output debugging info @@ -2787,7 +2368,7 @@ information is displayed in color on terminals that can display termcap color sequences. Here is example output: % perl -e 'use re "debug"; "abc" =~ /a*b+c/;' - Compiling REx 'a*b+c' + Compiling REx `a*b+c' size 9 first at 1 1: STAR(4) 2: EXACT <a>(0) @@ -2795,28 +2376,28 @@ termcap color sequences. Here is example output: 5: EXACT <b>(0) 7: EXACT <c>(9) 9: END(0) - floating 'bc' at 0..2147483647 (checking floating) minlen 2 - Guessing start of match, REx 'a*b+c' against 'abc'... - Found floating substr 'bc' at offset 1... + floating `bc' at 0..2147483647 (checking floating) minlen 2 + Guessing start of match, REx `a*b+c' against `abc'... + Found floating substr `bc' at offset 1... Guessed: match at offset 0 - Matching REx 'a*b+c' against 'abc' + Matching REx `a*b+c' against `abc' Setting an EVAL scope, savestack=3 - 0 <> <abc> | 1: STAR - EXACT <a> can match 1 times out of 32767... + 0 <> <abc> | 1: STAR + EXACT <a> can match 1 times out of 32767... Setting an EVAL scope, savestack=3 - 1 <a> <bc> | 4: PLUS - EXACT <b> can match 1 times out of 32767... + 1 <a> <bc> | 4: PLUS + EXACT <b> can match 1 times out of 32767... Setting an EVAL scope, savestack=3 - 2 <ab> <c> | 7: EXACT <c> - 3 <abc> <> | 9: END + 2 <ab> <c> | 7: EXACT <c> + 3 <abc> <> | 9: END Match successful! - Freeing REx: 'a*b+c' + Freeing REx: `a*b+c' If you have gotten this far into the tutorial, you can probably guess what the different parts of the debugging output tell you. The first part - Compiling REx 'a*b+c' + Compiling REx `a*b+c' size 9 first at 1 1: STAR(4) 2: EXACT <a>(0) @@ -2830,32 +2411,32 @@ starred object, in this case C<'a'>, and if it matches, goto line 4, i.e., C<PLUS(7)>. The middle lines describe some heuristics and optimizations performed before a match: - floating 'bc' at 0..2147483647 (checking floating) minlen 2 - Guessing start of match, REx 'a*b+c' against 'abc'... - Found floating substr 'bc' at offset 1... + floating `bc' at 0..2147483647 (checking floating) minlen 2 + Guessing start of match, REx `a*b+c' against `abc'... + Found floating substr `bc' at offset 1... Guessed: match at offset 0 Then the match is executed and the remaining lines describe the process: - Matching REx 'a*b+c' against 'abc' + Matching REx `a*b+c' against `abc' Setting an EVAL scope, savestack=3 - 0 <> <abc> | 1: STAR - EXACT <a> can match 1 times out of 32767... + 0 <> <abc> | 1: STAR + EXACT <a> can match 1 times out of 32767... Setting an EVAL scope, savestack=3 - 1 <a> <bc> | 4: PLUS - EXACT <b> can match 1 times out of 32767... + 1 <a> <bc> | 4: PLUS + EXACT <b> can match 1 times out of 32767... Setting an EVAL scope, savestack=3 - 2 <ab> <c> | 7: EXACT <c> - 3 <abc> <> | 9: END + 2 <ab> <c> | 7: EXACT <c> + 3 <abc> <> | 9: END Match successful! - Freeing REx: 'a*b+c' + Freeing REx: `a*b+c' -Each step is of the form S<C<< n <x> <y> >>>, with C<< <x> >> the +Each step is of the form S<C<< n <x> <y> >> >, with C<< <x> >> the part of the string matched and C<< <y> >> the part not yet -matched. The S<C<< | 1: STAR >>> says that Perl is at line number 1 -in the compilation list above. See -L<perldebguts/"Debugging Regular Expressions"> for much more detail. +matched. The S<C<< | 1: STAR >> > says that perl is at line number 1 +n the compilation list above. See +L<perldebguts/"Debugging regular expressions"> for much more detail. An alternative method of debugging regexps is to embed C<print> statements within the regexp. This provides a blow-by-blow account of @@ -2888,11 +2469,11 @@ prints =head1 BUGS Code expressions, conditional expressions, and independent expressions -are I<experimental>. Don't use them in production code. Yet. +are B<experimental>. Don't use them in production code. Yet. =head1 SEE ALSO -This is just a tutorial. For the full story on Perl regular +This is just a tutorial. For the full story on perl regular expressions, see the L<perlre> regular expressions reference page. For more information on the matching C<m//> and substitution C<s///> diff --git a/gnu/usr.bin/perl/pod/perlutil.pod b/gnu/usr.bin/perl/pod/perlutil.pod index 55bfd5c7d5e..be7a345f796 100644 --- a/gnu/usr.bin/perl/pod/perlutil.pod +++ b/gnu/usr.bin/perl/pod/perlutil.pod @@ -11,9 +11,7 @@ process. This document exists to list all of these utilities, explain what they are for and provide pointers to each module's documentation, if appropriate. -=head1 LIST OF UTILITIES - -=head2 Documentation +=head2 DOCUMENTATION =over 3 @@ -31,14 +29,15 @@ described in this document. If it's run from a terminal, F<perldoc> will usually call F<pod2man> to translate POD (Plain Old Documentation - see L<perlpod> for an -explanation) into a manpage, and then run F<man> to display it; if +explanation) into a man page, and then run F<man> to display it; if F<man> isn't available, F<pod2text> will be used instead and the output piped through your favourite pager. -=item L<pod2html|pod2html> +=item L<pod2html|pod2html> and L<pod2latex|pod2latex> -As well as these two, there is another converter: F<pod2html> will -produce HTML pages from POD. +As well as these two, there are two other converters: F<pod2html> will +produce HTML pages from POD, and F<pod2latex>, which produces LaTeX +files. =item L<pod2usage|pod2usage> @@ -65,7 +64,7 @@ utility will look for errors in your markup. F<splain> is an interface to L<perldiag> - paste in your error message to it, and it'll explain it for you. -=item C<roffitall> +=item L<roffitall|roffitall> The C<roffitall> utility is not installed on your system but lives in the F<pod/> directory of your Perl source kit; it converts all the @@ -74,7 +73,7 @@ typeset PostScript or text file of the whole lot. =back -=head2 Converters +=head2 CONVERTORS To help you convert legacy programs to Perl, we've included three conversion filters: @@ -88,11 +87,11 @@ on the simple F<awk> script C<{print $2}> will produce a Perl program based around this code: while (<>) { - ($Fld1,$Fld2) = split(/[:\n]/, $_, -1); + ($Fld1,$Fld2) = split(/[:\n]/, $_, 9999); print $Fld2; } -=item L<s2p|s2p> and L<psed> +=item L<s2p|s2p> Similarly, F<s2p> converts F<sed> scripts to Perl programs. F<s2p> run on C<s/foo/bar> will produce a Perl program based around this: @@ -103,9 +102,6 @@ on C<s/foo/bar> will produce a Perl program based around this: print if $printit; } -When invoked as F<psed>, it behaves as a F<sed> implementation, written in -Perl. - =item L<find2perl|find2perl> Finally, F<find2perl> translates C<find> commands to Perl equivalents which @@ -127,28 +123,6 @@ As well as these filters for converting other languages, the L<pl2pm|pl2pm> utility will help you convert old-style Perl 4 libraries to new-style Perl5 modules. -=head2 Administration - -=over 3 - -=item L<config_data|config_data> - -Query or change configuration of Perl modules that use Module::Build-based -configuration files for features and config data. - -=item L<libnetcfg|libnetcfg> - -To display and change the libnet configuration run the libnetcfg command. - -=item L<perlivp> - -The F<perlivp> program is set up at Perl source code build time to test -the Perl version it was built under. It can be used after running C<make -install> (or your platform's equivalent procedure) to verify that perl -and its libraries have been installed correctly. - -=back - =head2 Development There are a set of utilities which help you in developing Perl programs, @@ -163,12 +137,6 @@ itself or any of the standard library modules back to the developers; please read through the documentation for F<perlbug> thoroughly before using it to submit a bug report. -=item L<perlthanks|perlbug> - -This program provides an easy way to send a thank-you message back to the -authors and maintainers of perl. It's just F<perlbug> installed under -another name. - =item L<h2ph|h2ph> Back before Perl had the XS system for connecting with C libraries, @@ -176,7 +144,7 @@ programmers used to get library constants by reading through the C header files. You may still see C<require 'syscall.ph'> or similar around - the F<.ph> file should be created by running F<h2ph> on the corresponding F<.h> file. See the F<h2ph> documentation for more on how -to convert a whole bunch of header files at once. +to convert a whole bunch of header files at ones. =item L<c2ph|c2ph> and L<pstruct|pstruct> @@ -191,108 +159,27 @@ F<h2xs> converts C header files into XS modules, and will try and write as much glue between C libraries and Perl modules as it can. It's also very useful for creating skeletons of pure Perl modules. -=item L<enc2xs> - -F<enc2xs> builds a Perl extension for use by Encode from either -Unicode Character Mapping files (.ucm) or Tcl Encoding Files (.enc). -Besides being used internally during the build process of the Encode -module, you can use F<enc2xs> to add your own encoding to perl. -No knowledge of XS is necessary. - -=item L<xsubpp> - -F<xsubpp> is a compiler to convert Perl XS code into C code. -It is typically run by the makefiles created by L<ExtUtils::MakeMaker>. - -F<xsubpp> will compile XS code into C code by embedding the constructs -necessary to let C functions manipulate Perl values and creates the glue -necessary to let Perl access those functions. - -=item L<prove> - -F<prove> is a command-line interface to the test-running functionality -of F<Test::Harness>. It's an alternative to C<make test>. - -=item L<corelist> - -A command-line front-end to C<Module::CoreList>, to query what modules -were shipped with given versions of perl. - -=back - -=head2 General tools - -A few general-purpose tools are shipped with perl, mostly because they -came along modules included in the perl distribution. - -=over 3 - -=item L<piconv> - -B<piconv> is a Perl version of B<iconv>, a character encoding converter -widely available for various Unixen today. This script was primarily a -technology demonstrator for Perl v5.8.0, but you can use piconv in the -place of iconv for virtually any case. - -=item L<ptar> - -F<ptar> is a tar-like program, written in pure Perl. - -=item L<ptardiff> - -F<ptardiff> is a small utility that produces a diff between an extracted -archive and an unextracted one. (Note that this utility requires the -C<Text::Diff> module to function properly; this module isn't distributed -with perl, but is available from the CPAN.) - -=item L<ptargrep> - -F<ptargrep> is a utility to apply pattern matching to the contents of files -in a tar archive. - -=item L<shasum> - -This utility, that comes with the C<Digest::SHA> module, is used to print -or verify SHA checksums. - -=item L<zipdetails> - -L<zipdetails> displays information about the internal record structure of the zip file. -It is not concerned with displaying any details of the compressed data stored in the zip file. - -=back - -=head2 Installation - -These utilities help manage extra Perl modules that don't come with the perl -distribution. - -=over 3 - -=item L<cpan> - -F<cpan> is a command-line interface to CPAN.pm. It allows you to install -modules or distributions from CPAN, or just get information about them, and -a lot more. It is similar to the command line mode of the L<CPAN> module, +=item L<dprofpp|dprofpp> - perl -MCPAN -e shell +Perl comes with a profiler, the F<Devel::Dprof> module. The +F<dprofpp> utility analyzes the output of this profiler and tells you +which subroutines are taking up the most run time. See L<Devel::Dprof> +for more information. -=item L<instmodsh> +=item L<perlcc|perlcc> -A little interface to ExtUtils::Installed to examine installed modules, -validate your packlists and even create a tarball from an installed module. +F<perlcc> is the interface to the experimental Perl compiler suite. =back -=head1 SEE ALSO +=head2 SEE ALSO L<perldoc|perldoc>, L<pod2man|pod2man>, L<perlpod>, L<pod2html|pod2html>, L<pod2usage|pod2usage>, L<podselect|podselect>, L<podchecker|podchecker>, L<splain|splain>, L<perldiag>, -C<roffitall|roffitall>, L<a2p|a2p>, L<s2p|s2p>, L<find2perl|find2perl>, +L<roffitall|roffitall>, L<a2p|a2p>, L<s2p|s2p>, L<find2perl|find2perl>, L<File::Find|File::Find>, L<pl2pm|pl2pm>, L<perlbug|perlbug>, -L<h2ph|h2ph>, L<c2ph|c2ph>, L<h2xs|h2xs>, L<enc2xs>, L<xsubpp>, -L<cpan>, L<instmodsh>, L<piconv>, L<prove>, -L<corelist>, L<ptar>, L<ptardiff>, L<shasum>, L<zipdetails> +L<h2ph|h2ph>, L<c2ph|c2ph>, L<h2xs|h2xs>, L<dprofpp|dprofpp>, +L<Devel::Dprof>, L<perlcc|perlcc> =cut diff --git a/gnu/usr.bin/perl/t/op/anonsub.t b/gnu/usr.bin/perl/t/op/anonsub.t index 6b8745f4e43..17889d9d2f9 100644 --- a/gnu/usr.bin/perl/t/op/anonsub.t +++ b/gnu/usr.bin/perl/t/op/anonsub.t @@ -1,39 +1,50 @@ -#!./perl -w +#!./perl chdir 't' if -d 't'; @INC = '../lib'; -require './test.pl'; -use strict; +$Is_VMS = $^O eq 'VMS'; +$Is_MSWin32 = $^O eq 'MSWin32'; +$ENV{PERL5LIB} = "../lib" unless $Is_VMS; $|=1; -run_multiple_progs('', \*DATA); +undef $/; +@prgs = split "\n########\n", <DATA>; +print "1..", scalar @prgs, "\n"; -foreach my $code ('sub;', 'sub ($) ;', '{ $x = sub }', 'sub ($) && 1') { - eval $code; - like($@, qr/^Illegal declaration of anonymous subroutine at/, - "'$code' is illegal"); -} +$tmpfile = "asubtmp000"; +1 while -f ++$tmpfile; +END { if ($tmpfile) { 1 while unlink $tmpfile; } } -{ - local $::TODO; - $::TODO = 'RT #17589 not completely resolved'; - # Here's a patch. It makes "sub;" and similar report an error immediately - # from the lexer. However the solution is not complete, it doesn't - # handle the case "sub ($) : lvalue;" (marked as a TODO test), because - # it's handled by the lexer in separate tokens, hence more difficult to - # work out. - my $code = 'sub ($) : lvalue;'; - eval $code; - like($@, qr/^Illegal declaration of anonymous subroutine at/, - "'$code' is illegal"); +for (@prgs){ + my $switch = ""; + if (s/^\s*(-\w+)//){ + $switch = $1; + } + my($prog,$expected) = split(/\nEXPECT\n/, $_); + open TEST, ">$tmpfile"; + print TEST "$prog\n"; + close TEST; + my $results = $Is_VMS ? + `MCR $^X "-I[-.lib]" $switch $tmpfile 2>&1` : + $Is_MSWin32 ? + `.\\perl -I../lib $switch $tmpfile 2>&1` : + `./perl $switch $tmpfile 2>&1`; + my $status = $?; + $results =~ s/\n+$//; + # allow expected output to be written as if $prog is on STDIN + $results =~ s/runltmp\d+/-/g; + $results =~ s/\n%[A-Z]+-[SIWEF]-.*$// if $Is_VMS; # clip off DCL status msg + $expected =~ s/\n+$//; + if ($results ne $expected) { + print STDERR "PROG: $switch\n$prog\n"; + print STDERR "EXPECTED:\n$expected\n"; + print STDERR "GOT:\n$results\n"; + print "not "; + } + print "ok ", ++$i, "\n"; } -eval "sub #foo\n{print 1}"; -is($@, ''); - -done_testing(); - __END__ sub X { my $n = "ok 1\n"; @@ -80,14 +91,3 @@ sub X { X(); EXPECT ok 1 -######## -print sub { return "ok 1\n" } -> (); -EXPECT -ok 1 -######## -# [perl #71154] undef &$code makes $code->() die with: Not a CODE reference -sub __ANON__ { print "42\n" } -undef &{$x=sub{}}; -$x->(); -EXPECT -Undefined subroutine called at - line 4. diff --git a/gnu/usr.bin/perl/t/op/length.t b/gnu/usr.bin/perl/t/op/length.t index b144b097466..ceb005ecc4a 100644 --- a/gnu/usr.bin/perl/t/op/length.t +++ b/gnu/usr.bin/perl/t/op/length.t @@ -2,11 +2,10 @@ BEGIN { chdir 't' if -d 't'; - require './test.pl'; @INC = '../lib'; } -plan (tests => 41); +print "1..13\n"; print "not " unless length("") == 0; print "ok 1\n"; @@ -34,215 +33,53 @@ print "ok 3\n"; } { - my $a = pack("U", 0xFF); - + my $a = "\x{80}"; + print "not " unless length($a) == 1; print "ok 6\n"; $test++; - + use bytes; - if (ord('A') == 193) - { - printf "#%vx for 0xFF\n",$a; - print "not " unless $a eq "\x8b\x73" && length($a) == 2; - } - else - { - print "not " unless $a eq "\xc3\xbf" && length($a) == 2; - } + print "not " unless $a eq "\xc2\x80" && length($a) == 2; print "ok 7\n"; $test++; } { my $a = "\x{100}"; - + print "not " unless length($a) == 1; print "ok 8\n"; $test++; - + use bytes; - if (ord('A') == 193) - { - printf "#%vx for 0x100\n",$a; - print "not " unless $a eq "\x8c\x41" && length($a) == 2; - } - else - { - print "not " unless $a eq "\xc4\x80" && length($a) == 2; - } + print "not " unless $a eq "\xc4\x80" && length($a) == 2; print "ok 9\n"; $test++; } { my $a = "\x{100}\x{80}"; - + print "not " unless length($a) == 2; print "ok 10\n"; $test++; - + use bytes; - if (ord('A') == 193) - { - printf "#%vx for 0x100 0x80\n",$a; - print "not " unless $a eq "\x8c\x41\x8a\x67" && length($a) == 4; - } - else - { - print "not " unless $a eq "\xc4\x80\xc2\x80" && length($a) == 4; - } + print "not " unless $a eq "\xc4\x80\xc2\x80" && length($a) == 4; print "ok 11\n"; $test++; } { my $a = "\x{80}\x{100}"; - + print "not " unless length($a) == 2; print "ok 12\n"; $test++; - - use bytes; - if (ord('A') == 193) - { - printf "#%vx for 0x80 0x100\n",$a; - print "not " unless $a eq "\x8a\x67\x8c\x41" && length($a) == 4; - } - else - { - print "not " unless $a eq "\xc2\x80\xc4\x80" && length($a) == 4; - } - print "ok 13\n"; - $test++; -} - -# Now for Unicode with magical vtbls - -{ - require Tie::Scalar; - my $a; - tie $a, 'Tie::StdScalar'; # makes $a magical - $a = "\x{263A}"; - print "not " unless length($a) == 1; - print "ok 14\n"; - $test++; - use bytes; - print "not " unless length($a) == 3; - print "ok 15\n"; + print "not " unless $a eq "\xc2\x80\xc4\x80" && length($a) == 4; + print "ok 13\n"; $test++; } - -{ - # Play around with Unicode strings, - # give a little workout to the UTF-8 length cache. - my $a = chr(256) x 100; - print length $a == 100 ? "ok 16\n" : "not ok 16\n"; - chop $a; - print length $a == 99 ? "ok 17\n" : "not ok 17\n"; - $a .= $a; - print length $a == 198 ? "ok 18\n" : "not ok 18\n"; - $a = chr(256) x 999; - print length $a == 999 ? "ok 19\n" : "not ok 19\n"; - substr($a, 0, 1) = ''; - print length $a == 998 ? "ok 20\n" : "not ok 20\n"; -} - -curr_test(21); - -require Tie::Scalar; - -$u = "ASCII"; - -tie $u, 'Tie::StdScalar', chr 256; - -is(length $u, 1, "Length of a UTF-8 scalar returned from tie"); -is(length $u, 1, "Again! Again!"); - -$^W = 1; - -my $warnings = 0; - -$SIG{__WARN__} = sub { - $warnings++; - warn @_; -}; - -is(length(undef), undef, "Length of literal undef"); - -my $u; - -is(length($u), undef, "Length of regular scalar"); - -$u = "Gotcha!"; - -tie $u, 'Tie::StdScalar'; - -is(length($u), undef, "Length of tied scalar (MAGIC)"); - -is($u, undef); - -{ - package U; - use overload '""' => sub {return undef;}; -} - -my $uo = bless [], 'U'; - -{ - my $w; - local $SIG{__WARN__} = sub { $w = shift }; - is(length($uo), 0, "Length of overloaded reference"); - like $w, qr/uninitialized/, 'uninit warning for stringifying as undef'; -} - -my $ul = 3; -is(($ul = length(undef)), undef, - "Returned length of undef with result in TARG"); -is($ul, undef, "Assigned length of undef with result in TARG"); - -$ul = 3; -is(($ul = length($u)), undef, - "Returned length of tied undef with result in TARG"); -is($ul, undef, "Assigned length of tied undef with result in TARG"); - -$ul = 3; -{ - my $w; - local $SIG{__WARN__} = sub { $w = shift }; - is(($ul = length($uo)), 0, - "Returned length of overloaded undef with result in TARG"); - like $w, qr/uninitialized/, 'uninit warning for stringifying as undef'; -} -is($ul, 0, "Assigned length of overloaded undef with result in TARG"); - -{ - my $y = "\x{100}BC"; - is(index($y, "B"), 1, 'adds an intermediate position to the offset cache'); - is(length $y, 3, - 'Check that sv_len_utf8() can take advantage of the offset cache'); -} - -{ - local $SIG{__WARN__} = sub { - pass("'print length undef' warned"); - }; - print length undef; -} - -{ - local $SIG{__WARN__} = sub { - pass '[perl #106726] no crash with length @lexical warning' - }; - eval ' sub { length my @forecasts } '; -} - -# length could be fooled by UTF8ness of non-magical variables changing with -# stringification. -my $ref = []; -bless $ref, "\x{100}"; -is length $ref, length "$ref", 'length on reference blessed to utf8 class'; - -is($warnings, 0, "There were no other warnings"); diff --git a/gnu/usr.bin/perl/t/op/my_stash.t b/gnu/usr.bin/perl/t/op/my_stash.t index 6ec7619e5f7..4a1d5022e02 100644 --- a/gnu/usr.bin/perl/t/op/my_stash.t +++ b/gnu/usr.bin/perl/t/op/my_stash.t @@ -3,18 +3,17 @@ package Foo; BEGIN { - chdir 't' if -d 't'; @INC = '../lib'; - require './test.pl'; } -plan 9; +use Test; + +plan tests => 7; use constant MyClass => 'Foo::Bar::Biz::Baz'; { package Foo::Bar::Biz::Baz; - 1; } for (qw(Foo Foo:: MyClass __PACKAGE__)) { @@ -30,10 +29,3 @@ for (qw(Nope Nope:: NoClass)) { ok $@; # print $@ if $@; } - -is runperl(prog => 'my main $r; sub FIELDS; $$r{foo}; print qq-ok\n-'), - "ok\n", - 'no crash with hash element when FIELDS sub stub exists'; -is runperl(prog => 'my main $r; sub FIELDS; @$r{f,b}; print qq-ok\n-'), - "ok\n", - 'no crash with hash slice when FIELDS sub stub exists'; diff --git a/gnu/usr.bin/perl/t/op/reverse.t b/gnu/usr.bin/perl/t/op/reverse.t index 0796614ffbf..bb7b9b77fea 100644 --- a/gnu/usr.bin/perl/t/op/reverse.t +++ b/gnu/usr.bin/perl/t/op/reverse.t @@ -3,84 +3,23 @@ BEGIN { chdir 't' if -d 't'; @INC = '../lib'; - require './test.pl'; } -plan tests => 26; +print "1..4\n"; -is(reverse("abc"), "cba", 'simple reverse'); +print "not " unless reverse("abc") eq "cba"; +print "ok 1\n"; $_ = "foobar"; -is(reverse(), "raboof", 'reverse of the default variable'); +print "not " unless reverse() eq "raboof"; +print "ok 2\n"; { my @a = ("foo", "bar"); my @b = reverse @a; - is($b[0], $a[1], 'array reversal moved second element to first'); - is($b[1], $a[0], 'array reversal moved first element to second'); -} - -{ - my @a = (1, 2, 3, 4); - @a = reverse @a; - is("@a", "4 3 2 1", 'four element array reversed'); - - delete $a[1]; - @a = reverse @a; - ok(!exists $a[2], 'array reversed with deleted second element'); - is($a[0] . $a[1] . $a[3], '124', 'remaining elements ok after delete and reverse'); - - @a = (5, 6, 7, 8, 9); - @a = reverse @a; - is("@a", "9 8 7 6 5", 'five element array reversed'); - - delete $a[3]; - @a = reverse @a; - ok(!exists $a[1], 'five element array reversed with deleted fourth element'); - is($a[0] . $a[2] . $a[3] . $a[4], '5789', 'remaining elements ok after delete and reverse'); - - delete $a[2]; - @a = reverse @a; - ok(!exists $a[2] && !exists $a[3], 'test position of two deleted elements after reversal'); - is($a[0] . $a[1] . $a[4], '985', 'check value of remaining elements'); - - my @empty; - @empty = reverse @empty; - is("@empty", "", 'reversed empty array is still empty'); -} - -use Tie::Array; - -{ - tie my @a, 'Tie::StdArray'; - - @a = (1, 2, 3, 4); - @a = reverse @a; - is("@a", "4 3 2 1", 'tie array reversal'); - - delete $a[1]; - @a = reverse @a; - ok(!exists $a[2], 'deleted element position ok after reversal of tie array'); - is($a[0] . $a[1] . $a[3], '124', 'remaining elements ok after delete and reversal for tie array'); - - @a = (5, 6, 7, 8, 9); - @a = reverse @a; - is("@a", "9 8 7 6 5", 'five element tie array reversal'); - - delete $a[3]; - @a = reverse @a; - ok(!exists $a[1], 'deleted element position ok after tie array reversal'); - is($a[0] . $a[2] . $a[3] . $a[4], '5789', 'remaining elements ok after tie array delete and reversal'); - - delete $a[2]; - @a = reverse @a; - ok(!exists $a[2] && !exists $a[3], 'two deleted element positions ok after tie array reversal'); - is($a[0] . $a[1] . $a[4], '985', 'remaining elements ok after two deletes and reversals'); - - tie my @empty, "Tie::StdArray"; - @empty = reverse @empty; - is(scalar(@empty), 0, 'reversed tie array still empty after reversal'); + print "not " unless $b[0] eq $a[1] && $b[1] eq $a[0]; + print "ok 3\n"; } { @@ -89,18 +28,6 @@ use Tie::Array; my $a = "\x{263A}\x{263A}x\x{263A}y\x{263A}"; my $b = scalar reverse($a); my $c = scalar reverse($b); - is($a, $c, 'Unicode string double reversal matches original'); -} - -{ - # Lexical $_. - no warnings 'experimental::lexical_topic'; - sub blurp { my $_ = shift; reverse } - - is(blurp("foo"), "oof", 'reversal of default variable in function'); - is(sub { my $_ = shift; reverse }->("bar"), "rab", 'reversal of default variable in anonymous function'); - { - local $_ = "XXX"; - is(blurp("paz"), "zap", 'reversal of default variable with local value set' ); - } + print "not " unless $a eq $c; + print "ok 4\n"; } diff --git a/gnu/usr.bin/perl/t/op/utf8decode.t b/gnu/usr.bin/perl/t/op/utf8decode.t index 92de82110ed..4d05a6b8d37 100644 --- a/gnu/usr.bin/perl/t/op/utf8decode.t +++ b/gnu/usr.bin/perl/t/op/utf8decode.t @@ -3,181 +3,181 @@ BEGIN { chdir 't' if -d 't'; @INC = '../lib'; - require './test.pl'; -} - -{ - my $wide = v256; - use bytes; - my $ordwide = ord($wide); - printf "# under use bytes ord(v256) = 0x%02x\n", $ordwide; - skip_all('UTF-EBCDIC (not UTF-8) used here') if $ordwide == 140; - - if ($ordwide != 196) { - printf "# v256 starts with 0x%02x\n", $ordwide; - } } no utf8; -foreach (<DATA>) { - if (/^(?:\d+(?:\.\d+)?)\s/ || /^#/) { - # print "# $_\n"; - } elsif (my ($id, $okay, $Unicode, $byteslen, $hex, $charslen, $experr) - = /^(\d+\.\d+\.\d+[bu]?) # ID - \s+(y|n|N-?\d+) # expect to pass or fail - \s+([0-9a-f]{1,8}(?:,[0-9a-f]{1,8})*|-) # Unicode characters - \s+(\d+) # number of octets - \s+([0-9a-f]{2}(?::[0-9a-f]{2})*) # octets in hex - \s+(\d+|-) # number of characters - (?:\s+(.+))? # expected error (or comment) - $/x) { - my @hex = split(/:/, $hex); - is(scalar @hex, $byteslen, 'Amount of hex tallies with byteslen'); - my $octets = join '', map {chr hex $_} @hex; - is(length $octets, $byteslen, 'Number of octets tallies with byteslen'); - if ($okay eq 'y') { - my @chars = map {hex $_} split ',', $Unicode; - is(scalar @chars, $charslen, 'Amount of hex tallies with charslen'); - my @got; - warning_is(sub {@got = unpack 'C0U*', $octets}, undef, - "No warnings expected for $id"); - is("@got", "@chars", 'Got expected Unicode characters'); - } elsif ($okay eq 'n') { - isnt($experr, '', "Expected warning for $id provided"); - warnings_like(sub {unpack 'C0U*', $octets}, [qr/$experr/], - "Only expected warning for $id"); - } elsif ($okay !~ /^N(-?\d+)/) { - is($okay, 'n', "Confused test description for $id"); - } else { - my $expect = $1; - my @warnings; - - { - local $SIG{__WARN__} = sub { - print "# $id: @_"; - push @warnings, "@_"; - }; - unpack 'C0U*', $octets; - } - - isnt($experr, '', "Expected first warning for $id provided"); - like($warnings[0], qr/$experr/, "Expected first warning for $id seen"); - local $::TODO; - if ($expect < 0) { - $expect = -$expect; - $::TODO = "Markus Kuhn states that $expect invalid sequences should be signalled"; - } - is(scalar @warnings, $expect, "Expected number of warnings for $id seen"); - } - } else { - fail("unknown format '$_'"); - } -} +print "1..78\n"; -done_testing(); +my $test = 1; # This table is based on Markus Kuhn's UTF-8 Decode Stress Tester, # http://www.cl.cam.ac.uk/~mgk25/ucs/examples/UTF-8-test.txt, -# version dated 2000-09-02. +# version dated 2000-09-02. + +# We use the \x notation instead of raw binary bytes for \x00-\x1f\x7f-\xff +# because e.g. many patch programs have issues with binary data. -__DATA__ +my @MK = split(/\n/, <<__EOMK__); 1 Correct UTF-8 -1.1.1 y 3ba,1f79,3c3,3bc,3b5 11 ce:ba:e1:bd:b9:cf:83:ce:bc:ce:b5 5 -2 Boundary conditions +1.1.1 y "\xce\xba\xe1\xbd\xb9\xcf\x83\xce\xbc\xce\xb5" - 11 ce:ba:e1:bd:b9:cf:83:ce:bc:ce:b5 5 +2 Boundary conditions 2.1 First possible sequence of certain length -2.1.1 y 0 1 00 1 -2.1.2 y 80 2 c2:80 1 -2.1.3 y 800 3 e0:a0:80 1 -2.1.4 y 10000 4 f0:90:80:80 1 -2.1.5 y 200000 5 f8:88:80:80:80 1 -2.1.6 y 4000000 6 fc:84:80:80:80:80 1 +2.1.1 y "\x00" 0 1 00 1 +2.1.2 y "\xc2\x80" 80 2 c2:80 1 +2.1.3 y "\xe0\xa0\x80" 800 3 e0:a0:80 1 +2.1.4 y "\xf0\x90\x80\x80" 10000 4 f0:90:80:80 1 +2.1.5 y "\xf8\x88\x80\x80\x80" 200000 5 f8:88:80:80:80 1 +2.1.6 y "\xfc\x84\x80\x80\x80\x80" 4000000 6 fc:84:80:80:80:80 1 2.2 Last possible sequence of certain length -2.2.1 y 7f 1 7f 1 -2.2.2 y 7ff 2 df:bf 1 -# The ffff is legal by default since 872c91ae155f6880 -2.2.3 y ffff 3 ef:bf:bf 1 character 0xffff -2.2.4 y 1fffff 4 f7:bf:bf:bf 1 -2.2.5 y 3ffffff 5 fb:bf:bf:bf:bf 1 -2.2.6 y 7fffffff 6 fd:bf:bf:bf:bf:bf 1 +2.2.1 y "\x7f" 7f 1 7f 1 +2.2.2 y "\xdf\xbf" 7ff 2 df:bf 1 +# The ffff is illegal unless UTF8_ALLOW_FFFF +2.2.3 n "\xef\xbf\xbf" ffff 3 ef:bf:bf 1 character 0xffff +2.2.4 y "\xf7\xbf\xbf\xbf" 1fffff 4 f7:bf:bf:bf 1 +2.2.5 y "\xfb\xbf\xbf\xbf\xbf" 3ffffff 5 fb:bf:bf:bf:bf 1 +2.2.6 y "\xfd\xbf\xbf\xbf\xbf\xbf" 7fffffff 6 fd:bf:bf:bf:bf:bf 1 2.3 Other boundary conditions -2.3.1 y d7ff 3 ed:9f:bf 1 -2.3.2 y e000 3 ee:80:80 1 -2.3.3 y fffd 3 ef:bf:bd 1 -2.3.4 y 10ffff 4 f4:8f:bf:bf 1 -2.3.5 y 110000 4 f4:90:80:80 1 +2.3.1 y "\xed\x9f\xbf" d7ff 3 ed:9f:bf 1 +2.3.2 y "\xee\x80\x80" e000 3 ee:80:80 1 +2.3.3 y "\xef\xbf\xbd" fffd 3 ef:bf:bd 1 +2.3.4 y "\xf4\x8f\xbf\xbf" 10ffff 4 f4:8f:bf:bf 1 +2.3.5 y "\xf4\x90\x80\x80" 110000 4 f4:90:80:80 1 3 Malformed sequences 3.1 Unexpected continuation bytes -3.1.1 n - 1 80 - unexpected continuation byte 0x80 -3.1.2 n - 1 bf - unexpected continuation byte 0xbf -3.1.3 N2 - 2 80:bf - unexpected continuation byte 0x80 -3.1.4 N3 - 3 80:bf:80 - unexpected continuation byte 0x80 -3.1.5 N4 - 4 80:bf:80:bf - unexpected continuation byte 0x80 -3.1.6 N5 - 5 80:bf:80:bf:80 - unexpected continuation byte 0x80 -3.1.7 N6 - 6 80:bf:80:bf:80:bf - unexpected continuation byte 0x80 -3.1.8 N7 - 7 80:bf:80:bf:80:bf:80 - unexpected continuation byte 0x80 -3.1.9 N64 - 64 80:81:82:83:84:85:86:87:88:89:8a:8b:8c:8d:8e:8f:90:91:92:93:94:95:96:97:98:99:9a:9b:9c:9d:9e:9f:a0:a1:a2:a3:a4:a5:a6:a7:a8:a9:aa:ab:ac:ad:ae:af:b0:b1:b2:b3:b4:b5:b6:b7:b8:b9:ba:bb:bc:bd:be:bf - unexpected continuation byte 0x80 +3.1.1 n "\x80" - 1 80 - unexpected continuation byte 0x80 +3.1.2 n "\xbf" - 1 bf - unexpected continuation byte 0xbf +3.1.3 n "\x80\xbf" - 2 80:bf - unexpected continuation byte 0x80 +3.1.4 n "\x80\xbf\x80" - 3 80:bf:80 - unexpected continuation byte 0x80 +3.1.5 n "\x80\xbf\x80\xbf" - 4 80:bf:80:bf - unexpected continuation byte 0x80 +3.1.6 n "\x80\xbf\x80\xbf\x80" - 5 80:bf:80:bf:80 - unexpected continuation byte 0x80 +3.1.7 n "\x80\xbf\x80\xbf\x80\xbf" - 6 80:bf:80:bf:80:bf - unexpected continuation byte 0x80 +3.1.8 n "\x80\xbf\x80\xbf\x80\xbf\x80" - 7 80:bf:80:bf:80:bf:80 - unexpected continuation byte 0x80 +3.1.9 n "\x80\x81\x82\x83\x84\x85\x86\x87\x88\x89\x8a\x8b\x8c\x8d\x8e\x8f\x90\x91\x92\x93\x94\x95\x96\x97\x98\x99\x9a\x9b\x9c\x9d\x9e\x9f\xa0\xa1\xa2\xa3\xa4\xa5\xa6\xa7\xa8\xa9\xaa\xab\xac\xad\xae\xaf\xb0\xb1\xb2\xb3\xb4\xb5\xb6\xb7\xb8\xb9\xba\xbb\xbc\xbd\xbe\xbf" - 64 80:81:82:83:84:85:86:87:88:89:8a:8b:8c:8d:8e:8f:90:91:92:93:94:95:96:97:98:99:9a:9b:9c:9d:9e:9f:a0:a1:a2:a3:a4:a5:a6:a7:a8:a9:aa:ab:ac:ad:ae:af:b0:b1:b2:b3:b4:b5:b6:b7:b8:b9:ba:bb:bc:bd:be:bf - unexpected continuation byte 0x80 3.2 Lonely start characters -3.2.1 N32 - 64 c0:20:c1:20:c2:20:c3:20:c4:20:c5:20:c6:20:c7:20:c8:20:c9:20:ca:20:cb:20:cc:20:cd:20:ce:20:cf:20:d0:20:d1:20:d2:20:d3:20:d4:20:d5:20:d6:20:d7:20:d8:20:d9:20:da:20:db:20:dc:20:dd:20:de:20:df:20 - unexpected non-continuation byte 0x20, immediately after start byte 0xc0 -3.2.2 N16 - 32 e0:20:e1:20:e2:20:e3:20:e4:20:e5:20:e6:20:e7:20:e8:20:e9:20:ea:20:eb:20:ec:20:ed:20:ee:20:ef:20 - unexpected non-continuation byte 0x20, immediately after start byte 0xe0 -3.2.3 N8 - 16 f0:20:f1:20:f2:20:f3:20:f4:20:f5:20:f6:20:f7:20 - unexpected non-continuation byte 0x20, immediately after start byte 0xf0 -3.2.4 N4 - 8 f8:20:f9:20:fa:20:fb:20 - unexpected non-continuation byte 0x20, immediately after start byte 0xf8 -3.2.5 N2 - 4 fc:20:fd:20 - unexpected non-continuation byte 0x20, immediately after start byte 0xfc +3.2.1 n "\xc0 \xc1 \xc2 \xc3 \xc4 \xc5 \xc6 \xc7 \xc8 \xc9 \xca \xcb \xcc \xcd \xce \xcf \xd0 \xd1 \xd2 \xd3 \xd4 \xd5 \xd6 \xd7 \xd8 \xd9 \xda \xdb \xdc \xdd \xde \xdf " - 64 c0:20:c1:20:c2:20:c3:20:c4:20:c5:20:c6:20:c7:20:c8:20:c9:20:ca:20:cb:20:cc:20:cd:20:ce:20:cf:20:d0:20:d1:20:d2:20:d3:20:d4:20:d5:20:d6:20:d7:20:d8:20:d9:20:da:20:db:20:dc:20:dd:20:de:20:df:20 - unexpected non-continuation byte 0x20 after start byte 0xc0 +3.2.2 n "\xe0 \xe1 \xe2 \xe3 \xe4 \xe5 \xe6 \xe7 \xe8 \xe9 \xea \xeb \xec \xed \xee \xef " - 32 e0:20:e1:20:e2:20:e3:20:e4:20:e5:20:e6:20:e7:20:e8:20:e9:20:ea:20:eb:20:ec:20:ed:20:ee:20:ef:20 - unexpected non-continuation byte 0x20 after start byte 0xe0 +3.2.3 n "\xf0 \xf1 \xf2 \xf3 \xf4 \xf5 \xf6 \xf7 " - 16 f0:20:f1:20:f2:20:f3:20:f4:20:f5:20:f6:20:f7:20 - unexpected non-continuation byte 0x20 after start byte 0xf0 +3.2.4 n "\xf8 \xf9 \xfa \xfb " - 8 f8:20:f9:20:fa:20:fb:20 - unexpected non-continuation byte 0x20 after start byte 0xf8 +3.2.5 n "\xfc \xfd " - 4 fc:20:fd:20 - unexpected non-continuation byte 0x20 after start byte 0xfc 3.3 Sequences with last continuation byte missing -3.3.1 n - 1 c0 - 1 byte, need 2 -3.3.2 n - 2 e0:80 - 2 bytes, need 3 -3.3.3 n - 3 f0:80:80 - 3 bytes, need 4 -3.3.4 n - 4 f8:80:80:80 - 4 bytes, need 5 -3.3.5 n - 5 fc:80:80:80:80 - 5 bytes, need 6 -3.3.6 n - 1 df - 1 byte, need 2 -3.3.7 n - 2 ef:bf - 2 bytes, need 3 -3.3.8 n - 3 f7:bf:bf - 3 bytes, need 4 -3.3.9 n - 4 fb:bf:bf:bf - 4 bytes, need 5 -3.3.10 n - 5 fd:bf:bf:bf:bf - 5 bytes, need 6 +3.3.1 n "\xc0" - 1 c0 - 1 byte, need 2 +3.3.2 n "\xe0\x80" - 2 e0:80 - 2 bytes, need 3 +3.3.3 n "\xf0\x80\x80" - 3 f0:80:80 - 3 bytes, need 4 +3.3.4 n "\xf8\x80\x80\x80" - 4 f8:80:80:80 - 4 bytes, need 5 +3.3.5 n "\xfc\x80\x80\x80\x80" - 5 fc:80:80:80:80 - 5 bytes, need 6 +3.3.6 n "\xdf" - 1 df - 1 byte, need 2 +3.3.7 n "\xef\xbf" - 2 ef:bf - 2 bytes, need 3 +3.3.8 n "\xf7\xbf\xbf" - 3 f7:bf:bf - 3 bytes, need 4 +3.3.9 n "\xfb\xbf\xbf\xbf" - 4 fb:bf:bf:bf - 4 bytes, need 5 +3.3.10 n "\xfd\xbf\xbf\xbf\xbf" - 5 fd:bf:bf:bf:bf - 5 bytes, need 6 3.4 Concatenation of incomplete sequences -3.4.1 N10 - 30 c0:e0:80:f0:80:80:f8:80:80:80:fc:80:80:80:80:df:ef:bf:f7:bf:bf:fb:bf:bf:bf:fd:bf:bf:bf:bf - unexpected non-continuation byte 0xe0, immediately after start byte 0xc0 +3.4.1 n "\xc0\xe0\x80\xf0\x80\x80\xf8\x80\x80\x80\xfc\x80\x80\x80\x80\xdf\xef\xbf\xf7\xbf\xbf\xfb\xbf\xbf\xbf\xfd\xbf\xbf\xbf\xbf" - 30 c0:e0:80:f0:80:80:f8:80:80:80:fc:80:80:80:80:df:ef:bf:f7:bf:bf:fb:bf:bf:bf:fd:bf:bf:bf:bf - unexpected non-continuation byte 0xe0 after start byte 0xc0 3.5 Impossible bytes -3.5.1 n - 1 fe - byte 0xfe -3.5.2 n - 1 ff - byte 0xff -3.5.3 N4 - 4 fe:fe:ff:ff - byte 0xfe +3.5.1 n "\xfe" - 1 fe - byte 0xfe +3.5.2 n "\xff" - 1 ff - byte 0xff +3.5.3 n "\xfe\xfe\xff\xff" - 4 fe:fe:ff:ff - byte 0xfe 4 Overlong sequences 4.1 Examples of an overlong ASCII character -4.1.1 n - 2 c0:af - 2 bytes, need 1 -4.1.2 n - 3 e0:80:af - 3 bytes, need 1 -4.1.3 n - 4 f0:80:80:af - 4 bytes, need 1 -4.1.4 n - 5 f8:80:80:80:af - 5 bytes, need 1 -4.1.5 n - 6 fc:80:80:80:80:af - 6 bytes, need 1 +4.1.1 n "\xc0\xaf" - 2 c0:af - 2 bytes, need 1 +4.1.2 n "\xe0\x80\xaf" - 3 e0:80:af - 3 bytes, need 1 +4.1.3 n "\xf0\x80\x80\xaf" - 4 f0:80:80:af - 4 bytes, need 1 +4.1.4 n "\xf8\x80\x80\x80\xaf" - 5 f8:80:80:80:af - 5 bytes, need 1 +4.1.5 n "\xfc\x80\x80\x80\x80\xaf" - 6 fc:80:80:80:80:af - 6 bytes, need 1 4.2 Maximum overlong sequences -4.2.1 n - 2 c1:bf - 2 bytes, need 1 -4.2.2 n - 3 e0:9f:bf - 3 bytes, need 2 -4.2.3 n - 4 f0:8f:bf:bf - 4 bytes, need 3 -4.2.4 n - 5 f8:87:bf:bf:bf - 5 bytes, need 4 -4.2.5 n - 6 fc:83:bf:bf:bf:bf - 6 bytes, need 5 +4.2.1 n "\xc1\xbf" - 2 c1:bf - 2 bytes, need 1 +4.2.2 n "\xe0\x9f\xbf" - 3 e0:9f:bf - 3 bytes, need 2 +4.2.3 n "\xf0\x8f\xbf\xbf" - 4 f0:8f:bf:bf - 4 bytes, need 3 +4.2.4 n "\xf8\x87\xbf\xbf\xbf" - 5 f8:87:bf:bf:bf - 5 bytes, need 4 +4.2.5 n "\xfc\x83\xbf\xbf\xbf\xbf" - 6 fc:83:bf:bf:bf:bf - 6 bytes, need 5 4.3 Overlong representation of the NUL character -4.3.1 n - 2 c0:80 - 2 bytes, need 1 -4.3.2 n - 3 e0:80:80 - 3 bytes, need 1 -4.3.3 n - 4 f0:80:80:80 - 4 bytes, need 1 -4.3.4 n - 5 f8:80:80:80:80 - 5 bytes, need 1 -4.3.5 n - 6 fc:80:80:80:80:80 - 6 bytes, need 1 +4.3.1 n "\xc0\x80" - 2 c0:80 - 2 bytes, need 1 +4.3.2 n "\xe0\x80\x80" - 3 e0:80:80 - 3 bytes, need 1 +4.3.3 n "\xf0\x80\x80\x80" - 4 f0:80:80:80 - 4 bytes, need 1 +4.3.4 n "\xf8\x80\x80\x80\x80" - 5 f8:80:80:80:80 - 5 bytes, need 1 +4.3.5 n "\xfc\x80\x80\x80\x80\x80" - 6 fc:80:80:80:80:80 - 6 bytes, need 1 5 Illegal code positions 5.1 Single UTF-16 surrogates -5.1.1 y d800 3 ed:a0:80 1 UTF-16 surrogate 0xd800 -5.1.2 y db7f 3 ed:ad:bf 1 UTF-16 surrogate 0xdb7f -5.1.3 y db80 3 ed:ae:80 1 UTF-16 surrogate 0xdb80 -5.1.4 y dbff 3 ed:af:bf 1 UTF-16 surrogate 0xdbff -5.1.5 y dc00 3 ed:b0:80 1 UTF-16 surrogate 0xdc00 -5.1.6 y df80 3 ed:be:80 1 UTF-16 surrogate 0xdf80 -5.1.7 y dfff 3 ed:bf:bf 1 UTF-16 surrogate 0xdfff +5.1.1 n "\xed\xa0\x80" - 3 ed:a0:80 - UTF-16 surrogate 0xd800 +5.1.2 n "\xed\xad\xbf" - 3 ed:ad:bf - UTF-16 surrogate 0xdb7f +5.1.3 n "\xed\xae\x80" - 3 ed:ae:80 - UTF-16 surrogate 0xdb80 +5.1.4 n "\xed\xaf\xbf" - 3 ed:af:bf - UTF-16 surrogate 0xdbff +5.1.5 n "\xed\xb0\x80" - 3 ed:b0:80 - UTF-16 surrogate 0xdc00 +5.1.6 n "\xed\xbe\x80" - 3 ed:be:80 - UTF-16 surrogate 0xdf80 +5.1.7 n "\xed\xbf\xbf" - 3 ed:bf:bf - UTF-16 surrogate 0xdfff 5.2 Paired UTF-16 surrogates -5.2.1 y d800,dc00 6 ed:a0:80:ed:b0:80 2 UTF-16 surrogates 0xd800, dc00 -5.2.2 y d800,dfff 6 ed:a0:80:ed:bf:bf 2 UTF-16 surrogates 0xd800, dfff -5.2.3 y db7f,dc00 6 ed:ad:bf:ed:b0:80 2 UTF-16 surrogates 0xdb7f, dc00 -5.2.4 y db7f,dfff 6 ed:ad:bf:ed:bf:bf 2 UTF-16 surrogates 0xdb7f, dfff -5.2.5 y db80,dc00 6 ed:ae:80:ed:b0:80 2 UTF-16 surrogates 0xdb80, dc00 -5.2.6 y db80,dfff 6 ed:ae:80:ed:bf:bf 2 UTF-16 surrogates 0xdb80, dfff -5.2.7 y dbff,dc00 6 ed:af:bf:ed:b0:80 2 UTF-16 surrogates 0xdbff, dc00 -5.2.8 y dbff,dfff 6 ed:af:bf:ed:bf:bf 2 UTF-16 surrogates 0xdbff, dfff +5.2.1 n "\xed\xa0\x80\xed\xb0\x80" - 6 ed:a0:80:ed:b0:80 - UTF-16 surrogate 0xd800 +5.2.2 n "\xed\xa0\x80\xed\xbf\xbf" - 6 ed:a0:80:ed:bf:bf - UTF-16 surrogate 0xd800 +5.2.3 n "\xed\xad\xbf\xed\xb0\x80" - 6 ed:ad:bf:ed:b0:80 - UTF-16 surrogate 0xdb7f +5.2.4 n "\xed\xad\xbf\xed\xbf\xbf" - 6 ed:ad:bf:ed:bf:bf - UTF-16 surrogate 0xdb7f +5.2.5 n "\xed\xae\x80\xed\xb0\x80" - 6 ed:ae:80:ed:b0:80 - UTF-16 surrogate 0xdb80 +5.2.6 n "\xed\xae\x80\xed\xbf\xbf" - 6 ed:ae:80:ed:bf:bf - UTF-16 surrogate 0xdb80 +5.2.7 n "\xed\xaf\xbf\xed\xb0\x80" - 6 ed:af:bf:ed:b0:80 - UTF-16 surrogate 0xdbff +5.2.8 n "\xed\xaf\xbf\xed\xbf\xbf" - 6 ed:af:bf:ed:bf:bf - UTF-16 surrogate 0xdbff 5.3 Other illegal code positions -5.3.1 y fffe 3 ef:bf:be 1 byte order mark 0xfffe -# The ffff is legal by default since 872c91ae155f6880 -5.3.2 y ffff 3 ef:bf:bf 1 character 0xffff +5.3.1 n "\xef\xbf\xbe" - 3 ef:bf:be - byte order mark 0xfffe +# The ffff is illegal unless UTF8_ALLOW_FFFF +5.3.2 n "\xef\xbf\xbf" - 3 ef:bf:bf - character 0xffff +__EOMK__ + +# 104..181 +{ + my $WARNCNT; + my $id; + + local $SIG{__WARN__} = + sub { + print "# $id: @_"; + $WARNCNT++; + $WARNMSG = "@_"; + }; + + sub moan { + print "$id: @_"; + } + + sub test_unpack_U { + $WARNCNT = 0; + $WARNMSG = ""; + unpack('U*', $_[0]); + } + + for (@MK) { + if (/^(?:\d+(?:\.\d+)?)\s/ || /^#/) { + # print "# $_\n"; + } elsif (/^(\d+\.\d+\.\d+[bu]?)\s+([yn])\s+"(.+)"\s+([0-9a-f]{1,8}|-)\s+(\d+)\s+([0-9a-f]{2}(?::[0-9a-f]{2})*)(?:\s+((?:\d+|-)(?:\s+(.+))?))?$/) { + $id = $1; + my ($okay, $bytes, $Unicode, $byteslen, $hex, $charslen, $error) = + ($2, $3, $4, $5, $6, $7, $8); + my @hex = split(/:/, $hex); + unless (@hex == $byteslen) { + my $nhex = @hex; + moan "amount of hex ($nhex) not equal to byteslen ($byteslen)\n"; + } + { + use bytes; + my $bytesbyteslen = length($bytes); + unless ($bytesbyteslen == $byteslen) { + moan "bytes length() ($bytesbyteslen) not equal to $byteslen\n"; + } + } + if ($okay eq 'y') { + test_unpack_U($bytes); + if ($WARNCNT) { + moan "unpack('U*') false negative\n"; + print "not "; + } + } elsif ($okay eq 'n') { + test_unpack_U($bytes); + if ($WARNCNT == 0 || ($error ne '' && $WARNMSG !~ /$error/)) { + moan "unpack('U*') false positive\n"; + print "not "; + } + } + print "ok $test\n"; + $test++; + } else { + moan "unknown format\n"; + } + } +} diff --git a/gnu/usr.bin/perl/t/run/runenv.t b/gnu/usr.bin/perl/t/run/runenv.t index 9380d2400ae..a59ad26f35c 100644 --- a/gnu/usr.bin/perl/t/run/runenv.t +++ b/gnu/usr.bin/perl/t/run/runenv.t @@ -8,81 +8,81 @@ BEGIN { chdir 't' if -d 't'; @INC = '../lib'; require Config; import Config; - require './test.pl'; - skip_all_without_config('d_fork'); + unless ($Config{'d_fork'}) { + print "1..0 # Skip: no fork\n"; + exit 0; + } } -plan tests => 104; - -my $STDOUT = tempfile(); -my $STDERR = tempfile(); +my $STDOUT = './results-0'; +my $STDERR = './results-1'; my $PERL = './perl'; my $FAILURE_CODE = 119; -delete $ENV{PERLLIB}; -delete $ENV{PERL5LIB}; -delete $ENV{PERL5OPT}; +print "1..9\n"; - -# Run perl with specified environment and arguments, return (STDOUT, STDERR) -sub runperl_and_capture { +# Run perl with specified environment and arguments returns a list. +# First element is true iff Perl's stdout and stderr match the +# supplied $stdout and $stderr argument strings exactly. +# second element is an explanation of the failure +sub runperl { local *F; - my ($env, $args) = @_; + my ($env, $args, $stdout, $stderr) = @_; - local %ENV = %ENV; - delete $ENV{PERLLIB}; - delete $ENV{PERL5LIB}; - delete $ENV{PERL5OPT}; + unshift @$args, '-I../lib'; + + $stdout = '' unless defined $stdout; + $stderr = '' unless defined $stderr; my $pid = fork; return (0, "Couldn't fork: $!") unless defined $pid; # failure if ($pid) { # parent + my ($actual_stdout, $actual_stderr); wait; return (0, "Failure in child.\n") if ($?>>8) == $FAILURE_CODE; - open my $stdout, '<', $STDOUT - or return (0, "Couldn't read $STDOUT file: $!"); - open my $stderr, '<', $STDERR - or return (0, "Couldn't read $STDERR file: $!"); - local $/; - # Empty file with <$stderr> returns nothing in list context - # (because there are no lines) Use scalar to force it to '' - return (scalar <$stdout>, scalar <$stderr>); + open F, "< $STDOUT" or return (0, "Couldn't read $STDOUT file"); + { local $/; $actual_stdout = <F> } + open F, "< $STDERR" or return (0, "Couldn't read $STDERR file"); + { local $/; $actual_stderr = <F> } + + if ($actual_stdout ne $stdout) { + return (0, "Stdout mismatch: expected [$stdout], saw [$actual_stdout]"); + } elsif ($actual_stderr ne $stderr) { + return (0, "Stderr mismatch: expected [$stderr], saw [$actual_stderr]"); + } else { + return 1; # success + } } else { # child for my $k (keys %$env) { $ENV{$k} = $env->{$k}; } - open STDOUT, '>', $STDOUT or exit $FAILURE_CODE; - open STDERR, '>', $STDERR and do { exec $PERL, @$args }; - # it did not work: + open STDOUT, "> $STDOUT" or exit $FAILURE_CODE; + open STDERR, "> $STDERR" or it_didnt_work(); + { exec $PERL, @$args } + it_didnt_work(); + } +} + + +sub it_didnt_work { print STDOUT "IWHCWJIHCI\cNHJWCJQWKJQJWCQW\n"; exit $FAILURE_CODE; - } } sub try { - my ($env, $args, $stdout, $stderr) = @_; - my ($actual_stdout, $actual_stderr) = runperl_and_capture($env, $args); - local $::Level = $::Level + 1; - my @envpairs = (); - for my $k (sort keys %$env) { - push @envpairs, "$k => $env->{$k}"; - } - my $label = join(',' => (@envpairs, @$args)); - if (ref $stdout) { - ok ( $actual_stdout =~/$stdout/, $label . ' stdout' ); - } else { - is ( $actual_stdout, $stdout, $label . ' stdout' ); - } - if (ref $stderr) { - ok ( $actual_stderr =~/$stderr/, $label . ' stderr' ); + my $testno = shift; + my ($success, $reason) = runperl(@_); + if ($success) { + print "ok $testno\n"; } else { - is ( $actual_stderr, $stderr, $label . ' stderr' ); + $reason =~ s/\n/\\n/g; + print "not ok $testno # $reason\n"; } } # PERL5OPT Command-line options (switches). Switches in # this variable are taken as if they were on -# every Perl command line. Only the -[DIMUdmtw] +# every Perl command line. Only the -[DIMUdmw] # switches are allowed. When running taint # checks (because the program was running setuid # or setgid, or the -T switch was used), this @@ -90,233 +90,58 @@ sub try { # -T, tainting will be enabled, and any # subsequent options ignored. -try({PERL5OPT => '-w'}, ['-e', 'print $::x'], +my $T = 1; +try($T++, {PERL5OPT => '-w'}, ['-e', 'print $::x'], "", - qq{Name "main::x" used only once: possible typo at -e line 1.\nUse of uninitialized value \$x in print at -e line 1.\n}); + qq{Name "main::x" used only once: possible typo at -e line 1.\nUse of uninitialized value in print at -e line 1.\n}); -try({PERL5OPT => '-Mstrict'}, ['-I../lib', '-e', 'print $::x'], +try($T++, {PERL5OPT => '-Mstrict'}, ['-e', 'print $::x'], "", ""); -try({PERL5OPT => '-Mstrict'}, ['-I../lib', '-e', 'print $x'], +try($T++, {PERL5OPT => '-Mstrict'}, ['-e', 'print $x'], "", qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n}); # Fails in 5.6.0 -try({PERL5OPT => '-Mstrict -w'}, ['-I../lib', '-e', 'print $x'], +try($T++, {PERL5OPT => '-Mstrict -w'}, ['-e', 'print $x'], "", qq{Global symbol "\$x" requires explicit package name at -e line 1.\nExecution of -e aborted due to compilation errors.\n}); # Fails in 5.6.0 -try({PERL5OPT => '-w -Mstrict'}, ['-I../lib', '-e', 'print $::x'], +try($T++, {PERL5OPT => '-w -Mstrict'}, ['-e', 'print $::x'], "", <<ERROR Name "main::x" used only once: possible typo at -e line 1. -Use of uninitialized value \$x in print at -e line 1. +Use of uninitialized value in print at -e line 1. ERROR ); # Fails in 5.6.0 -try({PERL5OPT => '-w -Mstrict'}, ['-I../lib', '-e', 'print $::x'], +try($T++, {PERL5OPT => '-w -Mstrict'}, ['-e', 'print $::x'], "", <<ERROR Name "main::x" used only once: possible typo at -e line 1. -Use of uninitialized value \$x in print at -e line 1. +Use of uninitialized value in print at -e line 1. ERROR ); -try({PERL5OPT => '-MExporter'}, ['-I../lib', '-e0'], +try($T++, {PERL5OPT => '-MExporter'}, ['-e0'], "", ""); # Fails in 5.6.0 -try({PERL5OPT => '-MExporter -MExporter'}, ['-I../lib', '-e0'], +try($T++, {PERL5OPT => '-MExporter -MExporter'}, ['-e0'], "", ""); -try({PERL5OPT => '-Mstrict -Mwarnings'}, - ['-I../lib', '-e', 'print "ok" if $INC{"strict.pm"} and $INC{"warnings.pm"}'], +try($T++, {PERL5OPT => '-Mstrict -Mwarnings'}, + ['-e', 'print "ok" if $INC{"strict.pm"} and $INC{"warnings.pm"}'], "ok", ""); -open my $fh, ">", "Oooof.pm" or die "Can't write Oooof.pm: $!"; -print $fh "package Oooof; 1;\n"; -close $fh; -END { 1 while unlink "Oooof.pm" } - -try({PERL5OPT => '-I. -MOooof'}, - ['-e', 'print "ok" if $INC{"Oooof.pm"} eq "Oooof.pm"'], - "ok", - ""); - -try({PERL5OPT => '-I./ -MOooof'}, - ['-e', 'print "ok" if $INC{"Oooof.pm"} eq "Oooof.pm"'], - "ok", - ""); - -try({PERL5OPT => '-w -w'}, - ['-e', 'print $ENV{PERL5OPT}'], - '-w -w', - ''); - -try({PERL5OPT => '-t'}, - ['-e', 'print ${^TAINT}'], - '-1', - ''); - -try({PERL5OPT => '-W'}, - ['-I../lib','-e', 'local $^W = 0; no warnings; print $x'], - '', - <<ERROR -Name "main::x" used only once: possible typo at -e line 1. -Use of uninitialized value \$x in print at -e line 1. -ERROR -); - -try({PERLLIB => "foobar$Config{path_sep}42"}, - ['-e', 'print grep { $_ eq "foobar" } @INC'], - 'foobar', - ''); - -try({PERLLIB => "foobar$Config{path_sep}42"}, - ['-e', 'print grep { $_ eq "42" } @INC'], - '42', - ''); +print "# ", $T-1, " tests total.\n"; -try({PERL5LIB => "foobar$Config{path_sep}42"}, - ['-e', 'print grep { $_ eq "foobar" } @INC'], - 'foobar', - ''); - -try({PERL5LIB => "foobar$Config{path_sep}42"}, - ['-e', 'print grep { $_ eq "42" } @INC'], - '42', - ''); - -try({PERL5LIB => "foo", - PERLLIB => "bar"}, - ['-e', 'print grep { $_ eq "foo" } @INC'], - 'foo', - ''); - -try({PERL5LIB => "foo", - PERLLIB => "bar"}, - ['-e', 'print grep { $_ eq "bar" } @INC'], - '', - ''); - -try({PERL_HASH_SEED_DEBUG => 1}, - ['-e','1'], - '', - qr/HASH_FUNCTION =/); - -try({PERL_HASH_SEED_DEBUG => 1}, - ['-e','1'], - '', - qr/HASH_SEED =/); - -# special case, seed "0" implies disabled hash key traversal randomization -try({PERL_HASH_SEED_DEBUG => 1, PERL_HASH_SEED => "0"}, - ['-e','1'], - '', - qr/PERTURB_KEYS = 0/); - -# check that setting it to a different value with the same logical value -# triggers the normal "deterministic mode". -try({PERL_HASH_SEED_DEBUG => 1, PERL_HASH_SEED => "0x0"}, - ['-e','1'], - '', - qr/PERTURB_KEYS = 2/); - -try({PERL_HASH_SEED_DEBUG => 1, PERL_PERTURB_KEYS => "0"}, - ['-e','1'], - '', - qr/PERTURB_KEYS = 0/); - -try({PERL_HASH_SEED_DEBUG => 1, PERL_PERTURB_KEYS => "1"}, - ['-e','1'], - '', - qr/PERTURB_KEYS = 1/); - -try({PERL_HASH_SEED_DEBUG => 1, PERL_PERTURB_KEYS => "2"}, - ['-e','1'], - '', - qr/PERTURB_KEYS = 2/); - -try({PERL_HASH_SEED_DEBUG => 1, PERL_HASH_SEED => "12345678"}, - ['-e','1'], - '', - qr/HASH_SEED = 0x12345678/); - -try({PERL_HASH_SEED_DEBUG => 1, PERL_HASH_SEED => "12"}, - ['-e','1'], - '', - qr/HASH_SEED = 0x12000000/); - -try({PERL_HASH_SEED_DEBUG => 1, PERL_HASH_SEED => "123456789"}, - ['-e','1'], - '', - qr/HASH_SEED = 0x12345678/); - -# Test that PERL_PERTURB_KEYS works as expected. We check that we get the same -# results if we use PERL_PERTURB_KEYS = 0 or 2 and we reuse the seed from previous run. -my @print_keys = ( '-e', '@_{"A".."Z"}=(); print keys %_'); -for my $mode ( 0,1, 2 ) { # disabled and deterministic respectively - my %base_opts = ( PERL_PERTURB_KEYS => $mode, PERL_HASH_SEED_DEBUG => 1 ), - my ($out, $err) = runperl_and_capture( { %base_opts }, [ @print_keys ]); - if ($err=~/HASH_SEED = (0x[a-f0-9]+)/) { - my $seed = $1; - my($out2, $err2) = runperl_and_capture( { %base_opts, PERL_HASH_SEED => $seed }, [ @print_keys ]); - if ( $mode == 1 ) { - isnt ($out,$out2,"PERL_PERTURB_KEYS = $mode results in different key order with the same key"); - } else { - is ($out,$out2,"PERL_PERTURB_KEYS = $mode allows one to recreate a random hash"); - } - is ($err,$err2,"Got the same debug output when we set PERL_HASH_SEED and PERL_PERTURB_KEYS"); - } +END { + 1 while unlink $STDOUT; + 1 while unlink $STDERR; } - -# Tests for S_incpush_use_sep(): - -my @dump_inc = ('-e', 'print "$_\n" foreach @INC'); - -my ($out, $err) = runperl_and_capture({}, [@dump_inc]); - -is ($err, '', 'No errors when determining @INC'); - -my @default_inc = split /\n/, $out; - -is ($default_inc[-1], '.', '. is last in @INC'); - -my $sep = $Config{path_sep}; -foreach (['nothing', ''], - ['something', 'zwapp', 'zwapp'], - ['two things', "zwapp${sep}bam", 'zwapp', 'bam'], - ['two things, ::', "zwapp${sep}${sep}bam", 'zwapp', 'bam'], - [': at start', "${sep}zwapp", 'zwapp'], - [': at end', "zwapp${sep}", 'zwapp'], - [':: sandwich ::', "${sep}${sep}zwapp${sep}${sep}", 'zwapp'], - [':', "${sep}"], - ['::', "${sep}${sep}"], - [':::', "${sep}${sep}${sep}"], - ['two things and :', "zwapp${sep}bam${sep}", 'zwapp', 'bam'], - [': and two things', "${sep}zwapp${sep}bam", 'zwapp', 'bam'], - [': two things :', "${sep}zwapp${sep}bam${sep}", 'zwapp', 'bam'], - ['three things', "zwapp${sep}bam${sep}${sep}owww", - 'zwapp', 'bam', 'owww'], - ) { - my ($name, $lib, @expect) = @$_; - push @expect, @default_inc; - - ($out, $err) = runperl_and_capture({PERL5LIB => $lib}, [@dump_inc]); - - is ($err, '', "No errors when determining \@INC for $name"); - - my @inc = split /\n/, $out; - - is (scalar @inc, scalar @expect, - "expected number of elements in \@INC for $name"); - - is ("@inc", "@expect", "expected elements in \@INC for $name"); -} - -# PERL5LIB tests with included arch directories still missing |