1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244 1245 1246 1247 1248 1249 1250 1251 1252 1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269 1270 1271 1272 1273 1274 1275 1276 1277 1278 1279 1280 1281 1282
|
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
"http://www.w3.org/TR/html4/loose.dtd">
<html>
<head>
<title>perltrap - perldoc.perl.org</title>
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
<meta http-equiv="Content-Language" content="en-gb">
<link rel="search" type="application/opensearchdescription+xml" title="Search perldoc.perl.org" href="/static/search.xml"/>
<link href="static/css-20100830.css" rel="stylesheet" rev="stylesheet" type="text/css" media="screen">
<link href="static/exploreperl.css" rel="stylesheet" rev="stylesheet" type="text/css">
</head>
<body onLoad="perldoc.startup();" onPageShow="if (event.persisted) perldoc.startup();">
<div id="page">
<div id="header">
<div id="homepage_link">
<a href="index.html"></a>
</div>
<div id="strapline">
Perl Programming Documentation
</div>
<div id="download_link" class="download">
<a href="http://www.perl.org/get.html">Download Perl</a>
</div>
<div id="explore_link" class="download">
<a id="explore_anchor" href="#">Explore</a>
</div>
</div>
<div id="body">
<div id="left_column">
<div class="side_group">
<div class="side_panel doc_panel">
<p>Manual</p>
<ul>
<li><a href="index-overview.html">Overview</a>
<li><a href="index-tutorials.html">Tutorials</a>
<li><a href="index-faq.html">FAQs</a>
<li><a href="index-history.html">History / Changes</a>
<li><a href="index-licence.html">License</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Reference</p>
<ul>
<li><a href="index-language.html">Language</a>
<li><a href="index-functions.html">Functions</a>
<li><a href="perlop.html">Operators</a>
<li><a href="perlvar.html">Special Variables</a>
<li><a href="index-pragmas.html">Pragmas</a>
<li><a href="index-utilities.html">Utilities</a>
<li><a href="index-internals.html">Internals</a>
<li><a href="index-platforms.html">Platform Specific</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Modules</p>
<ul>
<li>
<a href="index-modules-A.html">A</a>
•
<a href="index-modules-B.html">B</a>
•
<a href="index-modules-C.html">C</a>
•
<a href="index-modules-D.html">D</a>
•
<a href="index-modules-E.html">E</a>
<li>
<a href="index-modules-F.html">F</a>
•
<a href="index-modules-G.html">G</a>
•
<a href="index-modules-H.html">H</a>
•
<a href="index-modules-I.html">I</a>
•
<a href="index-modules-L.html">L</a>
<li>
<a href="index-modules-M.html">M</a>
•
<a href="index-modules-N.html">N</a>
•
<a href="index-modules-O.html">O</a>
•
<a href="index-modules-P.html">P</a>
•
<a href="index-modules-S.html">S</a>
<li>
<a href="index-modules-T.html">T</a>
•
<a href="index-modules-U.html">U</a>
•
<a href="index-modules-X.html">X</a>
</ul>
</div>
<div class="side_panel doc_panel">
<p>Tools</p>
<ul>
<li><a href="preferences.html">Preferences</a>
</ul>
</div>
</div>
</div>
<div id="centre_column">
<div id="content_header">
<div id="title_bar">
<div id="page_name">
<h1>perltrap</h1>
</div>
<div id="perl_version">
Perl 5 version 14.0 documentation
</div>
<div class="page_links" id="page_links_top">
<a href="#" onClick="toolbar.goToTop();return false;">Go to top</a>
</div>
<div class="page_links" id="page_links_bottom">
<a href="#" id="page_index_toggle">Show page index</a> •
<a href="#" id="recent_pages_toggle">Show recent pages</a>
</div>
<div id="search_form">
<form action="search.html" method="GET" id="search">
<input type="text" name="q" id="search_box" alt="Search">
</form>
</div>
</div>
<div id="breadcrumbs">
<a href="index.html">Home</a> >
<a href="index-tutorials.html">Tutorials</a> >
perltrap
</div>
</div>
<div id="content_body">
<!--[if lt IE 7]>
<div class="noscript">
<p>
<strong>It looks like you're using Internet Explorer 6. This is a very old
browser which does not offer full support for modern websites.</strong>
</p>
<p>
Unfortunately this means that this website will not work on
your computer.
</p>
<p>
Don't miss out though! To view the site (and get a better experience from
many other websites), simply upgrade to
<a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet
Explorer 8</a>
or download an alternative browser such as
<a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>,
<a href="http://www.apple.com/safari/download/">Safari</a>, or
<a href="http://www.google.co.uk/chrome">Google Chrome</a>.
</p>
<p>
All of these browsers are free. If you're using a PC at work, you may
need to contact your IT administrator.
</p>
</div>
<![endif]-->
<noscript>
<div class="noscript">
<p>
<strong>Please note: Many features of this site require JavaScript. You appear to have JavaScript disabled,
or are running a non-JavaScript capable web browser.</strong>
</p>
<p>
To get the best experience, please enable JavaScript or download a modern web browser such as <a href="http://www.microsoft.com/windows/Internet-explorer/default.aspx">Internet Explorer 8</a>, <a href="http://www.mozilla.com/en-US/firefox/firefox.html">Firefox</a>, <a href="http://www.apple.com/safari/download/">Safari</a>, or <a href="http://www.google.co.uk/chrome">Google Chrome</a>.
</p>
</div>
</noscript>
<div id="recent_pages" class="hud_container">
<div id="recent_pages_header" class="hud_header">
<div id="recent_pages_close" class="hud_close"><a href="#" onClick="recentPages.hide();return false;"></a></div>
<div id="recent_pages_title" class="hud_title"><span class="hud_span_top">Recently read</span></div>
<div id="recent_pages_topright" class="hud_topright"></div>
</div>
<div id="recent_pages_content" class="hud_content">
</div>
<div id="recent_pages_footer" class="hud_footer">
<div id="recent_pages_bottomleft" class="hud_bottomleft"></div>
<div id="recent_pages_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
<div id="recent_pages_resize" class="hud_resize"></div>
</div>
</div>
<div id="from_search"></div>
<h1>perltrap</h1>
<!-- -->
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Awk-Traps">Awk Traps</a><li><a href="#C%2fC%2b%2b-Traps">C/C++ Traps</a><li><a href="#Sed-Traps">Sed Traps</a><li><a href="#Shell-Traps">Shell Traps</a><li><a href="#Perl-Traps">Perl Traps</a><li><a href="#Perl4-to-Perl5-Traps">Perl4 to Perl5 Traps</a><li><a href="#Discontinuance%2c-Deprecation%2c-and-BugFix-traps">Discontinuance, Deprecation, and BugFix traps</a><li><a href="#Parsing-Traps">Parsing Traps</a><li><a href="#Numerical-Traps">Numerical Traps</a><li><a href="#General-data-type-traps">General data type traps</a><li><a href="#Context-Traps---scalar%2c-list-contexts">Context Traps - scalar, list contexts</a><li><a href="#Precedence-Traps">Precedence Traps</a><li><a href="#General-Regular-Expression-Traps-using-s%2f%2f%2f%2c-etc.">General Regular Expression Traps using s///, etc.</a><li><a href="#Subroutine%2c-Signal%2c-Sorting-Traps">Subroutine, Signal, Sorting Traps</a><li><a href="#OS-Traps">OS Traps</a><li><a href="#Interpolation-Traps">Interpolation Traps</a><li><a href="#DBM-Traps">DBM Traps</a><li><a href="#Unclassified-Traps">Unclassified Traps</a></ul></ul><a name="NAME"></a><h1>NAME</h1>
<p>perltrap - Perl traps for the unwary</p>
<a name="DESCRIPTION"></a><h1>DESCRIPTION</h1>
<p>The biggest trap of all is forgetting to <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
or use the <b>-w</b>
switch; see <a href="perllexwarn.html">perllexwarn</a> and <a href="perlrun.html">perlrun</a>. The second biggest trap is not
making your entire program runnable under <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span></code>
. The third biggest
trap is not reading the list of changes in this version of Perl; see
<a href="perldelta.html">perldelta</a>.</p>
<a name="Awk-Traps"></a><h2>Awk Traps</h2>
<p>Accustomed <b>awk</b> users should take special note of the following:</p>
<ul>
<li>
<p>A Perl program executes only once, not once for each input line. You can
do an implicit loop with <code class="inline">-<span class="w">n</span></code>
or <code class="inline">-p</code>
.</p>
</li>
<li>
<p>The English module, loaded via</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">English</span><span class="sc">;</span></li></ol></pre><p>allows you to refer to special variables (like <code class="inline"><span class="i">$/</span></code>
) with names (like
$RS), as though they were in <b>awk</b>; see <a href="perlvar.html">perlvar</a> for details.</p>
</li>
<li>
<p>Semicolons are required after all simple statements in Perl (except
at the end of a block). Newline is not a statement delimiter.</p>
</li>
<li>
<p>Curly brackets are required on <code class="inline">if</code>
s and <code class="inline">while</code>
s.</p>
</li>
<li>
<p>Variables begin with "$", "@" or "%" in Perl.</p>
</li>
<li>
<p>Arrays index from 0. Likewise string positions in substr() and
index().</p>
</li>
<li>
<p>You have to decide whether your array has numeric or string indices.</p>
</li>
<li>
<p>Hash values do not spring into existence upon mere reference.</p>
</li>
<li>
<p>You have to decide whether you want to use string or numeric
comparisons.</p>
</li>
<li>
<p>Reading an input line does not split it for you. You get to split it
to an array yourself. And the split() operator has different
arguments than <b>awk</b>'s.</p>
</li>
<li>
<p>The current input line is normally in $_, not $0. It generally does
not have the newline stripped. ($0 is the name of the program
executed.) See <a href="perlvar.html">perlvar</a>.</p>
</li>
<li>
<p>$<<i>digit</i>> does not refer to fields--it refers to substrings matched
by the last match pattern.</p>
</li>
<li>
<p>The print() statement does not add field and record separators unless
you set <code class="inline"><span class="i">$,</span></code>
and <code class="inline"><span class="i">$\</span></code>
. You can set $OFS and $ORS if you're using
the English module.</p>
</li>
<li>
<p>You must open your files before you print to them.</p>
</li>
<li>
<p>The range operator is "..", not comma. The comma operator works as in
C.</p>
</li>
<li>
<p>The match operator is "=~", not "~". ("~" is the one's complement
operator, as in C.)</p>
</li>
<li>
<p>The exponentiation operator is "**", not "^". "^" is the XOR
operator, as in C. (You know, one could get the feeling that <b>awk</b> is
basically incompatible with C.)</p>
</li>
<li>
<p>The concatenation operator is ".", not the null string. (Using the
null string would render <code class="inline"><span class="q">/pat/</span> /<span class="w">pat</span>/</code>
unparsable, because the third slash
would be interpreted as a division operator--the tokenizer is in fact
slightly context sensitive for operators like "/", "?", and ">".
And in fact, "." itself can be the beginning of a number.)</p>
</li>
<li>
<p>The <code class="inline"><a class="l_k" href="functions/next.html">next</a></code>, <code class="inline"><a class="l_k" href="functions/exit.html">exit</a></code>, and <code class="inline"><a class="l_k" href="functions/continue.html">continue</a></code> keywords work differently.</p>
</li>
<li>
<p>The following variables work differently:</p>
<pre class="verbatim"><ol><li> Awk Perl</li><li> ARGC scalar @ARGV (compare with $#ARGV)</li><li> ARGV[0] $0</li><li> FILENAME $ARGV</li><li> FNR $. - something</li><li> FS (whatever you like)</li><li> NF $#Fld, or some such</li><li> NR $.</li><li> OFMT $#</li><li> OFS $,</li><li> ORS $\</li><li> RLENGTH length($&)</li><li> RS $/</li><li> RSTART length($`)</li><li> SUBSEP $;</li></ol></pre></li>
<li>
<p>You cannot set $RS to a pattern, only a string.</p>
</li>
<li>
<p>When in doubt, run the <b>awk</b> construct through <b>a2p</b> and see what it
gives you.</p>
</li>
</ul>
<a name="C%2fC%2b%2b-Traps"></a><h2>C/C++ Traps</h2>
<p>Cerebral C and C++ programmers should take note of the following:</p>
<ul>
<li>
<p>Curly brackets are required on <code class="inline">if</code>
's and <code class="inline">while</code>
's.</p>
</li>
<li>
<p>You must use <code class="inline">elsif</code>
rather than <code class="inline">else if</code>
.</p>
</li>
<li>
<p>The <code class="inline"><span class="w">break</span></code>
and <code class="inline"><a class="l_k" href="functions/continue.html">continue</a></code> keywords from C become in Perl <code class="inline"><a class="l_k" href="functions/last.html">last</a></code>
and <code class="inline"><a class="l_k" href="functions/next.html">next</a></code>, respectively. Unlike in C, these do <i>not</i> work within a
<code class="inline"><a class="l_k" href="functions/do.html">do</a> <span class="s">{</span> <span class="s">}</span> while</code>
construct. See <a href="perlsyn.html#Loop-Control">Loop Control in perlsyn</a>.</p>
</li>
<li>
<p>The switch statement is called <code class="inline">given/when</code> and only available in
perl 5.10 or newer. See <a href="perlsyn.html#Switch-statements">Switch statements in perlsyn</a>.</p>
</li>
<li>
<p>Variables begin with "$", "@" or "%" in Perl.</p>
</li>
<li>
<p>Comments begin with "#", not "/*" or "//". Perl may interpret C/C++
comments as division operators, unterminated regular expressions or
the defined-or operator.</p>
</li>
<li>
<p>You can't take the address of anything, although a similar operator
in Perl is the backslash, which creates a reference.</p>
</li>
<li>
<p><code class="inline"><span class="w">ARGV</span></code>
must be capitalized. <code class="inline"><span class="i">$ARGV</span>[<span class="n">0</span>]</code>
is C's <code class="inline"><span class="w">argv</span><span class="s">[</span><span class="n">1</span><span class="s">]</span></code>
, and <code class="inline"><span class="w">argv</span><span class="s">[</span><span class="n">0</span><span class="s">]</span></code>
ends up in <code class="inline"><span class="i">$0</span></code>
.</p>
</li>
<li>
<p>System calls such as link(), unlink(), rename(), etc. return nonzero for
success, not 0. (system(), however, returns zero for success.)</p>
</li>
<li>
<p>Signal handlers deal with signal names, not numbers. Use <code class="inline"><a class="l_k" href="functions/kill.html">kill</a> -l</code>
to find their names on your system.</p>
</li>
</ul>
<a name="Sed-Traps"></a><h2>Sed Traps</h2>
<p>Seasoned <b>sed</b> programmers should take note of the following:</p>
<ul>
<li>
<p>A Perl program executes only once, not once for each input line. You can
do an implicit loop with <code class="inline">-<span class="w">n</span></code>
or <code class="inline">-p</code>
.</p>
</li>
<li>
<p>Backreferences in substitutions use "$" rather than "\".</p>
</li>
<li>
<p>The pattern matching metacharacters "(", ")", and "|" do not have backslashes
in front.</p>
</li>
<li>
<p>The range operator is <code class="inline">...</code>
, rather than comma.</p>
</li>
</ul>
<a name="Shell-Traps"></a><h2>Shell Traps</h2>
<p>Sharp shell programmers should take note of the following:</p>
<ul>
<li>
<p>The backtick operator does variable interpolation without regard to
the presence of single quotes in the command.</p>
</li>
<li>
<p>The backtick operator does no translation of the return value, unlike <b>csh</b>.</p>
</li>
<li>
<p>Shells (especially <b>csh</b>) do several levels of substitution on each
command line. Perl does substitution in only certain constructs
such as double quotes, backticks, angle brackets, and search patterns.</p>
</li>
<li>
<p>Shells interpret scripts a little bit at a time. Perl compiles the
entire program before executing it (except for <code class="inline">BEGIN</code>
blocks, which
execute at compile time).</p>
</li>
<li>
<p>The arguments are available via @ARGV, not $1, $2, etc.</p>
</li>
<li>
<p>The environment is not automatically made available as separate scalar
variables.</p>
</li>
<li>
<p>The shell's <code class="inline"><span class="w">test</span></code>
uses "=", "!=", "<" etc for string comparisons and "-eq",
"-ne", "-lt" etc for numeric comparisons. This is the reverse of Perl, which
uses <code class="inline">eq</code>
, <code class="inline">ne</code>
, <code class="inline">lt</code>
for string comparisons, and <code class="inline">==</code>
, <code class="inline">!=</code>
<code class="inline"><</code>
etc
for numeric comparisons.</p>
</li>
</ul>
<a name="Perl-Traps"></a><h2>Perl Traps</h2>
<p>Practicing Perl Programmers should take note of the following:</p>
<ul>
<li>
<p>Remember that many operations behave differently in a list
context than they do in a scalar one. See <a href="perldata.html">perldata</a> for details.</p>
</li>
<li>
<p>Avoid barewords if you can, especially all lowercase ones.
You can't tell by just looking at it whether a bareword is
a function or a string. By using quotes on strings and
parentheses on function calls, you won't ever get them confused.</p>
</li>
<li>
<p>You cannot discern from mere inspection which builtins
are unary operators (like chop() and chdir())
and which are list operators (like print() and unlink()).
(Unless prototyped, user-defined subroutines can <b>only</b> be list
operators, never unary ones.) See <a href="perlop.html">perlop</a> and <a href="perlsub.html">perlsub</a>.</p>
</li>
<li>
<p>People have a hard time remembering that some functions
default to $_, or @ARGV, or whatever, but that others which
you might expect to do not.</p>
</li>
<li>
<p>The <FH> construct is not the name of the filehandle, it is a readline
operation on that handle. The data read is assigned to $_ only if the
file read is the sole condition in a while loop:</p>
<pre class="verbatim"><ol><li> while <span class="s">(</span><span class="q"><FH></span><span class="s">)</span> <span class="s">{</span> <span class="s">}</span></li><li> while <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$_</span> = <span class="q"><FH></span><span class="s">)</span><span class="s">)</span> <span class="s">{</span> <span class="s">}</span>..</li><li> <span class="q"><FH></span><span class="sc">;</span> <span class="c"># data discarded!</span></li></ol></pre></li>
<li>
<p>Remember not to use <code class="inline">=</code>
when you need <code class="inline">=~</code>
;
these two constructs are quite different:</p>
<pre class="verbatim"><ol><li> <span class="i">$x</span> = <span class="q">/foo/</span><span class="sc">;</span></li><li> <span class="i">$x</span> =~ <span class="q">/foo/</span><span class="sc">;</span></li></ol></pre></li>
<li>
<p>The <code class="inline"><a class="l_k" href="functions/do.html">do</a> <span class="s">{</span><span class="s">}</span></code>
construct isn't a real loop that you can use
loop control on.</p>
</li>
<li>
<p>Use <code class="inline"><a class="l_k" href="functions/my.html">my()</a></code> for local variables whenever you can get away with
it (but see <a href="perlform.html">perlform</a> for where you can't).
Using <code class="inline"><a class="l_k" href="functions/local.html">local()</a></code> actually gives a local value to a global
variable, which leaves you open to unforeseen side-effects
of dynamic scoping.</p>
</li>
<li>
<p>If you localize an exported variable in a module, its exported value will
not change. The local name becomes an alias to a new value but the
external name is still an alias for the original.</p>
</li>
</ul>
<a name="Perl4-to-Perl5-Traps"></a><h2>Perl4 to Perl5 Traps</h2>
<p>Practicing Perl4 Programmers should take note of the following
Perl4-to-Perl5 specific traps.</p>
<p>They're crudely ordered according to the following list:</p>
<ul>
<li><a name="Discontinuance%2c-Deprecation%2c-and-BugFix-traps"></a><b>Discontinuance, Deprecation, and BugFix traps</b>
<p>Anything that's been fixed as a perl4 bug, removed as a perl4 feature
or deprecated as a perl4 feature with the intent to encourage usage of
some other perl5 feature.</p>
</li>
<li><a name="Parsing-Traps"></a><b>Parsing Traps</b>
<p>Traps that appear to stem from the new parser.</p>
</li>
<li><a name="Numerical-Traps"></a><b>Numerical Traps</b>
<p>Traps having to do with numerical or mathematical operators.</p>
</li>
<li><a name="General-data-type-traps"></a><b>General data type traps</b>
<p>Traps involving perl standard data types.</p>
</li>
<li><a name="Context-Traps---scalar%2c-list-contexts"></a><b>Context Traps - scalar, list contexts</b>
<p>Traps related to context within lists, scalar statements/declarations.</p>
</li>
<li><a name="Precedence-Traps"></a><b>Precedence Traps</b>
<p>Traps related to the precedence of parsing, evaluation, and execution of
code.</p>
</li>
<li><a name="General-Regular-Expression-Traps-using-s%2f%2f%2f%2c-etc."></a><b>General Regular Expression Traps using s///, etc.</b>
<p>Traps related to the use of pattern matching.</p>
</li>
<li><a name="Subroutine%2c-Signal%2c-Sorting-Traps"></a><b>Subroutine, Signal, Sorting Traps</b>
<p>Traps related to the use of signals and signal handlers, general subroutines,
and sorting, along with sorting subroutines.</p>
</li>
<li><a name="OS-Traps"></a><b>OS Traps</b>
<p>OS-specific traps.</p>
</li>
<li><a name="DBM-Traps"></a><b>DBM Traps</b>
<p>Traps specific to the use of <code class="inline"><a class="l_k" href="functions/dbmopen.html">dbmopen()</a></code>, and specific dbm implementations.</p>
</li>
<li><a name="Unclassified-Traps"></a><b>Unclassified Traps</b>
<p>Everything else.</p>
</li>
</ul>
<p>If you find an example of a conversion trap that is not listed here,
please submit it to <<i>perlbug@perl.org</i>> for inclusion.
Also note that at least some of these can be caught with the
<code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">warnings</span></code>
pragma or the <b>-w</b> switch.</p>
<a name="Discontinuance%2c-Deprecation%2c-and-BugFix-traps"></a><h2>Discontinuance, Deprecation, and BugFix traps</h2>
<p>Anything that has been discontinued, deprecated, or fixed as
a bug from perl4.</p>
<ul>
<li><a name="*-Symbols-starting-with-%22_%22-no-longer-forced-into-main"></a><b>Symbols starting with "_" no longer forced into main</b>
<p>Symbols starting with "_" are no longer forced into package main, except
for <code class="inline"><span class="i">$_</span></code>
itself (and <code class="inline"><span class="i">@_</span></code>
, etc.).</p>
<pre class="verbatim"><ol><li><a name="package-test"></a> package <span class="i">test</span><span class="sc">;</span></li><li> <span class="i">$_legacy</span> = <span class="n">1</span><span class="sc">;</span></li><li></li><li><a name="package-main"></a> package <span class="i">main</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"\$_legacy is "</span><span class="cm">,</span><span class="i">$_legacy</span><span class="cm">,</span><span class="q">"\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: $_legacy is 1</span></li><li> <span class="c"># perl5 prints: $_legacy is</span></li></ol></pre></li>
<li><a name="*-Double-colon-valid-package-separator-in-variable-name"></a><b>Double-colon valid package separator in variable name</b>
<p>Double-colon is now a valid package separator in a variable name. Thus these
behave differently in perl4 vs. perl5, because the packages don't exist.</p>
<pre class="verbatim"><ol><li> <span class="i">$a</span>=<span class="n">1</span><span class="sc">;</span><span class="i">$b</span>=<span class="n">2</span><span class="sc">;</span><span class="i">$c</span>=<span class="n">3</span><span class="sc">;</span><span class="i">$var</span>=<span class="n">4</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"$a::$b::$c "</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"$var::abc::xyz\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: 1::2::3 4::abc::xyz</span></li><li> <span class="c"># perl5 prints: 3</span></li></ol></pre><p>Given that <code class="inline"><span class="w">::</span></code>
is now the preferred package delimiter, it is debatable
whether this should be classed as a bug or not.
(The older package delimiter, ' ,is used here)</p>
<pre class="verbatim"><ol><li> <span class="i">$x</span> = <span class="n">10</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"x=${'x}\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: x=10</span></li><li> <span class="c"># perl5 prints: Can't find string terminator "'" anywhere before EOF</span></li></ol></pre><p>You can avoid this problem, and remain compatible with perl4, if you
always explicitly include the package name:</p>
<pre class="verbatim"><ol><li> <span class="i">$x</span> = <span class="n">10</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"x=${main'x}\n"</span><span class="sc">;</span></li></ol></pre><p>Also see precedence traps, for parsing <code class="inline"><span class="i">$:</span></code>
.</p>
</li>
<li><a name="*-2nd-and-3rd-args-to-splice()-are-now-in-scalar-context"></a><b>2nd and 3rd args to <code class="inline"><a class="l_k" href="functions/splice.html">splice()</a></code> are now in scalar context</b>
<p>The second and third arguments of <code class="inline"><a class="l_k" href="functions/splice.html">splice()</a></code> are now evaluated in scalar
context (as the Camel says) rather than list context.</p>
<pre class="verbatim"><ol><li><a name="sub1"></a> sub <span class="m">sub1</span><span class="s">{</span><a class="l_k" href="functions/return.html">return</a><span class="s">(</span><span class="n">0</span><span class="cm">,</span><span class="n">2</span><span class="s">)</span> <span class="s">}</span> <span class="c"># return a 2-element list</span></li><li><a name="sub2"></a> sub <span class="m">sub2</span><span class="s">{</span> <a class="l_k" href="functions/return.html">return</a><span class="s">(</span><span class="n">1</span><span class="cm">,</span><span class="n">2</span><span class="cm">,</span><span class="n">3</span><span class="s">)</span><span class="s">}</span> <span class="c"># return a 3-element list</span></li><li> <span class="i">@a1</span> = <span class="s">(</span><span class="q">"a"</span><span class="cm">,</span><span class="q">"b"</span><span class="cm">,</span><span class="q">"c"</span><span class="cm">,</span><span class="q">"d"</span><span class="cm">,</span><span class="q">"e"</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">@a2</span> = <a class="l_k" href="functions/splice.html">splice</a><span class="s">(</span><span class="i">@a1</span><span class="cm">,</span><span class="i">&sub1</span><span class="cm">,</span><span class="i">&sub2</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">' '</span><span class="cm">,</span><span class="i">@a2</span><span class="s">)</span><span class="cm">,</span><span class="q">"\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: a b</span></li><li> <span class="c"># perl5 prints: c d e</span></li></ol></pre></li>
<li><a name="*-Can't-do-goto-into-a-block-that-is-optimized-away"></a><b>Can't do <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code> into a block that is optimized away</b>
<p>You can't do a <code class="inline"><a class="l_k" href="functions/goto.html">goto</a></code> into a block that is optimized away. Darn.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/goto.html">goto</a> <span class="j">marker1</span><span class="sc">;</span></li><li></li><li> for<span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="s">{</span></li><li> <span class="j">marker1:</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Here I is!\n"</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <span class="c"># perl4 prints: Here I is!</span></li><li> <span class="c"># perl5 errors: Can't "goto" into the middle of a foreach loop</span></li></ol></pre></li>
<li><a name="*-Can't-use-whitespace-as-variable-name-or-quote-delimiter"></a><b>Can't use whitespace as variable name or quote delimiter</b>
<p>It is no longer syntactically legal to use whitespace as the name
of a variable, or as a delimiter for any kind of quote construct.
Double darn.</p>
<pre class="verbatim"><ol><li> $a = ("foo bar");</li><li> $b = q baz ;</li><li> print "a is $a, b is $b\n";</li><li></li><li> # perl4 prints: a is foo bar, b is baz</li><li> # perl5 errors: Bareword found where operator expected</li></ol></pre></li>
<li><a name="*-while%2fif-BLOCK-BLOCK-gone"></a><b><code class="inline">while/if BLOCK BLOCK</code> gone</b>
<p>The archaic while/if BLOCK BLOCK syntax is no longer supported.</p>
<pre class="verbatim"><ol><li> if { 1 } {</li><li> print "True!";</li><li> }</li><li> else {</li><li> print "False!";</li><li> }</li><li></li><li> # perl4 prints: True!</li><li> # perl5 errors: syntax error at test.pl line 1, near "if {"</li></ol></pre></li>
<li><a name="*-**-binds-tighter-than-unary-minus"></a><b><code class="inline"><span class="i">**</span></code>
binds tighter than unary minus</b>
<p>The <code class="inline"><span class="i">**</span></code>
operator now binds more tightly than unary minus.
It was documented to work this way before, but didn't.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="n">-4</span>**<span class="n">2</span><span class="cm">,</span><span class="q">"\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: 16</span></li><li> <span class="c"># perl5 prints: -16</span></li></ol></pre></li>
<li><a name="*-foreach-changed-when-iterating-over-a-list"></a><b><code class="inline">foreach</code>
changed when iterating over a list</b>
<p>The meaning of <code class="inline">foreach<span class="s">{</span><span class="s">}</span></code>
has changed slightly when it is iterating over a
list which is not an array. This used to assign the list to a
temporary array, but no longer does so (for efficiency). This means
that you'll now be iterating over the actual values, not over copies of
the values. Modifications to the loop variable can change the original
values.</p>
<pre class="verbatim"><ol><li> <span class="i">@list</span> = <span class="s">(</span><span class="q">'ab'</span><span class="cm">,</span><span class="q">'abc'</span><span class="cm">,</span><span class="q">'bcd'</span><span class="cm">,</span><span class="q">'def'</span><span class="s">)</span><span class="sc">;</span></li><li> foreach <span class="i">$var</span> <span class="s">(</span><a class="l_k" href="functions/grep.html">grep</a><span class="s">(</span><span class="q">/ab/</span><span class="cm">,</span><span class="i">@list</span><span class="s">)</span><span class="s">)</span><span class="s">{</span></li><li> <span class="i">$var</span> = <span class="n">1</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="s">(</span><a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span><span class="i">@list</span><span class="s">)</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: ab:abc:bcd:def</span></li><li> <span class="c"># perl5 prints: 1:1:bcd:def</span></li></ol></pre><p>To retain Perl4 semantics you need to assign your list
explicitly to a temporary array and then iterate over that. For
example, you might need to change</p>
<pre class="verbatim"><ol><li> foreach $var (grep(/ab/,@list)){</li></ol></pre><p>to</p>
<pre class="verbatim"><ol><li> foreach $var (@tmp = grep(/ab/,@list)){</li></ol></pre><p>Otherwise changing $var will clobber the values of @list. (This most often
happens when you use <code class="inline"><span class="i">$_</span></code>
for the loop variable, and call subroutines in
the loop that don't properly localize <code class="inline"><span class="i">$_</span></code>
.)</p>
</li>
<li><a name="*-split-with-no-args-behavior-changed"></a><b><code class="inline"><a class="l_k" href="functions/split.html">split</a></code> with no args behavior changed</b>
<p><code class="inline"><a class="l_k" href="functions/split.html">split</a></code> with no arguments now behaves like <code class="inline"><a class="l_k" href="functions/split.html">split</a> <span class="q">' '</span></code>
(which doesn't
return an initial null field if $_ starts with whitespace), it used to
behave like <code class="inline"><a class="l_k" href="functions/split.html">split</a> <span class="q">/\s+/</span></code>
(which does).</p>
<pre class="verbatim"><ol><li> <span class="i">$_</span> = <span class="q">' hi mom'</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">':'</span><span class="cm">,</span> <a class="l_k" href="functions/split.html">split</a><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: :hi:mom</span></li><li> <span class="c"># perl5 prints: hi:mom</span></li></ol></pre></li>
<li><a name="*-*-e*-behavior-fixed"></a><b><b>-e</b> behavior fixed</b>
<p>Perl 4 would ignore any text which was attached to an <b>-e</b> switch,
always taking the code snippet from the following arg. Additionally, it
would silently accept an <b>-e</b> switch without a following arg. Both of
these behaviors have been fixed.</p>
<pre class="verbatim"><ol><li> perl -e'print "attached to -e"' 'print "separate arg"'</li><li></li><li> # perl4 prints: separate arg</li><li> # perl5 prints: attached to -e</li><li></li><li> perl -e</li><li></li><li> # perl4 prints:</li><li> # perl5 dies: No code specified for -e.</li></ol></pre></li>
<li><a name="*-push-returns-number-of-elements-in-resulting-list"></a><b><code class="inline"><a class="l_k" href="functions/push.html">push</a></code> returns number of elements in resulting list</b>
<p>In Perl 4 the return value of <code class="inline"><a class="l_k" href="functions/push.html">push</a></code> was undocumented, but it was
actually the last value being pushed onto the target list. In Perl 5
the return value of <code class="inline"><a class="l_k" href="functions/push.html">push</a></code> is documented, but has changed, it is the
number of elements in the resulting list.</p>
<pre class="verbatim"><ol><li> <span class="i">@x</span> = <span class="s">(</span><span class="q">'existing'</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/push.html">push</a><span class="s">(</span><span class="i">@x</span><span class="cm">,</span> <span class="q">'first new'</span><span class="cm">,</span> <span class="q">'second new'</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: second new</span></li><li> <span class="c"># perl5 prints: 3</span></li></ol></pre></li>
<li><a name="*-Some-error-messages-differ"></a><b>Some error messages differ</b>
<p>Some error messages will be different.</p>
</li>
<li><a name="*-split()-honors-subroutine-args"></a><b><code class="inline"><a class="l_k" href="functions/split.html">split()</a></code> honors subroutine args</b>
<p>In Perl 4, if in list context the delimiters to the first argument of
<code class="inline"><a class="l_k" href="functions/split.html">split()</a></code> were <code class="inline"><span class="q">??</span></code>
, the result would be placed in <code class="inline"><span class="i">@_</span></code>
as well as
being returned. Perl 5 has more respect for your subroutine arguments.</p>
</li>
<li><a name="*-Bugs-removed"></a><b>Bugs removed</b>
<p>Some bugs may have been inadvertently removed. :-)</p>
</li>
</ul>
<a name="Parsing-Traps"></a><h2>Parsing Traps</h2>
<p>Perl4-to-Perl5 traps from having to do with parsing.</p>
<ul>
<li><a name="*-Space-between-.-and-%3d-triggers-syntax-error"></a><b>Space between . and = triggers syntax error</b>
<p>Note the space between . and =</p>
<pre class="verbatim"><ol><li> <span class="i">$string</span> . = <span class="q">"more string"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$string</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: more string</span></li><li> <span class="c"># perl5 prints: syntax error at - line 1, near ". ="</span></li></ol></pre></li>
<li><a name="*-Better-parsing-in-perl-5"></a><b>Better parsing in perl 5</b>
<p>Better parsing in perl 5</p>
<pre class="verbatim"><ol><li><a name="foo"></a> sub <span class="m">foo</span> <span class="s">{</span><span class="s">}</span></li><li> <span class="i">&foo</span></li><li> <a class="l_k" href="functions/print.html">print</a><span class="s">(</span><span class="q">"hello, world\n"</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: hello, world</span></li><li> <span class="c"># perl5 prints: syntax error</span></li></ol></pre></li>
<li><a name="*-Function-parsing"></a><b>Function parsing</b>
<p>"if it looks like a function, it is a function" rule.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a></li><li> <span class="s">(</span><span class="i">$foo</span> == <span class="n">1</span><span class="s">)</span> ? <span class="q">"is one\n"</span> <span class="co">:</span> <span class="q">"is zero\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: is zero</span></li><li> <span class="c"># perl5 warns: "Useless use of a constant in void context" if using -w</span></li></ol></pre></li>
<li><a name="*-String-interpolation-of-%24%23array-differs"></a><b>String interpolation of <code class="inline"><span class="i">$#array</span></code>
differs</b>
<p>String interpolation of the <code class="inline"><span class="i">$#array</span></code>
construct differs when braces
are to used around the name.</p>
<pre class="verbatim"><ol><li> <span class="i">@a</span> = <span class="s">(</span><span class="n">1</span>..<span class="n">3</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"${#a}"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: 2</span></li><li> <span class="c"># perl5 fails with syntax error</span></li><li></li><li> <span class="i">@a</span> = <span class="s">(</span><span class="n">1</span>..<span class="n">3</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"$#{a}"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: {a}</span></li><li> <span class="c"># perl5 prints: 2</span></li></ol></pre></li>
<li><a name="*-Perl-guesses-on-map%2c-grep-followed-by-%7b-if-it-starts-BLOCK-or-hash-ref"></a><b>Perl guesses on <code class="inline"><a class="l_k" href="functions/map.html">map</a></code>, <code class="inline"><a class="l_k" href="functions/grep.html">grep</a></code> followed by <code class="inline">{</code> if it starts BLOCK or hash ref</b>
<p>When perl sees <code class="inline">map {</code> (or <code class="inline">grep {</code>), it has to guess whether the <code class="inline">{</code>
starts a BLOCK or a hash reference. If it guesses wrong, it will report
a syntax error near the <code class="inline">}</code> and the missing (or unexpected) comma.</p>
<p>Use unary <code class="inline">+</code>
before <code class="inline">{</code> on a hash reference, and unary <code class="inline">+</code>
applied
to the first thing in a BLOCK (after <code class="inline">{</code>), for perl to guess right all
the time. (See <a href="functions/map.html">map</a>.)</p>
</li>
</ul>
<a name="Numerical-Traps"></a><h2>Numerical Traps</h2>
<p>Perl4-to-Perl5 traps having to do with numerical operators,
operands, or output from same.</p>
<ul>
<li><a name="*-Formatted-output-and-significant-digits"></a><b>Formatted output and significant digits</b>
<p>Formatted output and significant digits. In general, Perl 5
tries to be more precise. For example, on a Solaris Sparc:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="n">7.373504</span> - <span class="n">0</span><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/printf.html">printf</a> <span class="q">"%20.18f\n"</span><span class="cm">,</span> <span class="n">7.373504</span> - <span class="n">0</span><span class="sc">;</span></li><li></li><li> <span class="c"># Perl4 prints:</span></li><li> <span class="n">7.3750399999999996141</span></li><li> <span class="n">7.375039999999999614</span></li><li></li><li> <span class="c"># Perl5 prints:</span></li><li> <span class="n">7.373504</span></li><li> <span class="n">7.373503999999999614</span></li></ol></pre><p>Notice how the first result looks better in Perl 5.</p>
<p>Your results may vary, since your floating point formatting routines
and even floating point format may be slightly different.</p>
</li>
<li><a name="*-Auto-increment-operator-over-signed-int-limit-deleted"></a><b>Auto-increment operator over signed int limit deleted</b>
<p>This specific item has been deleted. It demonstrated how the auto-increment
operator would not catch when a number went over the signed int limit. Fixed
in version 5.003_04. But always be wary when using large integers.
If in doubt:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/use.html">use</a> <span class="w">Math::BigInt</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="*-Assignment-of-return-values-from-numeric-equality-tests-doesn't-work"></a><b>Assignment of return values from numeric equality tests doesn't work</b>
<p>Assignment of return values from numeric equality tests
does not work in perl5 when the test evaluates to false (0).
Logical tests now return a null, instead of 0</p>
<pre class="verbatim"><ol><li> <span class="i">$p</span> = <span class="s">(</span><span class="i">$test</span> == <span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$p</span><span class="cm">,</span><span class="q">"\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: 0</span></li><li> <span class="c"># perl5 prints:</span></li></ol></pre><p>Also see <a href="#General-Regular-Expression-Traps-using-s%2f%2f%2f%2c-etc.">General Regular Expression Traps using s///, etc.</a>
for another example of this new feature...</p>
</li>
<li><a name="*-Bitwise-string-ops"></a><b>Bitwise string ops</b>
<p>When bitwise operators which can operate upon either numbers or
strings (<code class="inline"><span class="i">& |</span> ^ ~</code>
) are given only strings as arguments, perl4 would
treat the operands as bitstrings so long as the program contained a call
to the <code class="inline"><a class="l_k" href="functions/vec.html">vec()</a></code> function. perl5 treats the string operands as bitstrings.
(See <a href="perlop.html#Bitwise-String-Operators">Bitwise String Operators in perlop</a> for more details.)</p>
<pre class="verbatim"><ol><li> $fred = "10";</li><li> $barney = "12";</li><li> $betty = $fred & $barney;</li><li> print "$betty\n";</li><li> # Uncomment the next line to change perl4's behavior</li><li> # ($dummy) = vec("dummy", 0, 0);</li><li></li><li> # Perl4 prints:</li><li> 8</li><li></li><li> # Perl5 prints:</li><li> 10</li><li></li><li> # If vec() is used anywhere in the program, both print:</li><li> 10</li></ol></pre></li>
</ul>
<a name="General-data-type-traps"></a><h2>General data type traps</h2>
<p>Perl4-to-Perl5 traps involving most data-types, and their usage
within certain expressions and/or context.</p>
<ul>
<li><a name="*-Negative-array-subscripts-now-count-from-the-end-of-array"></a><b>Negative array subscripts now count from the end of array</b>
<p>Negative array subscripts now count from the end of the array.</p>
<pre class="verbatim"><ol><li> <span class="i">@a</span> = <span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="n">3</span><span class="cm">,</span> <span class="n">4</span><span class="cm">,</span> <span class="n">5</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"The third element of the array is $a[3] also expressed as $a[-2] \n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: The third element of the array is 4 also expressed as</span></li><li> <span class="c"># perl5 prints: The third element of the array is 4 also expressed as 4</span></li></ol></pre></li>
<li><a name="*-Setting-%24%23array-lower-now-discards-array-elements"></a><b>Setting <code class="inline"><span class="i">$#array</span></code>
lower now discards array elements</b>
<p>Setting <code class="inline"><span class="i">$#array</span></code>
lower now discards array elements, and makes them
impossible to recover.</p>
<pre class="verbatim"><ol><li> <span class="i">@a</span> = <span class="s">(</span><span class="w">a</span><span class="cm">,</span><span class="w">b</span><span class="cm">,</span><span class="w">c</span><span class="cm">,</span><span class="w">d</span><span class="cm">,</span><span class="w">e</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Before: "</span><span class="cm">,</span><a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">''</span><span class="cm">,</span><span class="i">@a</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$#a</span> =<span class="n">1</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">", After: "</span><span class="cm">,</span><a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">''</span><span class="cm">,</span><span class="i">@a</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$#a</span> =<span class="n">3</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">", Recovered: "</span><span class="cm">,</span><a class="l_k" href="functions/join.html">join</a><span class="s">(</span><span class="q">''</span><span class="cm">,</span><span class="i">@a</span><span class="s">)</span><span class="cm">,</span><span class="q">"\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: Before: abcde, After: ab, Recovered: abcd</span></li><li> <span class="c"># perl5 prints: Before: abcde, After: ab, Recovered: ab</span></li></ol></pre></li>
<li><a name="*-Hashes-get-defined-before-use"></a><b>Hashes get defined before use</b>
<p>Hashes get defined before use</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/local.html">local</a><span class="s">(</span><span class="i">$s</span><span class="cm">,</span><span class="i">@a</span><span class="cm">,</span><span class="i">%h</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/die.html">die</a> <span class="q">"scalar \$s defined"</span> if <a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$s</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/die.html">die</a> <span class="q">"array \@a defined"</span> if <a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">@a</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/die.html">die</a> <span class="q">"hash \%h defined"</span> if <a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">%h</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints:</span></li><li> <span class="c"># perl5 dies: hash %h defined</span></li></ol></pre><p>Perl will now generate a warning when it sees defined(@a) and
defined(%h).</p>
</li>
<li><a name="*-Glob-assignment-from-localized-variable-to-variable"></a><b>Glob assignment from localized variable to variable</b>
<p>glob assignment from variable to variable will fail if the assigned
variable is localized subsequent to the assignment</p>
<pre class="verbatim"><ol><li> <span class="i">@a</span> = <span class="s">(</span><span class="q">"This is Perl 4"</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">*b</span> = <span class="i">*a</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/local.html">local</a><span class="s">(</span><span class="i">@a</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">@b</span><span class="cm">,</span><span class="q">"\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: This is Perl 4</span></li><li> <span class="c"># perl5 prints:</span></li></ol></pre></li>
<li><a name="*-Assigning-undef-to-glob"></a><b>Assigning <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> to glob</b>
<p>Assigning <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> to a glob has no effect in Perl 5. In Perl 4
it undefines the associated scalar (but may have other side effects
including SEGVs). Perl 5 will also warn if <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> is assigned to a
typeglob. (Note that assigning <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> to a typeglob is different
than calling the <code class="inline"><a class="l_k" href="functions/undef.html">undef</a></code> function on a typeglob (<code class="inline"><a class="l_k" href="functions/undef.html">undef</a> <span class="i">*foo</span></code>
), which
has quite a few effects.</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> = <span class="q">"bar"</span><span class="sc">;</span></li><li> <span class="i">*foo</span> = <a class="l_k" href="functions/undef.html">undef</a><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$foo</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints:</span></li><li> <span class="c"># perl4 warns: "Use of uninitialized variable" if using -w</span></li><li> <span class="c"># perl5 prints: bar</span></li><li> <span class="c"># perl5 warns: "Undefined value assigned to typeglob" if using -w</span></li></ol></pre></li>
<li><a name="*-Changes-in-unary-negation-(of-strings)"></a><b>Changes in unary negation (of strings)</b>
<p>Changes in unary negation (of strings)
This change effects both the return value and what it
does to auto(magic)increment.</p>
<pre class="verbatim"><ol><li> <span class="i">$x</span> = <span class="q">"aaa"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> ++<span class="i">$x</span><span class="cm">,</span><span class="q">" : "</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> -<span class="i">$x</span><span class="cm">,</span><span class="q">" : "</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> ++<span class="i">$x</span><span class="cm">,</span><span class="q">"\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: aab : -0 : 1</span></li><li> <span class="c"># perl5 prints: aab : -aab : aac</span></li></ol></pre></li>
<li><a name="*-Modifying-of-constants-prohibited"></a><b>Modifying of constants prohibited</b>
<p>perl 4 lets you modify constants:</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> = <span class="q">"x"</span><span class="sc">;</span></li><li> <span class="i">&mod</span><span class="s">(</span><span class="i">$foo</span><span class="s">)</span><span class="sc">;</span></li><li> for <span class="s">(</span><span class="i">$x</span> = <span class="n">0</span><span class="sc">;</span> <span class="i">$x</span> < <span class="n">3</span><span class="sc">;</span> <span class="i">$x</span>++<span class="s">)</span> <span class="s">{</span></li><li> <span class="i">&mod</span><span class="s">(</span><span class="q">"a"</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li><a name="mod"></a> sub <span class="m">mod</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"before: $_[0]"</span><span class="sc">;</span></li><li> <span class="i">$_</span>[<span class="n">0</span>] = <span class="q">"m"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">" after: $_[0]\n"</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <span class="c"># perl4:</span></li><li> <span class="c"># before: x after: m</span></li><li> <span class="c"># before: a after: m</span></li><li> <span class="c"># before: m after: m</span></li><li> <span class="c"># before: m after: m</span></li><li></li><li> <span class="c"># Perl5:</span></li><li> <span class="c"># before: x after: m</span></li><li> <span class="c"># Modification of a read-only value attempted at foo.pl line 12.</span></li><li> <span class="c"># before: a</span></li></ol></pre></li>
<li><a name="*-defined-%24var-behavior-changed"></a><b><code class="inline"><a class="l_k" href="functions/defined.html">defined</a> <span class="i">$var</span></code>
behavior changed</b>
<p>The behavior is slightly different for:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"$x"</span><span class="cm">,</span> <a class="l_k" href="functions/defined.html">defined</a> <span class="i">$x</span></li><li></li><li> <span class="c"># perl 4: 1</span></li><li> <span class="c"># perl 5: <no output, $x is not called into existence></span></li></ol></pre></li>
<li><a name="*-Variable-Suicide"></a><b>Variable Suicide</b>
<p>Variable suicide behavior is more consistent under Perl 5.
Perl5 exhibits the same behavior for hashes and scalars,
that perl4 exhibits for only scalars.</p>
<pre class="verbatim"><ol><li> <span class="i">$aGlobal</span>{ <span class="q">"aKey"</span> } = <span class="q">"global value"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"MAIN:"</span><span class="cm">,</span> <span class="i">$aGlobal</span>{<span class="q">"aKey"</span>}<span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li><li> <span class="i">$GlobalLevel</span> = <span class="n">0</span><span class="sc">;</span></li><li> <span class="i">&test</span><span class="s">(</span> <span class="i">*aGlobal</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li><a name="test"></a> sub <span class="m">test</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/local.html">local</a><span class="s">(</span> <span class="i">*theArgument</span> <span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/local.html">local</a><span class="s">(</span> <span class="i">%aNewLocal</span> <span class="s">)</span><span class="sc">;</span> <span class="c"># perl 4 != 5.001l,m</span></li><li> <span class="i">$aNewLocal</span>{<span class="q">"aKey"</span>} = <span class="q">"this should never appear"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"SUB: "</span><span class="cm">,</span> <span class="i">$theArgument</span>{<span class="q">"aKey"</span>}<span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li><li> <span class="i">$aNewLocal</span>{<span class="q">"aKey"</span>} = <span class="q">"level $GlobalLevel"</span><span class="sc">;</span> <span class="c"># what should print</span></li><li> <span class="i">$GlobalLevel</span>++<span class="sc">;</span></li><li> if<span class="s">(</span> <span class="i">$GlobalLevel</span><<span class="n">4</span> <span class="s">)</span> <span class="s">{</span></li><li> <span class="i">&test</span><span class="s">(</span> <span class="i">*aNewLocal</span> <span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <span class="s">}</span></li><li></li><li> <span class="c"># Perl4:</span></li><li> <span class="c"># MAIN:global value</span></li><li> <span class="c"># SUB: global value</span></li><li> <span class="c"># SUB: level 0</span></li><li> <span class="c"># SUB: level 1</span></li><li> <span class="c"># SUB: level 2</span></li><li></li><li> <span class="c"># Perl5:</span></li><li> <span class="c"># MAIN:global value</span></li><li> <span class="c"># SUB: global value</span></li><li> <span class="c"># SUB: this should never appear</span></li><li> <span class="c"># SUB: this should never appear</span></li><li> <span class="c"># SUB: this should never appear</span></li></ol></pre></li>
</ul>
<a name="Context-Traps---scalar%2c-list-contexts"></a><h2>Context Traps - scalar, list contexts</h2>
<ul>
<li><a name="*-Elements-of-argument-lists-for-formats-evaluated-in-list-context"></a><b>Elements of argument lists for formats evaluated in list context</b>
<p>The elements of argument lists for formats are now evaluated in list
context. This means you can interpolate list values now.</p>
<pre class="verbatim"><ol><li> @fmt = ("foo","bar","baz");</li><li> format STDOUT=</li><li> @<<<<< @||||| @>>>>></li><li> @fmt;</li><li> .</li><li> write;</li><li></li><li> # perl4 errors: Please use commas to separate fields in file</li><li> # perl5 prints: foo bar baz</li></ol></pre></li>
<li><a name="*-caller()-returns-false-value-in-scalar-context-if-no-caller-present"></a><b><code class="inline"><a class="l_k" href="functions/caller.html">caller()</a></code> returns false value in scalar context if no caller present</b>
<p>The <code class="inline"><a class="l_k" href="functions/caller.html">caller()</a></code> function now returns a false value in a scalar context
if there is no caller. This lets library files determine if they're
being required.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/caller.html">caller</a><span class="s">(</span><span class="s">)</span> ? <span class="s">(</span><a class="l_k" href="functions/print.html">print</a> <span class="q">"You rang?\n"</span><span class="s">)</span> <span class="co">:</span> <span class="s">(</span><a class="l_k" href="functions/print.html">print</a> <span class="q">"Got a 0\n"</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 errors: There is no caller</span></li><li> <span class="c"># perl5 prints: Got a 0</span></li></ol></pre></li>
<li><a name="*-Comma-operator-in-scalar-context-gives-scalar-context-to-args"></a><b>Comma operator in scalar context gives scalar context to args</b>
<p>The comma operator in a scalar context is now guaranteed to give a
scalar context to its last argument. It gives scalar or void context
to any preceding arguments, depending on circumstances.</p>
<pre class="verbatim"><ol><li> <span class="i">@y</span>= <span class="s">(</span><span class="q">'a'</span><span class="cm">,</span><span class="q">'b'</span><span class="cm">,</span><span class="q">'c'</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$x</span> = <span class="s">(</span><span class="n">1</span><span class="cm">,</span> <span class="n">2</span><span class="cm">,</span> <span class="i">@y</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"x = $x\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># Perl4 prints: x = c # Interpolates array @y into the list</span></li><li> <span class="c"># Perl5 prints: x = 3 # Evaluates array @y in scalar context</span></li></ol></pre></li>
<li><a name="*-sprintf()-prototyped-as-(%24%3b%40)"></a><b><code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf()</a></code> prototyped as <code class="inline">($;@)</code></b>
<p><code class="inline"><a class="l_k" href="functions/sprintf.html">sprintf()</a></code> is prototyped as ($;@), so its first argument is given scalar
context. Thus, if passed an array, it will probably not do what you want,
unlike Perl 4:</p>
<pre class="verbatim"><ol><li> <span class="i">@z</span> = <span class="s">(</span><span class="q">'%s%s'</span><span class="cm">,</span> <span class="q">'foo'</span><span class="cm">,</span> <span class="q">'bar'</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$x</span> = <a class="l_k" href="functions/sprintf.html">sprintf</a><span class="s">(</span><span class="i">@z</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$x</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: foobar</span></li><li> <span class="c"># perl5 prints: 3</span></li></ol></pre><p><code class="inline"><a class="l_k" href="functions/printf.html">printf()</a></code> works the same as it did in Perl 4, though:</p>
<pre class="verbatim"><ol><li> <span class="i">@z</span> = <span class="s">(</span><span class="q">'%s%s'</span><span class="cm">,</span> <span class="q">'foo'</span><span class="cm">,</span> <span class="q">'bar'</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/printf.html">printf</a> <span class="i">STDOUT</span> <span class="s">(</span><span class="i">@z</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: foobar</span></li><li> <span class="c"># perl5 prints: foobar</span></li></ol></pre></li>
</ul>
<a name="Precedence-Traps"></a><h2>Precedence Traps</h2>
<p>Perl4-to-Perl5 traps involving precedence order.</p>
<p>Perl 4 has almost the same precedence rules as Perl 5 for the operators
that they both have. Perl 4 however, seems to have had some
inconsistencies that made the behavior differ from what was documented.</p>
<ul>
<li><a name="*-LHS-vs.-RHS-of-any-assignment-operator"></a><b>LHS vs. RHS of any assignment operator</b>
<p>LHS vs. RHS of any assignment operator. LHS is evaluated first
in perl4, second in perl5; this can affect the relationship
between side-effects in sub-expressions.</p>
<pre class="verbatim"><ol><li> <span class="i">@arr</span> = <span class="s">(</span> <span class="q">'left'</span><span class="cm">,</span> <span class="q">'right'</span> <span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$a</span>{<a class="l_k" href="functions/shift.html">shift</a> <span class="i">@arr</span>} = <a class="l_k" href="functions/shift.html">shift</a> <span class="i">@arr</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/join.html">join</a><span class="s">(</span> <span class="q">' '</span><span class="cm">,</span> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%a</span> <span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: left</span></li><li> <span class="c"># perl5 prints: right</span></li></ol></pre></li>
<li><a name="*-Semantic-errors-introduced-due-to-precedence"></a><b>Semantic errors introduced due to precedence</b>
<p>These are now semantic errors because of precedence:</p>
<pre class="verbatim"><ol><li> <span class="i">@list</span> = <span class="s">(</span><span class="n">1</span><span class="cm">,</span><span class="n">2</span><span class="cm">,</span><span class="n">3</span><span class="cm">,</span><span class="n">4</span><span class="cm">,</span><span class="n">5</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">%map</span> = <span class="s">(</span><span class="q">"a"</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="q">"b"</span><span class="cm">,</span><span class="n">2</span><span class="cm">,</span><span class="q">"c"</span><span class="cm">,</span><span class="n">3</span><span class="cm">,</span><span class="q">"d"</span><span class="cm">,</span><span class="n">4</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$n</span> = <a class="l_k" href="functions/shift.html">shift</a> <span class="i">@list</span> + <span class="n">2</span><span class="sc">;</span> <span class="c"># first item in list plus 2</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"n is $n, "</span><span class="sc">;</span></li><li> <span class="i">$m</span> = <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%map</span> + <span class="n">2</span><span class="sc">;</span> <span class="c"># number of items in hash plus 2</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"m is $m\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: n is 3, m is 6</span></li><li> <span class="c"># perl5 errors and fails to compile</span></li></ol></pre></li>
<li><a name="*-Precedence-of-assignment-operators-same-as-the-precedence-of-assignment"></a><b>Precedence of assignment operators same as the precedence of assignment</b>
<p>The precedence of assignment operators is now the same as the precedence
of assignment. Perl 4 mistakenly gave them the precedence of the associated
operator. So you now must parenthesize them in expressions like</p>
<pre class="verbatim"><ol><li> <span class="q">/foo/</span> ? <span class="s">(</span><span class="i">$a</span> += <span class="n">2</span><span class="s">)</span> <span class="co">:</span> <span class="s">(</span><span class="i">$a</span> -= <span class="n">2</span><span class="s">)</span><span class="sc">;</span></li></ol></pre><p>Otherwise</p>
<pre class="verbatim"><ol><li> <span class="q">/foo/</span> ? <span class="i">$a</span> += <span class="n">2</span> <span class="co">:</span> <span class="i">$a</span> -= <span class="n">2</span></li></ol></pre><p>would be erroneously parsed as</p>
<pre class="verbatim"><ol><li> <span class="s">(</span><span class="q">/foo/</span> ? <span class="i">$a</span> += <span class="n">2</span> <span class="co">:</span> <span class="i">$a</span><span class="s">)</span> -= <span class="n">2</span><span class="sc">;</span></li></ol></pre><p>On the other hand,</p>
<pre class="verbatim"><ol><li> <span class="i">$a</span> += <span class="q">/foo/</span> ? <span class="n">1</span> <span class="co">:</span> <span class="n">2</span><span class="sc">;</span></li></ol></pre><p>now works as a C programmer would expect.</p>
</li>
<li><a name="*-open-requires-parentheses-around-filehandle"></a><b><code class="inline"><a class="l_k" href="functions/open.html">open</a></code> requires parentheses around filehandle</b>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/open.html">open</a> <span class="w">FOO</span> || <a class="l_k" href="functions/die.html">die</a><span class="sc">;</span></li></ol></pre><p>is now incorrect. You need parentheses around the filehandle.
Otherwise, perl5 leaves the statement as its default precedence:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">FOO</span> || <a class="l_k" href="functions/die.html">die</a><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 opens or dies</span></li><li> <span class="c"># perl5 opens FOO, dying only if 'FOO' is false, i.e. never</span></li></ol></pre></li>
<li><a name="*-%24%3a-precedence-over-%24%3a%3a-gone"></a><b><code class="inline"><span class="i">$:</span></code>
precedence over <code class="inline"><span class="i">$::</span></code>
gone</b>
<p>perl4 gives the special variable, <code class="inline"><span class="i">$:</span></code>
precedence, where perl5
treats <code class="inline"><span class="i">$::</span></code>
as main <code class="inline"><a class="l_k" href="functions/package.html">package</a></code></p>
<pre class="verbatim"><ol><li> <span class="i">$a</span> = <span class="q">"x"</span><span class="sc">;</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">"$::a"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl 4 prints: -:a</span></li><li> <span class="c"># perl 5 prints: x</span></li></ol></pre></li>
<li><a name="*-Precedence-of-file-test-operators-documented"></a><b>Precedence of file test operators documented</b>
<p>perl4 had buggy precedence for the file test operators vis-a-vis
the assignment operators. Thus, although the precedence table
for perl4 leads one to believe <code class="inline">-e <span class="i">$foo</span> .= <span class="q">"q"</span></code>
should parse as
<code class="inline"><span class="s">(</span><span class="s">(</span>-e <span class="i">$foo</span><span class="s">)</span> .= <span class="q">"q"</span><span class="s">)</span></code>
, it actually parses as <code class="inline"><span class="s">(</span>-e <span class="s">(</span><span class="i">$foo</span> .= <span class="q">"q"</span><span class="s">)</span><span class="s">)</span></code>
.
In perl5, the precedence is as documented.</p>
<pre class="verbatim"><ol><li> -e <span class="i">$foo</span> .= <span class="q">"q"</span></li><li></li><li> <span class="c"># perl4 prints: no output</span></li><li> <span class="c"># perl5 prints: Can't modify -e in concatenation</span></li></ol></pre></li>
<li><a name="*-keys%2c-each%2c-values-are-regular-named-unary-operators"></a><b><code class="inline"><a class="l_k" href="functions/keys.html">keys</a></code>, <code class="inline"><a class="l_k" href="functions/each.html">each</a></code>, <code class="inline"><a class="l_k" href="functions/values.html">values</a></code> are regular named unary operators</b>
<p>In perl4, keys(), each() and values() were special high-precedence operators
that operated on a single hash, but in perl5, they are regular named unary
operators. As documented, named unary operators have lower precedence
than the arithmetic and concatenation operators <code class="inline">+ - .</code>, but the perl4
variants of these operators actually bind tighter than <code class="inline">+ - .</code>.
Thus, for:</p>
<pre class="verbatim"><ol><li> <span class="i">%foo</span> = <span class="n">1</span>..<span class="n">10</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/keys.html">keys</a> <span class="i">%foo</span> - <span class="n">1</span></li><li></li><li> <span class="c"># perl4 prints: 4</span></li><li> <span class="c"># perl5 prints: Type of arg 1 to keys must be hash (not subtraction)</span></li></ol></pre><p>The perl4 behavior was probably more useful, if less consistent.</p>
</li>
</ul>
<a name="General-Regular-Expression-Traps-using-s%2f%2f%2f%2c-etc."></a><h2>General Regular Expression Traps using s///, etc.</h2>
<p>All types of RE traps.</p>
<ul>
<li><a name="*-s'%24lhs'%24rhs'-interpolates-on-either-side"></a><b><code class="inline"><a class="l_k" href="functions/s.html">s'$lhs'$rhs'</a></code> interpolates on either side</b>
<p><code class="inline"><a class="l_k" href="functions/s.html">s'$lhs'$rhs'</a></code> now does no interpolation on either side. It used to
interpolate $lhs but not $rhs. (And still does not match a literal
'$' in string)</p>
<pre class="verbatim"><ol><li> <span class="i">$a</span>=<span class="n">1</span><span class="sc">;</span><span class="i">$b</span>=<span class="n">2</span><span class="sc">;</span></li><li> <span class="i">$string</span> = <span class="q">'1 2 $a $b'</span><span class="sc">;</span></li><li> <span class="i">$string</span> =~ <span class="q">s'$a'$b'</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$string</span><span class="cm">,</span><span class="q">"\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: $b 2 $a $b</span></li><li> <span class="c"># perl5 prints: 1 2 $a $b</span></li></ol></pre></li>
<li><a name="*-m%2f%2fg-attaches-its-state-to-the-searched-string"></a><b><code class="inline"><a class="l_k" href="functions/m.html">m//g</a></code> attaches its state to the searched string</b>
<p><code class="inline"><a class="l_k" href="functions/m.html">m//g</a></code> now attaches its state to the searched string rather than the
regular expression. (Once the scope of a block is left for the sub, the
state of the searched string is lost)</p>
<pre class="verbatim"><ol><li> <span class="i">$_</span> = <span class="q">"ababab"</span><span class="sc">;</span></li><li> while<span class="s">(</span><span class="q">m/ab/g</span><span class="s">)</span><span class="s">{</span></li><li> <span class="i">&doit</span><span class="s">(</span><span class="q">"blah"</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li><a name="doit"></a> sub <span class="m">doit</span><span class="s">{</span><a class="l_k" href="functions/local.html">local</a><span class="s">(</span><span class="i">$_</span><span class="s">)</span> = <a class="l_k" href="functions/shift.html">shift</a><span class="sc">;</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Got $_ "</span><span class="s">}</span></li><li></li><li> <span class="c"># perl4 prints: Got blah Got blah Got blah Got blah</span></li><li> <span class="c"># perl5 prints: infinite loop blah...</span></li></ol></pre></li>
<li><a name="*-m%2f%2fo-used-within-an-anonymous-sub"></a><b><code class="inline"><a class="l_k" href="functions/m.html">m//o</a></code> used within an anonymous sub</b>
<p>Currently, if you use the <code class="inline"><a class="l_k" href="functions/m.html">m//o</a></code> qualifier on a regular expression
within an anonymous sub, <i>all</i> closures generated from that anonymous
sub will use the regular expression as it was compiled when it was used
the very first time in any such closure. For instance, if you say</p>
<pre class="verbatim"><ol><li><a name="build_match"></a> sub <span class="m">build_match</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/my.html">my</a><span class="s">(</span><span class="i">$left</span><span class="cm">,</span><span class="i">$right</span><span class="s">)</span> = <span class="i">@_</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/return.html">return</a> <a class="l_k" href="functions/sub.html">sub</a> <span class="s">{</span> <span class="i">$_</span>[<span class="n">0</span>] =~ <span class="q">/$left stuff $right/o</span><span class="sc">;</span> <span class="s">}</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <span class="i">$good</span> = <span class="i">build_match</span><span class="s">(</span><span class="q">'foo'</span><span class="cm">,</span><span class="q">'bar'</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$bad</span> = <span class="i">build_match</span><span class="s">(</span><span class="q">'baz'</span><span class="cm">,</span><span class="q">'blarch'</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$good</span>-><span class="s">(</span><span class="q">'foo stuff bar'</span><span class="s">)</span> ? <span class="q">"ok\n"</span> <span class="co">:</span> <span class="q">"not ok\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$bad</span>-><span class="s">(</span><span class="q">'baz stuff blarch'</span><span class="s">)</span> ? <span class="q">"ok\n"</span> <span class="co">:</span> <span class="q">"not ok\n"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$bad</span>-><span class="s">(</span><span class="q">'foo stuff bar'</span><span class="s">)</span> ? <span class="q">"not ok\n"</span> <span class="co">:</span> <span class="q">"ok\n"</span><span class="sc">;</span></li></ol></pre><p>For most builds of Perl5, this will print:
ok
not ok
not ok</p>
<p>build_match() will always return a sub which matches the contents of
$left and $right as they were the <i>first</i> time that build_match()
was called, not as they are in the current call.</p>
</li>
<li><a name="*-%24%2b-isn't-set-to-whole-match"></a><b><code class="inline"><span class="i">$+</span></code>
isn't set to whole match</b>
<p>If no parentheses are used in a match, Perl4 sets <code class="inline"><span class="i">$+</span></code>
to
the whole match, just like <code class="inline"><span class="i">$&</span></code>
. Perl5 does not.</p>
<pre class="verbatim"><ol><li> <span class="q">"abcdef"</span> =~ <span class="q">/b.*e/</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"\$+ = $+\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: bcde</span></li><li> <span class="c"># perl5 prints:</span></li></ol></pre></li>
<li><a name="*-Substitution-now-returns-null-string-if-it-fails"></a><b>Substitution now returns null string if it fails</b>
<p>substitution now returns the null string if it fails</p>
<pre class="verbatim"><ol><li> <span class="i">$string</span> = <span class="q">"test"</span><span class="sc">;</span></li><li> <span class="i">$value</span> = <span class="s">(</span><span class="i">$string</span> =~ <span class="q">s/foo//</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$value</span><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: 0</span></li><li> <span class="c"># perl5 prints:</span></li></ol></pre><p>Also see <a href="#Numerical-Traps">Numerical Traps</a> for another example of this new feature.</p>
</li>
<li><a name="*-s%60lhs%60rhs%60-is-now-a-normal-substitution"></a><b><code class="inline"><a class="l_k" href="functions/s.html">s`lhs`rhs`</a></code> is now a normal substitution</b>
<p><code class="inline"><a class="l_k" href="functions/s.html">s`lhs`rhs`</a></code> (using backticks) is now a normal substitution, with no
backtick expansion</p>
<pre class="verbatim"><ol><li> <span class="i">$string</span> = <span class="q">""</span><span class="sc">;</span></li><li> <span class="i">$string</span> =~ <span class="q">s`^`hostname`</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">$string</span><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: <the local hostname></span></li><li> <span class="c"># perl5 prints: hostname</span></li></ol></pre></li>
<li><a name="*-Stricter-parsing-of-variables-in-regular-expressions"></a><b>Stricter parsing of variables in regular expressions</b>
<p>Stricter parsing of variables used in regular expressions</p>
<pre class="verbatim"><ol><li> <span class="q">s/^([^$grpc]*$grpc[$opt$plus$rep]?)//o</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4: compiles w/o error</span></li><li> <span class="c"># perl5: with Scalar found where operator expected ..., near "$opt$plus"</span></li></ol></pre><p>an added component of this example, apparently from the same script, is
the actual value of the s'd string after the substitution.
<code class="inline"><span class="s">[</span><span class="i">$opt</span><span class="s">]</span></code>
is a character class in perl4 and an array subscript in perl5</p>
<pre class="verbatim"><ol><li> <span class="i">$grpc</span> = <span class="q">'a'</span><span class="sc">;</span></li><li> <span class="i">$opt</span> = <span class="q">'r'</span><span class="sc">;</span></li><li> <span class="i">$_</span> = <span class="q">'bar'</span><span class="sc">;</span></li><li> <span class="q">s/^([^$grpc]*$grpc[$opt]?)/foo/</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: foo</span></li><li> <span class="c"># perl5 prints: foobar</span></li></ol></pre></li>
<li><a name="*-m%3fx%3f-matches-only-once"></a><b><code class="inline"><a class="l_k" href="functions/m.html">m?x?</a></code> matches only once</b>
<p>Under perl5, <code class="inline"><a class="l_k" href="functions/m.html">m?x?</a></code> matches only once, like <code class="inline"><span class="q">?x?</span></code>
. Under perl4, it matched
repeatedly, like <code class="inline"><span class="q">/x/</span></code>
or <code class="inline"><a class="l_k" href="functions/m.html">m!x!</a></code>.</p>
<pre class="verbatim"><ol><li> <span class="i">$test</span> = <span class="q">"once"</span><span class="sc">;</span></li><li><a name="match"></a> sub <span class="m">match</span> <span class="s">{</span> <span class="i">$test</span> =~ <span class="q">m?once?</span><span class="sc">;</span> <span class="s">}</span></li><li> <span class="i">&match</span><span class="s">(</span><span class="s">)</span><span class="sc">;</span></li><li> if<span class="s">(</span> <span class="i">&match</span><span class="s">(</span><span class="s">)</span> <span class="s">)</span> <span class="s">{</span></li><li> <span class="c"># m?x? matches more then once</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"perl4\n"</span><span class="sc">;</span></li><li> <span class="s">}</span> else <span class="s">{</span></li><li> <span class="c"># m?x? matches only once</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"perl5\n"</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li></li><li> <span class="c"># perl4 prints: perl4</span></li><li> <span class="c"># perl5 prints: perl5</span></li></ol></pre></li>
<li><a name="*-Failed-matches-don't-reset-the-match-variables"></a><b>Failed matches don't reset the match variables</b>
<p>Unlike in Ruby, failed matches in Perl do not reset the match variables
($1, $2, ..., <code class="inline"><span class="i">$`</span></code>
, ...).</p>
</li>
</ul>
<a name="Subroutine%2c-Signal%2c-Sorting-Traps"></a><h2>Subroutine, Signal, Sorting Traps</h2>
<p>The general group of Perl4-to-Perl5 traps having to do with
Signals, Sorting, and their related subroutines, as well as
general subroutine traps. Includes some OS-Specific traps.</p>
<ul>
<li><a name="*-Barewords-that-used-to-look-like-strings-look-like-subroutine-calls"></a><b>Barewords that used to look like strings look like subroutine calls</b>
<p>Barewords that used to look like strings to Perl will now look like subroutine
calls if a subroutine by that name is defined before the compiler sees them.</p>
<pre class="verbatim"><ol><li><a name="SeeYa"></a> sub <span class="m">SeeYa</span> <span class="s">{</span> <a class="l_k" href="functions/warn.html">warn</a><span class="q">"Hasta la vista, baby!"</span> <span class="s">}</span></li><li> <span class="i">$SIG</span>{<span class="q">'TERM'</span>} = <span class="i">SeeYa</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"SIGTERM is now $SIG{'TERM'}\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: SIGTERM is now main'SeeYa</span></li><li> <span class="c"># perl5 prints: SIGTERM is now main::1 (and warns "Hasta la vista, baby!")</span></li></ol></pre><p>Use <b>-w</b> to catch this one</p>
</li>
<li><a name="*-Reverse-is-no-longer-allowed-as-the-name-of-a-sort-subroutine"></a><b>Reverse is no longer allowed as the name of a sort subroutine</b>
<p>reverse is no longer allowed as the name of a sort subroutine.</p>
<pre class="verbatim"><ol><li><a name="reverse"></a> sub <span class="m">reverse</span><span class="s">{</span> <a class="l_k" href="functions/print.html">print</a> <span class="q">"yup "</span><span class="sc">;</span> <span class="i">$a</span> <=> <span class="i">$b</span> <span class="s">}</span></li><li> <a class="l_k" href="functions/print.html">print</a> <a class="l_k" href="functions/sort.html">sort</a> <a class="l_k" href="functions/reverse.html">reverse</a> <span class="s">(</span><span class="n">2</span><span class="cm">,</span><span class="n">1</span><span class="cm">,</span><span class="n">3</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: yup yup 123</span></li><li> <span class="c"># perl5 prints: 123</span></li><li> <span class="c"># perl5 warns (if using -w): Ambiguous call resolved as CORE::reverse()</span></li></ol></pre></li>
<li><a name="*-warn()-won't-let-you-specify-a-filehandle."></a><b><code class="inline"><a class="l_k" href="functions/warn.html">warn()</a></code> won't let you specify a filehandle.</b>
<p>Although it _always_ printed to STDERR, warn() would let you specify a
filehandle in perl4. With perl5 it does not.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/warn.html">warn</a> <span class="w">STDERR</span> <span class="q">"Foo!"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: Foo!</span></li><li> <span class="c"># perl5 prints: String found where operator expected</span></li></ol></pre></li>
</ul>
<a name="OS-Traps"></a><h2>OS Traps</h2>
<ul>
<li><a name="*-SysV-resets-signal-handler-correctly"></a><b>SysV resets signal handler correctly</b>
<p>Under HPUX, and some other SysV OSes, one had to reset any signal handler,
within the signal handler function, each time a signal was handled with
perl4. With perl5, the reset is now done correctly. Any code relying
on the handler _not_ being reset will have to be reworked.</p>
<p>Since version 5.002, Perl uses sigaction() under SysV.</p>
<pre class="verbatim"><ol><li><a name="gotit"></a> sub <span class="m">gotit</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"Got @_... "</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <span class="i">$SIG</span>{<span class="q">'INT'</span>} = <span class="q">'gotit'</span><span class="sc">;</span></li><li></li><li> <span class="i">$|</span> = <span class="n">1</span><span class="sc">;</span></li><li> <span class="i">$pid</span> = <a class="l_k" href="functions/fork.html">fork</a><span class="sc">;</span></li><li> if <span class="s">(</span><span class="i">$pid</span><span class="s">)</span> <span class="s">{</span></li><li> <a class="l_k" href="functions/kill.html">kill</a><span class="s">(</span><span class="q">'INT'</span><span class="cm">,</span> <span class="i">$pid</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/sleep.html">sleep</a><span class="s">(</span><span class="n">1</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/kill.html">kill</a><span class="s">(</span><span class="q">'INT'</span><span class="cm">,</span> <span class="i">$pid</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="s">}</span> else <span class="s">{</span></li><li> while <span class="s">(</span><span class="n">1</span><span class="s">)</span> <span class="s">{</span><a class="l_k" href="functions/sleep.html">sleep</a><span class="s">(</span><span class="n">10</span><span class="s">)</span><span class="sc">;</span><span class="s">}</span></li><li> <span class="s">}</span></li><li></li><li> <span class="c"># perl4 (HPUX) prints: Got INT...</span></li><li> <span class="c"># perl5 (HPUX) prints: Got INT... Got INT...</span></li></ol></pre></li>
<li><a name="*-SysV-seek()-appends-correctly"></a><b>SysV <code class="inline"><a class="l_k" href="functions/seek.html">seek()</a></code> appends correctly</b>
<p>Under SysV OSes, <code class="inline"><a class="l_k" href="functions/seek.html">seek()</a></code> on a file opened to append <code class="inline">>></code>
now does
the right thing w.r.t. the fopen() manpage. e.g., - When a file is opened
for append, it is impossible to overwrite information already in
the file.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/open.html">open</a><span class="s">(</span><span class="w">TEST</span><span class="cm">,</span><span class="q">">>seek.test"</span><span class="s">)</span><span class="sc">;</span></li><li> <span class="i">$start</span> = <a class="l_k" href="functions/tell.html">tell</a> <span class="w">TEST</span><span class="sc">;</span></li><li> foreach<span class="s">(</span><span class="n">1</span> .. <span class="n">9</span><span class="s">)</span><span class="s">{</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">TEST</span> <span class="q">"$_ "</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <span class="i">$end</span> = <a class="l_k" href="functions/tell.html">tell</a> <span class="w">TEST</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/seek.html">seek</a><span class="s">(</span><span class="w">TEST</span><span class="cm">,</span><span class="i">$start</span><span class="cm">,</span><span class="n">0</span><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">TEST</span> <span class="q">"18 characters here"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 (solaris) seek.test has: 18 characters here</span></li><li> <span class="c"># perl5 (solaris) seek.test has: 1 2 3 4 5 6 7 8 9 18 characters here</span></li></ol></pre></li>
</ul>
<a name="Interpolation-Traps"></a><h2>Interpolation Traps</h2>
<p>Perl4-to-Perl5 traps having to do with how things get interpolated
within certain expressions, statements, contexts, or whatever.</p>
<ul>
<li><a name="*-%40-always-interpolates-an-array-in-double-quotish-strings"></a><b><code class="inline"><span class="i">@</span></code>
always interpolates an array in double-quotish strings</b>
<p>@ now always interpolates an array in double-quotish strings.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"To: someone@somewhere.com\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: To:someone@somewhere.com</span></li><li> <span class="c"># perl < 5.6.1, error : In string, @somewhere now must be written as \@somewhere</span></li><li> <span class="c"># perl >= 5.6.1, warning : Possible unintended interpolation of @somewhere in string</span></li></ol></pre></li>
<li><a name="*-Double-quoted-strings-may-no-longer-end-with-an-unescaped-%24"></a><b>Double-quoted strings may no longer end with an unescaped $</b>
<p>Double-quoted strings may no longer end with an unescaped $.</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> = <span class="q">"foo$"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"foo is $foo\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: foo is foo$</span></li><li> <span class="c"># perl5 errors: Final $ should be \$ or $name</span></li></ol></pre><p>Note: perl5 DOES NOT error on the terminating @ in $bar</p>
</li>
<li><a name="*-Arbitrary-expressions-are-evaluated-inside-braces-within-double-quotes"></a><b>Arbitrary expressions are evaluated inside braces within double quotes</b>
<p>Perl now sometimes evaluates arbitrary expressions inside braces that occur
within double quotes (usually when the opening brace is preceded by <code class="inline"><span class="i">$</span></code>
or <code class="inline"><span class="i">@</span></code>
).</p>
<pre class="verbatim"><ol><li> <span class="i">@www</span> = <span class="q">"buz"</span><span class="sc">;</span></li><li> <span class="i">$foo</span> = <span class="q">"foo"</span><span class="sc">;</span></li><li> <span class="i">$bar</span> = <span class="q">"bar"</span><span class="sc">;</span></li><li><a name="foo"></a> sub <span class="m">foo</span> <span class="s">{</span> <a class="l_k" href="functions/return.html">return</a> <span class="q">"bar"</span> <span class="s">}</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"|@{w.w.w}|${main'foo}|"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: |@{w.w.w}|foo|</span></li><li> <span class="c"># perl5 prints: |buz|bar|</span></li></ol></pre><p>Note that you can <code class="inline"><a class="l_k" href="functions/use.html">use</a> <span class="w">strict</span><span class="sc">;</span></code>
to ward off such trappiness under perl5.</p>
</li>
<li><a name="*-%24%24x-now-tries-to-dereference-%24x"></a><b><code class="inline"><span class="i">$$x</span></code>
now tries to dereference $x</b>
<p>The construct "this is $$x" used to interpolate the pid at that point, but
now tries to dereference $x. <code class="inline"><span class="i">$$</span></code>
by itself still works fine, however.</p>
<pre class="verbatim"><ol><li> <span class="i">$s</span> = <span class="q">"a reference"</span><span class="sc">;</span></li><li> <span class="i">$x</span> = <span class="i">*s</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"this is $$x\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: this is XXXx (XXX is the current pid)</span></li><li> <span class="c"># perl5 prints: this is a reference</span></li></ol></pre></li>
<li><a name="*-Creation-of-hashes-on-the-fly-with-eval-%22EXPR%22-requires-protection"></a><b>Creation of hashes on the fly with <code class="inline"><a class="l_k" href="functions/eval.html">eval</a> <span class="q">"EXPR"</span></code>
requires protection</b>
<p>Creation of hashes on the fly with <code class="inline"><a class="l_k" href="functions/eval.html">eval</a> <span class="q">"EXPR"</span></code>
now requires either both
<code class="inline"><span class="i">$</span></code>
's to be protected in the specification of the hash name, or both curlies
to be protected. If both curlies are protected, the result will be compatible
with perl4 and perl5. This is a very common practice, and should be changed
to use the block form of <code class="inline"><a class="l_k" href="functions/eval.html">eval{}</a></code> if possible.</p>
<pre class="verbatim"><ol><li> <span class="i">$hashname</span> = <span class="q">"foobar"</span><span class="sc">;</span></li><li> <span class="i">$key</span> = <span class="q">"baz"</span><span class="sc">;</span></li><li> <span class="i">$value</span> = <span class="n">1234</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/eval.html">eval</a> <span class="q">"\$$hashname{'$key'} = q|$value|"</span><span class="sc">;</span></li><li> <span class="s">(</span><a class="l_k" href="functions/defined.html">defined</a><span class="s">(</span><span class="i">$foobar</span>{<span class="q">'baz'</span>}<span class="s">)</span><span class="s">)</span> ? <span class="s">(</span><a class="l_k" href="functions/print.html">print</a> <span class="q">"Yup"</span><span class="s">)</span> <span class="co">:</span> <span class="s">(</span><a class="l_k" href="functions/print.html">print</a> <span class="q">"Nope"</span><span class="s">)</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: Yup</span></li><li> <span class="c"># perl5 prints: Nope</span></li></ol></pre><p>Changing</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/eval.html">eval</a> <span class="q">"\$$hashname{'$key'} = q|$value|"</span><span class="sc">;</span></li></ol></pre><p>to</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/eval.html">eval</a> <span class="q">"\$\$hashname{'$key'} = q|$value|"</span><span class="sc">;</span></li></ol></pre><p>causes the following result:</p>
<pre class="verbatim"><ol><li> <span class="c"># perl4 prints: Nope</span></li><li> <span class="c"># perl5 prints: Yup</span></li></ol></pre><p>or, changing to</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/eval.html">eval</a> <span class="q">"\$$hashname\{'$key'\} = q|$value|"</span><span class="sc">;</span></li></ol></pre><p>causes the following result:</p>
<pre class="verbatim"><ol><li> <span class="c"># perl4 prints: Yup</span></li><li> <span class="c"># perl5 prints: Yup</span></li><li> <span class="c"># and is compatible for both versions</span></li></ol></pre></li>
<li><a name="*-Bugs-in-earlier-perl-versions"></a><b>Bugs in earlier perl versions</b>
<p>perl4 programs which unconsciously rely on the bugs in earlier perl versions.</p>
<pre class="verbatim"><ol><li> <span class="w">perl</span> -e <span class="q">'$bar=q/not/; print "This is $foo{$bar} perl5"'</span></li><li></li><li> <span class="c"># perl4 prints: This is not perl5</span></li><li> <span class="c"># perl5 prints: This is perl5</span></li></ol></pre></li>
<li><a name="*-Array-and-hash-brackets-during-interpolation"></a><b>Array and hash brackets during interpolation</b>
<p>You also have to be careful about array and hash brackets during
interpolation.</p>
<pre class="verbatim"><ol><li> print "$foo["</li><li></li><li> perl 4 prints: [</li><li> perl 5 prints: syntax error</li><li></li><li> print "$foo{"</li><li></li><li> perl 4 prints: {</li><li> perl 5 prints: syntax error</li></ol></pre><p>Perl 5 is expecting to find an index or key name following the respective
brackets, as well as an ending bracket of the appropriate type. In order
to mimic the behavior of Perl 4, you must escape the bracket like so.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"$foo\["</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"$foo\{"</span><span class="sc">;</span></li></ol></pre></li>
<li><a name="*-Interpolation-of-%5c%24%24foo%7bbar%7d"></a><b>Interpolation of <code class="inline">\<span class="i">$$foo</span>{<span class="w">bar</span>}</code>
</b>
<p>Similarly, watch out for: <code class="inline">\<span class="i">$$foo</span>{<span class="w">bar</span>}</code>
</p>
<pre class="verbatim"><ol><li> <span class="i">$foo</span> = <span class="q">"baz"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"\$$foo{bar}\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: $baz{bar}</span></li><li> <span class="c"># perl5 prints: $</span></li></ol></pre><p>Perl 5 is looking for <code class="inline"><span class="i">$foo</span>{<span class="w">bar</span>}</code>
which doesn't exist, but perl 4 is
happy just to expand $foo to "baz" by itself. Watch out for this
especially in <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code>'s.</p>
</li>
<li><a name="*-qq()-string-passed-to-eval-will-not-find-string-terminator"></a><b><code class="inline"><a class="l_k" href="functions/qq.html">qq()</a></code> string passed to <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code> will not find string terminator</b>
<p><code class="inline"><a class="l_k" href="functions/qq.html">qq()</a></code> string passed to <code class="inline"><a class="l_k" href="functions/eval.html">eval</a></code></p>
<pre class="verbatim"><ol><li> eval qq(</li><li> foreach \$y (keys %\$x\) {</li><li> \$count++;</li><li> }</li><li> );</li><li></li><li> # perl4 runs this ok</li><li> # perl5 prints: Can't find string terminator ")"</li></ol></pre></li>
</ul>
<a name="DBM-Traps"></a><h2>DBM Traps</h2>
<p>General DBM traps.</p>
<ul>
<li><a name="*-Perl5-must-have-been-linked-with-same-dbm%2fndbm-as-the-default-for-dbmopen()"></a><b>Perl5 must have been linked with same dbm/ndbm as the default for <code class="inline"><a class="l_k" href="functions/dbmopen.html">dbmopen()</a></code></b>
<p>Existing dbm databases created under perl4 (or any other dbm/ndbm tool)
may cause the same script, run under perl5, to fail. The build of perl5
must have been linked with the same dbm/ndbm as the default for <code class="inline"><a class="l_k" href="functions/dbmopen.html">dbmopen()</a></code>
to function properly without <code class="inline"><a class="l_k" href="functions/tie.html">tie</a></code>'ing to an extension dbm implementation.</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/dbmopen.html">dbmopen</a> <span class="s">(</span><span class="i">%dbm</span><span class="cm">,</span> <span class="q">"file"</span><span class="cm">,</span> <a class="l_k" href="functions/undef.html">undef</a><span class="s">)</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="q">"ok\n"</span><span class="sc">;</span></li><li></li><li> <span class="c"># perl4 prints: ok</span></li><li> <span class="c"># perl5 prints: ok (IFF linked with -ldbm or -lndbm)</span></li></ol></pre></li>
<li><a name="*-DBM-exceeding-limit-on-the-key%2fvalue-size-will-cause-perl5-to-exit-immediately"></a><b>DBM exceeding limit on the key/value size will cause perl5 to exit immediately</b>
<p>Existing dbm databases created under perl4 (or any other dbm/ndbm tool)
may cause the same script, run under perl5, to fail. The error generated
when exceeding the limit on the key/value size will cause perl5 to exit
immediately.</p>
<pre class="verbatim"><ol><li> dbmopen(DB, "testdb",0600) || die "couldn't open db! $!";</li><li> $DB{'trap'} = "x" x 1024; # value too large for most dbm/ndbm</li><li> print "YUP\n";</li><li></li><li> # perl4 prints:</li><li> dbm store returned -1, errno 28, key "trap" at - line 3.</li><li> YUP</li><li></li><li> # perl5 prints:</li><li> dbm store returned -1, errno 28, key "trap" at - line 3.</li></ol></pre></li>
</ul>
<a name="Unclassified-Traps"></a><h2>Unclassified Traps</h2>
<p>Everything else.</p>
<ul>
<li><a name="*-require%2fdo-trap-using-returned-value"></a><b><code class="inline"><a class="l_k" href="functions/require.html">require</a></code>/<code class="inline"><a class="l_k" href="functions/do.html">do</a></code> trap using returned value</b>
<p>If the file doit.pl has:</p>
<pre class="verbatim"><ol><li><a name="foo"></a> sub <span class="m">foo</span> <span class="s">{</span></li><li> <span class="i">$rc</span> = <a class="l_k" href="functions/do.html">do</a> <span class="q">"./do.pl"</span><span class="sc">;</span></li><li> <a class="l_k" href="functions/return.html">return</a> <span class="n">8</span><span class="sc">;</span></li><li> <span class="s">}</span></li><li> <a class="l_k" href="functions/print.html">print</a> <span class="i">&foo</span><span class="cm">,</span> <span class="q">"\n"</span><span class="sc">;</span></li></ol></pre><p>And the do.pl file has the following single line:</p>
<pre class="verbatim"><ol><li> <a class="l_k" href="functions/return.html">return</a> <span class="n">3</span><span class="sc">;</span></li></ol></pre><p>Running doit.pl gives the following:</p>
<pre class="verbatim"><ol><li> <span class="c"># perl 4 prints: 3 (aborts the subroutine early)</span></li><li> <span class="c"># perl 5 prints: 8</span></li></ol></pre><p>Same behavior if you replace <code class="inline"><a class="l_k" href="functions/do.html">do</a></code> with <code class="inline"><a class="l_k" href="functions/require.html">require</a></code>.</p>
</li>
<li><a name="*-split-on-empty-string-with-LIMIT-specified"></a><b><code class="inline"><a class="l_k" href="functions/split.html">split</a></code> on empty string with LIMIT specified</b>
<pre class="verbatim"><ol><li> <span class="i">$string</span> = <span class="q">''</span><span class="sc">;</span></li><li> <span class="i">@list</span> = <a class="l_k" href="functions/split.html">split</a><span class="s">(</span><span class="q">/foo/</span><span class="cm">,</span> <span class="i">$string</span><span class="cm">,</span> <span class="n">2</span><span class="s">)</span></li></ol></pre><p>Perl4 returns a one element list containing the empty string but Perl5
returns an empty list.</p>
</li>
</ul>
<p>As always, if any of these are ever officially declared as bugs,
they'll be fixed and removed.</p>
<div id="page_index" class="hud_container">
<div id="page_index_header" class="hud_header">
<div id="page_index_close" class="hud_close"><a href="#" onClick="pageIndex.hide();return false;"></a></div>
<div id="page_index_title" class="hud_title"><span class="hud_span_top">Page index</span></div>
<div id="page_index_topright" class="hud_topright"></div>
</div>
<div id="page_index_content" class="hud_content">
<ul><li><a href="#NAME">NAME</a><li><a href="#DESCRIPTION">DESCRIPTION</a><ul><li><a href="#Awk-Traps">Awk Traps</a><li><a href="#C%2fC%2b%2b-Traps">C/C++ Traps</a><li><a href="#Sed-Traps">Sed Traps</a><li><a href="#Shell-Traps">Shell Traps</a><li><a href="#Perl-Traps">Perl Traps</a><li><a href="#Perl4-to-Perl5-Traps">Perl4 to Perl5 Traps</a><li><a href="#Discontinuance%2c-Deprecation%2c-and-BugFix-traps">Discontinuance, Deprecation, and BugFix traps</a><li><a href="#Parsing-Traps">Parsing Traps</a><li><a href="#Numerical-Traps">Numerical Traps</a><li><a href="#General-data-type-traps">General data type traps</a><li><a href="#Context-Traps---scalar%2c-list-contexts">Context Traps - scalar, list contexts</a><li><a href="#Precedence-Traps">Precedence Traps</a><li><a href="#General-Regular-Expression-Traps-using-s%2f%2f%2f%2c-etc.">General Regular Expression Traps using s///, etc.</a><li><a href="#Subroutine%2c-Signal%2c-Sorting-Traps">Subroutine, Signal, Sorting Traps</a><li><a href="#OS-Traps">OS Traps</a><li><a href="#Interpolation-Traps">Interpolation Traps</a><li><a href="#DBM-Traps">DBM Traps</a><li><a href="#Unclassified-Traps">Unclassified Traps</a></ul></ul>
</div>
<div id="page_index_footer" class="hud_footer">
<div id="page_index_bottomleft" class="hud_bottomleft"></div>
<div id="page_index_bottom" class="hud_bottom"><span class="hud_span_bottom"></span></div>
<div id="page_index_resize" class="hud_resize"></div>
</div>
</div>
</div>
<div id="content_footer">
</div>
</div>
<div class="clear"></div>
</div>
<div id="footer">
<div id="footer_content">
<div id="footer_strapline">
perldoc.perl.org - Official documentation for the Perl programming language
</div>
<div id="footer_links">
<div id="address">
<p class="name">Contact details</p>
<p class="address">
Site maintained by <a href="http://perl.jonallen.info">Jon Allen (JJ)</a><br>
See the <a href="http://perl.jonallen.info/projects/perldoc">project page</a> for more details
</p>
<p class="contact">
Documentation maintained by the <a href="http://lists.cpan.org/showlist.cgi?name=perl5-porters">Perl 5 Porters</a>
</p>
</div>
<ul class="f1">
<li>Manual
<ul class="f2">
<li><a href="index-overview.html">Overview</a>
<li><a href="index-tutorials.html">Tutorials</a>
<li><a href="index-faq.html">FAQs</a>
<li><a href="index-history.html">Changes</a>
</ul>
<li>Reference
<ul class="f2">
<li><a href="index-language.html">Language</a>
<li><a href="index-functions.html">Functions</a>
<li><a href="perlop.html">Operators</a>
<li><a href="perlvar.html">Variables</a>
</ul>
<li>Modules
<ul class="f2">
<li><a href="index-modules-A.html">Modules</a>
<li><a href="index-pragmas.html">Pragmas</a>
<li><a href="index-utilities.html">Utilities</a>
</ul>
<li>Misc
<ul class="f2">
<li><a href="index-licence.html">License</a>
<li><a href="index-internals.html">Internals</a>
<li><a href="index-platforms.html">Platforms</a>
</ul> </ul>
<div class="clear"></div>
</div>
</div>
<div id="footer_end">
</div>
</div>
</div>
<script language="JavaScript" type="text/javascript" src="static/exploreperl.js"></script>
<script language="JavaScript" src="static/combined-20100403.js" type="text/javascript"></script>
<script language="JavaScript" type="text/javascript">
perldoc.setPath(0);
perldoc.pageName = 'perltrap';
perldoc.pageAddress = 'perltrap.html';
perldoc.contentPage = 1;
explorePerl.render();
explorePerl.addEvents('explore_anchor');
</script>
</body>
</html>
|