Beefy Boxes and Bandwidth Generously Provided by pair Networks
P is for Practical

Disable perltidy in a region?

by diotalevi (Canon)
on Jan 10, 2006 at 19:08 UTC ( #522285=perlquestion: print w/replies, xml ) Need Help??

diotalevi has asked for the wisdom of the Perl Monks concerning the following question:

Are there any pragmas, syntax, comments, or anything that would allow me to disable perltidy's reformatting in a region? Perltidy is fine most of the time but occasionally writes something utterly crapulous. I'd like to add my own whitespace in those cases.

I've instructed emacs to auto-tidy all the perl w/ the following code. I include it a bonus to anyone who reads this.

(defun cperl-save-buffer (&optional args) (interactive "p") (if (buffer-modified-p) (perltidy-buffer)) (save-buffer t)) (defun perltidy-buffer () (interactive) (save-excursion (shell-command-on-region (point-min) (point-max) "perltidy -st" nil t shell-command-default-error-buffer))) (add-to-list 'cperl-mode-hook (lambda () (substitute-key-definition 'save-buffer 'cperl-save-buffer cperl-m +ode-map global-map)))

Replies are listed 'Best First'.
Re: Disable perltidy in a region?
by ambrus (Abbot) on Jan 10, 2006 at 19:09 UTC

      That would work. It has a runtime penalty associated with it, though, if the eval happens more than once.

      I think.


        I don't think he's suggesting to definitively turn (possibly) large chunks of code into string evals and otoh diotalevi is not likely to do anything like that - unless it's for an astonishing trick of his, that is!

        I think he's suggesting to trick perltidy temporarily. I don't use perltidy myself, but my editor generally does a good job automatically indenting the code for me. However for Perl that's hard and occasionally fails. In those cases I insert comments and trick it into thinking the code is somewhat more "regular" than it actually is, so that it can indent it nicely. Then I undo the local modifications.

Re: Disable perltidy in a region?
by chrestomanci (Priest) on Sep 10, 2013 at 15:21 UTC

    There is a section in the perltidy documentation on how to do that. See: skipping_selected_sections_of_code

    In short, you should sourrond the code you don't want modified with  #<<< and  #>>> tags.

Log In?

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://522285]
Approved by larryp
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others perusing the Monastery: (6)
As of 2022-10-04 16:16 GMT
Find Nodes?
    Voting Booth?
    My preferred way to holiday/vacation is:

    Results (18 votes). Check out past polls.