Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW

[Golfing] Uncommenting a block

by LanX (Bishop)
on Apr 14, 2010 at 11:59 UTC ( #834654=perlquestion: print w/replies, xml ) Need Help??
LanX has asked for the wisdom of the Perl Monks concerning the following question:


in a discussion how to efficiently hide a chunk of code (here org-mode syntax from emacs) in perlcode I came up with this "solution":

Using a here-doc to the next empty line in void context

codeline(); <<; * Headline ** [TODO] write test ** [DONE] documentation codeline(); # last line was empty

This seems to be much shorter than if(0){..} constructs (which disallow '}' in the chunk) or POD (which needs plenty of empty lines) and can be easily extended to span empty lines by adding an explicit Here-doc-signaler or a variable to catch the chunk at runtime.

Anyway I get warnings because non-quoted Here-docs are deprecated so I need to switch to <<'';

Any other suggestions how to uncomment chunks?

Cheers Rolf

Replies are listed 'Best First'.
Re: [Golfing] Uncommenting a block
by moritz (Cardinal) on Apr 14, 2010 at 12:33 UTC
    With vim it's easy to insert blocks of text, I usually comment out stuff with a # and then a blank.

    You can mark the block with Ctrl+v, then go to the first column, type I#<blank><escape> and be happy. Deleting is just as easy: again mark with Ctrl+v, and delete with d (or x, for that matter).

      Well it wasn't an editor question ...

      In this use case I had to comment out org-mode syntax which only works at the beginning of the line and unfortunately doesn't tolerate any leading "#" ...

      Cheers Rolf

      (1)comment-region in emacs is even easier, I bound a macro to a key which (un)comments the marked region or the current line, chunks, paragraphs or function depending on repetition! And if I miss a vi-feature I switch to viper-mode ...

        Well it wasn't an editor question ...
        Which can still be answered by "use the appropriate tool for the job", which in this case happens to be an editor.

        Abusing language features is an ugly hack, and going to hurt you in the long run. One drawback of using a heredoc is that you don't see if you're inside a comment without knowing the context.

        Not seeing the context happens quite often, for example when reviewing diffs/patches.

        Perl 6 - links to (nearly) everything that is Perl 6.
Re: [Golfing] Uncommenting a block
by Anonymous Monk on Apr 14, 2010 at 12:03 UTC
      Thanks, interesting, but my (shortest) approach is not discussed (or only in more verbose versions)

      From what I saw nesting and conditional commenting seem to be of special interest... which can be done with this solution:

      if ($habibi) { print $habibi; <<'' if (0); #forget the following print " talks often much cr*p";<P> $h++ }

      Warning about whitespaces in "empty" lines ending a heredoc seem to be a good application for code filters. (...finally)

      Cheers Rolf

        "empty" lines ending a heredoc have trailing spaces at the end, by definition. perlcritic catches that :).

         David Serrano
         (Please treat my english text just like Perl code, i.e. feel free to notify me of any syntax, grammar, style and/or spelling error. Thank you!).

Re: [Golfing] Uncommenting a block
by stefbv (Curate) on Apr 14, 2010 at 17:05 UTC

    So you want to embed TODO headlines in perl code, than use the global TODO list from agenda views to see what needs to be done further in your code, right? If yes, than it's an interesting approach, which certainly has it's advantages.

    For this task, I use a (classic) remember TODO template to create new headlines in specific org files. This template will automatically add a link to the line of the code in the buffer was called from.

    Cheers, Stefan

      > So you want to embed TODO headlines in perl code, than use the global TODO list from agenda views to see what needs to be done further in your code, right?

      yes kind of, many features of org-mode are very intuitive.

      For the records, the best solution I found so far is

      print; =org * TODO whatever ... ** =cut print;
      this is legal Perl code (but not legal POD because of missing blank lines and unknown directive)


      sub ORG ($) { } print; ORG <<''; * TODO whatever ... ** print;

      Cheers Rolf

      UPDATE: repaired code, haven't taught my Wiki-expander to igore code tags yet...

        How about?:

        print; =for org * TODO whatever ... ** =cut print;


        Update: added spaces before and after POD.

        Update2: s/spaces/empty lines/

Re: [Golfing] Uncommenting a block
by ikegami (Pope) on Apr 15, 2010 at 16:01 UTC
    I hate that your method ends with a blank line. Too inconspicuous.
    if(0){q{ ... }}
    0&&q{ ... }

    if(0){ } is great for temporary debugging code too. You can just toggle between 0 and 1.

      > Too inconspicuous.

      in the context of emacs it's ok , but yes other editors might be in danger...

      I could cite Moritz: "use the right tools!" ;-)

      Cheers Rolf


    • org-mode is an emacs-feature
    • emacs visualizes trailing whitespaces
    • cperl-mode would instantly highlight an falsely extended here-doc
    • fly-make would complain about broken syntax
        emacs visualizes trailing whitespaces
        Sorry, I hate this feature, displaying underscores for whitespaces is very confusing.

        And IMHO there is no unavoidable situation in Perl where it matters if any blanks were unwillingly inserted.


Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://834654]
Approved by marto
[Discipulus]: he eh latinos superstitious peoples..
[Discipulus]: here at my $work fuc**ing bas**ds proposed to everyone 20 free month of pay to leave
[Discipulus]: I expect a cut of 20% of emplyoment nexet year..

How do I use this? | Other CB clients
Other Users?
Others scrutinizing the Monastery: (10)
As of 2018-05-23 20:52 GMT
Find Nodes?
    Voting Booth?