{
    "mode": "perldoc",
    "parameter": "perltrap",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/perltrap/json",
    "generated": "2026-12-03T14:58:24Z",
    "sections": {
        "NAME": {
            "content": "perltrap - Perl traps for the unwary\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "The biggest trap of all is forgetting to \"use warnings\" or use the -w switch; see warnings and\n\"-w\" in perlrun. The second biggest trap is not making your entire program runnable under \"use\nstrict\". The third biggest trap is not reading the list of changes in this version of Perl; see\nperldelta.\n",
            "subsections": [
                {
                    "name": "Awk Traps",
                    "content": "Accustomed awk users should take special note of the following:\n\n*   A Perl program executes only once, not once for each input line. You can do an implicit loop\nwith \"-n\" or \"-p\".\n\n*   The English module, loaded via\n\nuse English;\n\nallows you to refer to special variables (like $/) with names (like $RS), as though they\nwere in awk; see perlvar for details.\n\n*   Semicolons are required after all simple statements in Perl (except at the end of a block).\nNewline is not a statement delimiter.\n\n*   Curly brackets are required on \"if\"s and \"while\"s.\n\n*   Variables begin with \"$\", \"@\" or \"%\" in Perl.\n\n*   Arrays index from 0. Likewise string positions in substr() and index().\n\n*   You have to decide whether your array has numeric or string indices.\n\n*   Hash values do not spring into existence upon mere reference.\n\n*   You have to decide whether you want to use string or numeric comparisons.\n\n*   Reading an input line does not split it for you. You get to split it to an array yourself.\nAnd the split() operator has different arguments than awk's.\n\n*   The current input line is normally in $, not $0. It generally does not have the newline\nstripped. ($0 is the name of the program executed.) See perlvar.\n\n*   $<*digit*> does not refer to fields--it refers to substrings matched by the last match\npattern.\n\n*   The print() statement does not add field and record separators unless you set $, and $\\. You\ncan set $OFS and $ORS if you're using the English module.\n\n*   You must open your files before you print to them.\n\n*   The range operator is \"..\", not comma. The comma operator works as in C.\n\n*   The match operator is \"=~\", not \"~\". (\"~\" is the one's complement operator, as in C.)\n\n*   The exponentiation operator is \"\", not \"^\". \"^\" is the XOR operator, as in C. (You know,\none could get the feeling that awk is basically incompatible with C.)\n\n*   The concatenation operator is \".\", not the null string. (Using the null string would render\n\"/pat/ /pat/\" unparsable, because the third slash would be interpreted as a division\noperator--the tokenizer is in fact slightly context sensitive for operators like \"/\", \"?\",\nand \">\". And in fact, \".\" itself can be the beginning of a number.)\n\n*   The \"next\", \"exit\", and \"continue\" keywords work differently.\n\n*   The following variables work differently:\n\nAwk       Perl\nARGC      scalar @ARGV (compare with $#ARGV)\nARGV[0]   $0\nFILENAME  $ARGV\nFNR       $. - something\nFS        (whatever you like)\nNF        $#Fld, or some such\nNR        $.\nOFMT      $#\nOFS       $,\nORS       $\\\nRLENGTH   length($&)\nRS        $/\nRSTART    length($`)\nSUBSEP    $;\n\n*   You cannot set $RS to a pattern, only a string.\n\n*   When in doubt, run the awk construct through a2p and see what it gives you.\n\nC/C++ Traps\nCerebral C and C++ programmers should take note of the following:\n\n*   Curly brackets are required on \"if\"'s and \"while\"'s.\n\n*   You must use \"elsif\" rather than \"else if\".\n\n*   The \"break\" and \"continue\" keywords from C become in Perl \"last\" and \"next\", respectively.\nUnlike in C, these do *not* work within a \"do { } while\" construct. See \"Loop Control\" in\nperlsyn.\n\n*   The switch statement is called \"given\"/\"when\" and only available in perl 5.10 or newer. See\n\"Switch Statements\" in perlsyn.\n\n*   Variables begin with \"$\", \"@\" or \"%\" in Perl.\n\n*   Comments begin with \"#\", not \"/*\" or \"//\". Perl may interpret C/C++ comments as division\noperators, unterminated regular expressions or the defined-or operator.\n\n*   You can't take the address of anything, although a similar operator in Perl is the\nbackslash, which creates a reference.\n\n*   \"ARGV\" must be capitalized. $ARGV[0] is C's \"argv[1]\", and \"argv[0]\" ends up in $0.\n\n*   System calls such as link(), unlink(), rename(), etc. return nonzero for success, not 0.\n(system(), however, returns zero for success.)\n\n*   Signal handlers deal with signal names, not numbers. Use \"kill -l\" to find their names on\nyour system.\n"
                },
                {
                    "name": "JavaScript Traps",
                    "content": "Judicious JavaScript programmers should take note of the following:\n\n*   In Perl, binary \"+\" is always addition. \"$string1 + $string2\" converts both strings to\nnumbers and then adds them. To concatenate two strings, use the \".\" operator.\n\n*   The \"+\" unary operator doesn't do anything in Perl. It exists to avoid syntactic\nambiguities.\n\n*   Unlike \"for...in\", Perl's \"for\" (also spelled \"foreach\") does not allow the left-hand side\nto be an arbitrary expression. It must be a variable:\n\nfor my $variable (keys %hash) {\n...\n}\n\nFurthermore, don't forget the \"keys\" in there, as \"foreach my $kv (%hash) {}\" iterates over\nthe keys and values, and is generally not useful ($kv would be a key, then a value, and so\non).\n\n*   To iterate over the indices of an array, use \"foreach my $i (0 .. $#array) {}\". \"foreach my\n$v (@array) {}\" iterates over the values.\n\n*   Perl requires braces following \"if\", \"while\", \"foreach\", etc.\n\n*   In Perl, \"else if\" is spelled \"elsif\".\n\n*   \"? :\" has higher precedence than assignment. In JavaScript, one can write:\n\ncondition ? dosomething() : variable = 3\n\nand the variable is only assigned if the condition is false. In Perl, you need parentheses:\n\n$condition ? dosomething() : ($variable = 3);\n\nOr just use \"if\".\n\n*   Perl requires semicolons to separate statements.\n\n*   Variables declared with \"my\" only affect code *after* the declaration. You cannot write \"$x\n= 1; my $x;\" and expect the first assignment to affect the same variable. It will instead\nassign to an $x declared previously in an outer scope, or to a global variable.\n\nNote also that the variable is not visible until the following *statement*. This means that\nin \"my $x = 1 + $x\" the second $x refers to one declared previously.\n\n*   \"my\" variables are scoped to the current block, not to the current function. If you write\n\"{my $x;} $x;\", the second $x does not refer to the one declared inside the block.\n\n*   An object's members cannot be made accessible as variables. The closest Perl equivalent to\n\"with(object) { method() }\" is \"for\", which can alias $ to the object:\n\nfor ($object) {\n$->method;\n}\n\n*   The object or class on which a method is called is passed as one of the method's arguments,\nnot as a separate \"this\" value.\n"
                },
                {
                    "name": "Sed Traps",
                    "content": "Seasoned sed programmers should take note of the following:\n\n*   A Perl program executes only once, not once for each input line. You can do an implicit loop\nwith \"-n\" or \"-p\".\n\n*   Backreferences in substitutions use \"$\" rather than \"\\\".\n\n*   The pattern matching metacharacters \"(\", \")\", and \"|\" do not have backslashes in front.\n\n*   The range operator is \"...\", rather than comma.\n"
                },
                {
                    "name": "Shell Traps",
                    "content": "Sharp shell programmers should take note of the following:\n\n*   The backtick operator does variable interpolation without regard to the presence of single\nquotes in the command.\n\n*   The backtick operator does no translation of the return value, unlike csh.\n\n*   Shells (especially csh) do several levels of substitution on each command line. Perl does\nsubstitution in only certain constructs such as double quotes, backticks, angle brackets,\nand search patterns.\n\n*   Shells interpret scripts a little bit at a time. Perl compiles the entire program before\nexecuting it (except for \"BEGIN\" blocks, which execute at compile time).\n\n*   The arguments are available via @ARGV, not $1, $2, etc.\n\n*   The environment is not automatically made available as separate scalar variables.\n\n*   The shell's \"test\" uses \"=\", \"!=\", \"<\" etc for string comparisons and \"-eq\", \"-ne\", \"-lt\"\netc for numeric comparisons. This is the reverse of Perl, which uses \"eq\", \"ne\", \"lt\" for\nstring comparisons, and \"==\", \"!=\" \"<\" etc for numeric comparisons.\n"
                },
                {
                    "name": "Perl Traps",
                    "content": "Practicing Perl Programmers should take note of the following:\n\n*   Remember that many operations behave differently in a list context than they do in a scalar\none. See perldata for details.\n\n*   Avoid barewords if you can, especially all lowercase ones. You can't tell by just looking at\nit whether a bareword is a function or a string. By using quotes on strings and parentheses\non function calls, you won't ever get them confused.\n\n*   You cannot discern from mere inspection which builtins are unary operators (like chop() and\nchdir()) and which are list operators (like print() and unlink()). (Unless prototyped,\nuser-defined subroutines can only be list operators, never unary ones.) See perlop and\nperlsub.\n\n*   People have a hard time remembering that some functions default to $, or @ARGV, or\nwhatever, but that others which you might expect to do not.\n\n*   The <FH> construct is not the name of the filehandle, it is a readline operation on that\nhandle. The data read is assigned to $ only if the file read is the sole condition in a\nwhile loop:\n\nwhile (<FH>)      { }\nwhile (defined($ = <FH>)) { }..\n<FH>;  # data discarded!\n\n*   Remember not to use \"=\" when you need \"=~\"; these two constructs are quite different:\n\n$x =  /foo/;\n$x =~ /foo/;\n\n*   The \"do {}\" construct isn't a real loop that you can use loop control on.\n\n*   Use my() for local variables whenever you can get away with it (but see perlform for where\nyou can't). Using local() actually gives a local value to a global variable, which leaves\nyou open to unforeseen side-effects of dynamic scoping.\n\n*   If you localize an exported variable in a module, its exported value will not change. The\nlocal name becomes an alias to a new value but the external name is still an alias for the\noriginal.\n\nAs always, if any of these are ever officially declared as bugs, they'll be fixed and removed.\n"
                }
            ]
        }
    },
    "summary": "perltrap - Perl traps for the unwary",
    "flags": [],
    "examples": [],
    "see_also": []
}