{
    "mode": "man",
    "parameter": "PERLSTYLE",
    "section": "1",
    "url": "https://www.chedong.com/phpMan.php/man/PERLSTYLE/1/json",
    "generated": "2026-11-21T12:50:21Z",
    "sections": {
        "NAME": {
            "content": "perlstyle - Perl style guide\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "Each programmer will, of course, have his or her own preferences in regards to formatting,\nbut there are some general guidelines that will make your programs easier to read,\nunderstand, and maintain.\n\nThe most important thing is to use strict and warnings in all your code or know the reason\nwhy not to.  You may turn them off explicitly for particular portions of code via \"no\nwarnings\" or \"no strict\", and this can be limited to the specific warnings or strict features\nyou wish to disable.  The -w flag and $^W variable should not be used for this purpose since\nthey can affect code you use but did not write, such as modules from core or CPAN.\n\nA concise way to arrange for this is to use the \"use VERSION\" syntax, requesting a version\n5.36 or above, which will enable both the \"strict\" and \"warnings\" pragmata (as well as\nseveral other useful named features).\n\nuse v5.36;\n\nRegarding aesthetics of code layout, about the only thing Larry cares strongly about is that\nthe closing curly bracket of a multi-line BLOCK should line up with the keyword that started\nthe construct.  Beyond that, he has other preferences that aren't so strong:\n\n•   4-column indent.\n\n•   Opening curly on same line as keyword, if possible, otherwise line up.\n\n•   Space before the opening curly of a multi-line BLOCK.\n\n•   One-line BLOCK may be put on one line, including curlies.\n\n•   No space before the semicolon.\n\n•   Semicolon omitted in \"short\" one-line BLOCK.\n\n•   Space around most operators.\n\n•   Space around a \"complex\" subscript (inside brackets).\n\n•   Blank lines between chunks that do different things.\n\n•   Uncuddled elses.\n\n•   No space between function name and its opening parenthesis.\n\n•   Space after each comma.\n\n•   Long lines broken after an operator (except \"and\" and \"or\").\n\n•   Space after last parenthesis matching on current line.\n\n•   Line up corresponding items vertically.\n\n•   Omit redundant punctuation as long as clarity doesn't suffer.\n\nLarry  has  his  reasons  for each of these things, but he doesn't claim that everyone else's\nmind works the same as his does.\n\nHere are some other more substantive style issues to think about:\n\n•   Just because you CAN do something a particular way doesn't mean that  you  SHOULD  do  it\nthat  way.  Perl is designed to give you several ways to do anything, so consider picking\nthe most readable one.  For instance\n\nopen(my $fh, '<', $foo) || die \"Can't open $foo: $!\";\n\nis better than\n\ndie \"Can't open $foo: $!\" unless open(my $fh, '<', $foo);\n\nbecause the second way hides the main point of the statement in a modifier.  On the other\nhand\n\nprint \"Starting analysis\\n\" if $verbose;\n\nis better than\n\n$verbose && print \"Starting analysis\\n\";\n\nbecause the main point isn't whether the user typed -v or not.\n\nSimilarly, just because an operator lets you assume default arguments doesn't  mean  that\nyou  have  to  make  use  of  the  defaults.   The  defaults  are  there for lazy systems\nprogrammers writing one-shot programs.  If you want your program to be readable, consider\nsupplying the argument.\n\nAlong the same lines, just because you CAN omit parentheses in many places  doesn't  mean\nthat you ought to:\n\nreturn print reverse sort num values %array;\nreturn print(reverse(sort num (values(%array))));\n\nWhen  in  doubt, parenthesize.  At the very least it will let some poor schmuck bounce on\nthe % key in vi.\n\nEven if you aren't in doubt, consider the  mental  welfare  of  the  person  who  has  to\nmaintain the code after you, and who will probably put parentheses in the wrong place.\n\n•   Don't  go  through  silly  contortions to exit a loop at the top or the bottom, when Perl\nprovides the \"last\" operator so you can exit in the middle.  Just \"outdent\" it  a  little\nto make it more visible:\n\nLINE:\nfor (;;) {\nstatements;\nlast LINE if $foo;\nnext LINE if /^#/;\nstatements;\n}\n\n•   Don't  be  afraid  to use loop labels--they're there to enhance readability as well as to\nallow multilevel loop breaks.  See the previous example.\n\n•   Avoid using grep() (or map()) or `backticks` in a void context, that is,  when  you  just\nthrow  away  their  return  values.  Those functions all have return values, so use them.\nOtherwise use a foreach() loop or the system() function instead.\n\n•   For portability, when using features that may not be implemented on every  machine,  test\nthe  construct  in  an eval to see if it fails.  If you know what version or patchlevel a\nparticular feature was implemented, you can test $] ($PERLVERSION in \"English\")  to  see\nif it will be there.  The \"Config\" module will also let you interrogate values determined\nby the Configure program when Perl was installed.\n\n•   Choose  mnemonic  identifiers.   If  you can't remember what mnemonic means, you've got a\nproblem.\n\n•   While short identifiers like $gotit are probably ok, use underscores to separate words in\nlonger  identifiers.   It  is  generally  easier  to   read   $varnameslikethis   than\n$VarNamesLikeThis, especially for non-native speakers of English. It's also a simple rule\nthat works consistently with \"VARNAMESLIKETHIS\".\n\nPackage  names  are  sometimes  an  exception  to  this  rule.   Perl informally reserves\nlowercase module names for \"pragma\" modules like \"integer\" and \"strict\".   Other  modules\nshould  begin  with a capital letter and use mixed case, but probably without underscores\ndue to limitations in primitive file systems' representations of module  names  as  files\nthat must fit into a few sparse bytes.\n\n•   You may find it helpful to use letter case to indicate the scope or nature of a variable.\nFor example:\n\n$ALLCAPSHERE   constants only (beware clashes with perl vars!)\n$SomeCapsHere  package-wide global/static\n$nocapshere    function scope my() or local() variables\n\nFunction and method names seem to work best as all lowercase.  E.g., \"$obj->asstring()\".\n\nYou  can  use  a leading underscore to indicate that a variable or function should not be\nused outside the package that defined it.\n\n•   If you have a really hairy regular expression, use the \"/x\"  or \"/xx\" modifiers  and  put\nin  some  whitespace to make it look a little less like line noise.  Don't use slash as a\ndelimiter when your regexp has slashes or backslashes.\n\n•   Use the \"and\" and \"or\" operators to avoid having to parenthesize list operators so  much,\nand  to  reduce  the  incidence  of  punctuation operators like \"&&\" and \"||\".  Call your\nsubroutines as if they were functions or list operators to avoid excessive ampersands and\nparentheses.\n\n•   Use here documents instead of repeated print() statements.\n\n•   Line up corresponding things vertically, especially if it'd be too long  to  fit  on  one\nline anyway.\n\n$IDX = $STMTIME;\n$IDX = $STATIME       if $optu;\n$IDX = $STCTIME       if $optc;\n$IDX = $STSIZE        if $opts;\n\nmkdir $tmpdir, 0700 or die \"can't mkdir $tmpdir: $!\";\nchdir($tmpdir)      or die \"can't chdir $tmpdir: $!\";\nmkdir 'tmp',   0777 or die \"can't mkdir $tmpdir/tmp: $!\";\n\n•   Always  check  the  return  codes  of  system  calls.   Good  error messages should go to\n\"STDERR\", include which program caused the problem,  what  the  failed  system  call  and\narguments were, and (VERY IMPORTANT) should contain the standard system error message for\nwhat went wrong.  Here's a simple but sufficient example:\n\nopendir(my $dh, $dir)        or die \"can't opendir $dir: $!\";\n\n•   Line up your transliterations when it makes sense:\n\ntr [abc]\n[xyz];\n\n•   Think  about  reusability.   Why waste brainpower on a one-shot when you might want to do\nsomething like it again?  Consider generalizing your code.  Consider writing a module  or\nobject class.  Consider making your code run cleanly with \"use strict\" and \"use warnings\"\nin  effect.   Consider  giving  away your code.  Consider changing your whole world view.\nConsider... oh, never mind.\n\n•   Try to document your code and use Pod formatting in a consistent way. Here  are  commonly\nexpected conventions:\n\n•   use  \"C<>\"  for function, variable and module names (and more generally anything that\ncan be considered part of code, like  filehandles  or  specific  values).  Note  that\nfunction  names  are considered more readable with parentheses after their name, that\nis function().\n\n•   use \"B<>\" for commands names like cat or grep.\n\n•   use \"F<>\" or \"C<>\" for file names. \"F<>\" should be the only Pod code for file  names,\nbut  as  most  Pod  formatters render it as italic, Unix and Windows paths with their\nslashes and backslashes may be less readable, and better rendered with \"C<>\".\n\n•   Be consistent.\n\n•   Be nice.\n\nperl v5.38.2                                 2026-08-18                                 PERLSTYLE(1)",
            "subsections": []
        }
    },
    "summary": "perlstyle - Perl style guide",
    "flags": [],
    "examples": [],
    "see_also": []
}