Perl Programming

打开cperl-mode

让cperl-mode称为默认的perl编辑模式:

(defalias 'perl-mode 'cperl-mode)

在没有以.perl, .pl结尾,或者#!perl开始的文件中开启, 需要在文件开头以注释的形式选择:

#anything     -*-mode:perl;-*-

编辑源文件

察看文档

M-x cperl-perldoc
M-x cperl-perldoc-at-point

调试程序

cperl-mode is an interactive compiled Lisp function in `cperl-mode.el'. (cperl-mode)

Major mode for editing Perl code. Expression and list commands understand all C brackets. Tab indents for Perl code. Paragraphs are separated by blank lines only. Delete converts tabs to spaces as it moves back.

Various characters in Perl almost always come in pairs: {}, (), [], sometimes <>. When the user types the first, she gets the second as well, with optional special formatting done on {}. (Disabled by default.) You can always quote (with C-q) the left "paren" to avoid the expansion. The processing of < is special, since most the time you mean "less". CPerl mode tries to guess whether you want to type pair <>, and inserts is if it appropriate. You can set `cperl-electric-parens-string' to the string that contains the parenths from the above list you want to be electrical. Electricity of parenths is controlled by `cperl-electric-parens'. You may also set `cperl-electric-parens-mark' to have electric parens look for active mark and "embrace" a region if possible.'

CPerl mode provides expansion of the Perl control constructs:

if, else, elsif, unless, while, until, continue, do, for, foreach, formy and foreachmy.

and POD directives (Disabled by default, see `cperl-electric-keywords'.)

The user types the keyword immediately followed by a space, which causes the construct to be expanded, and the point is positioned where she is most likely to want to be. eg. when the user types a space following "if" the following appears in the buffer: if () { or if () } { } and the cursor is between the parentheses. The user can then type some boolean expression within the parens. Having done that, typing M-x cperl-linefeed places you - appropriately indented - on a new line between the braces (if you typed M-x cperl-linefeed in a POD directive line, then appropriate number of new lines is inserted).

If CPerl decides that you want to insert "English" style construct like

bite if angry;

it will not do any expansion. See also help on variable `cperl-extra-newline-before-brace'. (Note that one can switch the help message on expansion by setting `cperl-message-electric-keyword' to nil.)

M-x cperl-linefeed is a convenience replacement for typing carriage return. It places you in the next line with proper indentation, or if you type it inside the inline block of control construct, like

foreach (@lines) {print; print}

and you are on a boundary of a statement inside braces, it will transform the construct into a multiline and will place you into an appropriately indented blank line. If you need a usual `newline-and-indent' behavior, it is on C-j, see documentation on `cperl-electric-linefeed'.

Use M-x cperl-invert-if-unless to change a construction of the form

if (A) { B }

into

B if A;

key binding — ——-

C-c Prefix Command TAB cperl-indent-command C-j newline-and-indent ESC Prefix Command ( cperl-electric-paren ) cperl-electric-rparen : cperl-electric-terminator < cperl-electric-paren [ cperl-electric-paren ] cperl-electric-rparen { cperl-electric-lbrace } cperl-electric-brace DEL cperl-electric-backspace

C-M-q cperl-indent-exp C-M-\ cperl-indent-region C-M-| cperl-lineup

C-c C-a cperl-toggle-auto-newline C-c C-b cperl-find-bad-style C-c C-d cperl-here-doc-spell C-c C-e cperl-toggle-electric C-c C-f auto-fill-mode C-c C-h Prefix Command C-c C-j cperl-linefeed C-c C-k cperl-toggle-abbrev C-c C-n cperl-narrow-to-here-doc C-c C-p cperl-pod-spell C-c C-t cperl-invert-if-unless C-c C-v cperl-next-interpolated-REx C-c C-w cperl-toggle-construct-fix C-c C-x cperl-next-interpolated-REx-0 C-c C-y cperl-next-interpolated-REx-1

C-c C-h F cperl-info-on-command C-c C-h P cperl-perldoc-at-point C-c C-h a cperl-toggle-autohelp C-c C-h f cperl-info-on-current-command C-c C-h p cperl-perldoc C-c C-h v cperl-get-help

Setting the variable `cperl-font-lock' to t switches on font-lock-mode (even with older Emacsen), `cperl-electric-lbrace-space' to t switches on electric space between $ and {, `cperl-electric-parens-string' is the string that contains parentheses that should be electric in CPerl (see also `cperl-electric-parens-mark' and `cperl-electric-parens'), setting `cperl-electric-keywords' enables electric expansion of control structures in CPerl. `cperl-electric-linefeed' governs which one of two linefeed behavior is preferable. You can enable all these options simultaneously (recommended mode of use) by setting `cperl-hairy' to t. In this case you can switch separate options off by setting them to `null'. Note that one may undo the extra whitespace inserted by semis and braces in `auto-newline'-mode by consequent M-x cperl-electric-backspace.

If your site has perl5 documentation in info format, you can use commands M-x cperl-info-on-current-command and M-x cperl-info-on-command to access it. These keys run commands `cperl-info-on-current-command' and `cperl-info-on-command', which one is which is controlled by variable `cperl-info-on-command-no-prompt' and `cperl-clobber-lisp-bindings' (in turn affected by `cperl-hairy').

Even if you have no info-format documentation, short one-liner-style help is available on M-x cperl-get-help, and one can run perldoc or man via menu.

It is possible to show this help automatically after some idle time. This is regulated by variable `cperl-lazy-help-time'. Default with `cperl-hairy' (if the value of `cperl-lazy-help-time' is nil) is 5 secs idle time . It is also possible to switch this on/off from the menu, or via M-x cperl-toggle-autohelp. Requires `run-with-idle-timer'.

Use M-x cperl-lineup to vertically lineup some construction - put the beginning of the region at the start of construction, and make region span the needed amount of lines.

Variables `cperl-pod-here-scan', `cperl-pod-here-fontify', `cperl-pod-face', `cperl-pod-head-face' control processing of POD and here-docs sections. With capable Emaxen results of scan are used for indentation too, otherwise they are used for highlighting only.

Variables controlling indentation style:

`cperl-tab-always-indent' Non-nil means TAB in CPerl mode should always reindent the current line, regardless of where in the line point is when the TAB command is used. `cperl-indent-left-aligned-comments' Non-nil means that the comment starting in leftmost column should indent. `cperl-auto-newline' Non-nil means automatically newline before and after braces, and after colons and semicolons, inserted in Perl code. The following M-x cperl-electric-backspace will remove the inserted whitespace. Insertion after colons requires both this variable and `cperl-auto-newline-after-colon' set. `cperl-auto-newline-after-colon' Non-nil means automatically newline even after colons. Subject to `cperl-auto-newline' setting. `cperl-indent-level' Indentation of Perl statements within surrounding block. The surrounding block's indentation is the indentation of the line on which the open-brace appears. `cperl-continued-statement-offset' Extra indentation given to a substatement, such as the then-clause of an if, or body of a while, or just a statement continuation. `cperl-continued-brace-offset' Extra indentation given to a brace that starts a substatement. This is in addition to `cperl-continued-statement-offset'. `cperl-brace-offset' Extra indentation for line if it starts with an open brace. `cperl-brace-imaginary-offset' An open brace following other text is treated as if it the line started this far to the right of the actual line indentation. `cperl-label-offset' Extra indentation for line that is a label. `cperl-min-label-indent' Minimal indentation for line that is a label.

Settings for classic indent-styles: K&R BSD=C++ GNU Perl Style=Whitesmith

`cperl-indent-level' 5 4 2 4 `cperl-brace-offset' 0 0 0 0 `cperl-continued-brace-offset' -5 -4 0 0 `cperl-label-offset' -5 -4 -2 -4 `cperl-continued-statement-offset' 5 4 2 4

CPerl knows several indentation styles, and may bulk set the corresponding variables. Use M-x cperl-set-style to do this. Use M-x cperl-set-style-back to restore the memorized preexisting values (both available from menu). See examples in `cperl-style-examples'.

Part of the indentation style is how different parts of if/elsif/else statements are broken into lines; in CPerl, this is reflected on how templates for these constructs are created (controlled by `cperl-extra-newline-before-brace'), and how reflow-logic should treat "continuation" blocks of else/elsif/continue, controlled by the same variable, and by `cperl-extra-newline-before-brace-multiline', `cperl-merge-trailing-else', `cperl-indent-region-fix-constructs'.

If `cperl-indent-level' is 0, the statement after opening brace in column 0 is indented on `cperl-brace-offset'+`cperl-continued-statement-offset'.

Turning on CPerl mode calls the hooks in the variable `cperl-mode-hook' with no args.

DO NOT FORGET to read micro-docs (available from `Perl' menu) or as help on variables `cperl-tips', `cperl-problems', `cperl-praise', `cperl-speed'.

Wiki comments powered by Disqus