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

Hi,

Am interested to know if there are shortcuts in Perl, immaterial of the editor or IDE being used, to insert comments in a Perl script, while the script is being edited i.e. using input redirection characters

TIA, Ram

Replies are listed 'Best First'.
Re: Shortcut for inserting comments
by graff (Chancellor) on Jul 03, 2007 at 13:05 UTC
    When you say "input redirection characters", are you talking about the "HEREDOC" syntax? e.g.:
    #!/usr/bin/perl use strict; use warnings; my $first_try; my $commented_out = <<ENDCOMMENT; if( $first_try ) { print "this part of my code is experimental\n"; my $message = "it's not ready to be treated as executable yet"; # do stuff with $message... } ENDCOMMENT print "@ARGV\n";
    That won't work because any "my" declarations inside the HEREDOC will not be recognized as such, and the script will not compile with use strict.

    Another way is to use an always-false "if":

    if (0) { # lots of code here that I don't want to use just now... }
    This way, the code needs to be well-formed in order for the script to compile (bracketing and quotes must be balanced, lexical-scope variables must be declared), but it's fairly effective if your code is already pretty well organized.

    A third way is to use POD directives:

    # this part is currently runnable: my $foo = "bar\n"; =head2 Commented out my $baz = "blah"; # lots more code that I don't want to use right now... =cut # this part is also currently runnable... print $foo;
    In this case, the stuff within the POD-commented region doesn't need to be syntactically well-formed -- anything goes. Here, the only issue to worry about is if you happen declare subroutines or lexical variables within the commented region, and then try to use them outside that region -- that's a problem for all approaches, but it's the only problem in the POD approach. Again, if your code is already well-organized, this shouldn't be too much of a problem.

    That said, I think most good programming editors have a "comment-region" type of command or macro (as well as "uncomment-region") that knows how to put (remove) "#" at the beginning of every line in a selected region of text.

    Another trick -- if you know the line numbers, or if the start and end line content are sufficiently unique, you can stream-edit the script with perl:

    perl -i.bak -pe '$_="# $_" if($.>=10 and $.<=20)' myscript.pl
    (comment lines 10-20 inclusive), or:
    perl -i.bak -pe '$_="# $_" if(/^\$foo =/../^\$bar =/)' myscript.pl
    (comment lines starting with "$foo = ..." and ending with "$bar = ...", inclusive).
      perl -i.bak -pe '$_="# $_" if($.>=10 and $.<=20)' myscript.pl

      You can omit mention of $. and the comparators if you want.

      perl -i.bak -pe '$_="# $_" if 10 .. 20;' myscript.pl

      Cheers,

      JohnGG

      A third way is to use POD directives:

      Unfortunately, if you're actually using POD to document your scripts, that might not work so well. You can (mostly) get around this issue by using =begin comment / =end comment:

      # this part is currently runnable: my $foo = "bar\n"; =begin comment my $baz = "blah"; # lots more code that I don't want to use right now... =end comment =cut

      The one thing to beware of when doing this is if you're trying to comment out other POD -- a =cut in the middle of it will screw you up, and I'm guessing there might be other things that could as well

Re: Shortcut for inserting comments
by clinton (Priest) on Jul 03, 2007 at 12:34 UTC
    yes - you use the '#' key. Anything after # will be a comment.

    BEWARE

    • If you have an open quote (perlop) the # key will not work
    • When you press the ENTER key, the # key stops accepting comments

    Clint

Re: Shortcut for inserting comments
by naikonta (Curate) on Jul 03, 2007 at 15:14 UTC
    In vim:
    • Press ESC
    • Then V (shift-v)
    • Move cursor up or down to select wanted lines
    • Then press : and type s/^/# /
    • And finally, press Enter
    If you create a visual mapping like vmap # :s/^/# /<CR>, you have an instant shortcut to comment highlighted lines out. Now, highlight wanted lines, press the # character (shift-3), and voila, those lines are commented out. I'm sure there's a better way.

    You mentioned about IDE, right? Well, vim is my Perl IDE.


    Open source softwares? Share and enjoy. Make profit from them if you can. Yet, share and enjoy!

Re: Shortcut for inserting comments
by swampyankee (Parson) on Jul 03, 2007 at 14:52 UTC

    How much shorter could a short cut be than pressing the # key? If you mean a short cut for inserting pre-defined comments, possibly with some variable substitution, you could use something like (X)Emacs Skeletons and Auto-Insert, cpp, or Devel::PreProcessor. Except for such basics as copyrights, rcs or sccs ID keywords, I would think that a short cut for inserting comments would be rather useless, as there has to be some thought as to their contents.

    emc

    Any New York City or Connecticut area jobs? I'm currently unemployed.

    There are some enterprises in which a careful disorderliness is the true method.

    —Herman Melville