Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

comment sections

by powerhouse (Friar)
on Apr 15, 2003 at 09:05 UTC ( [id://250508]=perlquestion: print w/replies, xml ) Need Help??

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

Is there a way to have perl ignore a big section without having to put comments in front of every row? something like this:
#!/usr/bin/perl -w use strict; # Start comments Blah blah still in comments bunch of stuff, # End comments.... $more_code_and_stuff;
Something like that? I have a script that has a big chunk of data that I want to remove, but I don't want to take it OUT of the file, so I just want to put comments around it, but it's like 65 lines, and that is a lot of #'s to put in there ;o)

thx,
Richard

Replies are listed 'Best First'.
Re: comment sections
by broquaint (Abbot) on Apr 15, 2003 at 09:11 UTC
    s there a way to have perl ignore a big section without having to put comments in front of every row?
    Stricly speaking, no, as # is the only of creating comments (natively) in perl. However you can use POD to comment out large sections like so
    use strict; =pod ... =cut exit 0;
    However that won't work if you've got POD in the code inbetween. Another option is to use heredocs
    use strict; <<'IGNORETHIS'; ... IGNORETHIS exit 0;
    And finally there's always a CPAN module to do the job - Acme::Comment.
    HTH

    _________
    broquaint

      If you are going to use the here doc way, I suggest putting single quotes around the delimiter; otherwise, Perl will interpolate anything starting with a $ or a @, which might cause warnings, or even compile errors if you use strict.

      Abigail

      However you can use POD to comment out large sections like so ....

      If I'm going to use pod for this (my editor handles it nicely so I don't usually) I prefer to use

      =begin block_comment ... =end block_comment =cut

      or for smaller chunks of code without blank lines in them a simple

      =for consideration_later ..... =cut

      Doing it this way has the advantage that the pod'ed out code doesnt get treated by the various pod parsers as being part of the actual documentation.


      ---
      demerphq

      <Elian> And I do take a kind of perverse pleasure in having an OO assembly language...
Re: comment sections
by Abigail-II (Bishop) on Apr 15, 2003 at 09:35 UTC
    Get yourself a good editor! Here's how you would comment out the current line, and the 65 lines following it in vi:
    :.,+65s/^/#/

    13 characters, including the trailing return.

    Abigail

      In vim, you can also use visual selection mode to specify the area to be affected before you issue an ex command. Together with the { and } keys for paragraph-oriented navigation I find this very handy. You could go one better and keymap the respective :s sequences, though I haven't bothered to.

      Makeshifts last the longest.

Re: comment sections
by davis (Vicar) on Apr 15, 2003 at 09:27 UTC
    Just for silliness, I just had to post a link to Acme::Don't, which (IIRC) does the equivalent of wrapping a if(0) { ... } around your code. This implies that your code that you want to skip is compilable.

    davis
    It's not easy to juggle a pregnant wife and a troubled child, but somehow I managed to fit in eight hours of TV a day.
Re: comment sections
by nite_man (Deacon) on Apr 15, 2003 at 09:29 UTC
    You can use POD command for it:
    #!/usr/bin/perl -w use strict; =head1 Name =head1 Description =cut ... your code =head2 Methods =item method_1() ..... description =cut sub method_1 { ... } =item method_2 { =cut sub method_2 { ... } =back =head1 AUTHOR =cut
    In this case, you will have both comments and documentation of your script or module.
    Or if you use vim you can use its command for putting '#' in the lines which you define, but it's another story :-)
    Cheers
          
    --------------------------------
    SV* sv_bless(SV* sv, HV* stash);
    
Re: comment sections
by Wookie (Beadle) on Apr 15, 2003 at 11:42 UTC
    You could simply:
    #!/usr/bin/perl -w use strict; # Every job should # Start of comments if (defined undef) { foo... } # End of comments &do_more_stuff();
    In terms of efficency - using a constant should kill a large block like that at compile time as opposed to run time (although the optimiser might take away the code in the solution above?).
    #!/usr/bin/perl -w use strict; use constant COMMENT_WRAPPER => 0; # Start comments if (COMMENT_WRAPPER) { Blah Blah } # End comments &carry_on_with_stuff();

    game(Wookie,opponent) eq 'Wookie' ? undef $problem : remove_limbs(arms,opponent);
      But that requires that the stuff you outcomment is actually compilable. And it won't help to outcomment a BEGIN block.

      Abigail

        I vote for using #s as I've stated elsewhere under the heading no pod & code.

        Personally I find the desire or need to temporarily comment out fair sized blocks of code is a warning of impending danger or difficulty. And usually these style of comments are meant to be temporary. Often the trouble is so imminent that it has already arrived.

        Requiring commented code to be compilable seems like a feature to me. But I know what you mean.

        Quick update: The following is not what Abigail said. Abigail pointed out that you can't comment the entire BEGIN block this way. You can't. You can't comment out any other subroutine definition either. Or forward sub declaration.

        And you can comment out code in a begin block. Viz:

        #!/usr/bin/perl use strict; use warnings; BEGIN { use constant COMMENT => 0; } BEGIN { if ( COMMENT ){ print "silence\n"; } print "noise\n"; }
Re: comment sections
by h_golan (Initiate) on Apr 15, 2003 at 15:40 UTC
    =pod what ever u like as long as u want =cut

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others goofing around in the Monastery: (6)
As of 2025-04-30 19:41 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?
    erzuuliAnonymous Monks are no longer allowed to use Super Search, due to an excessive use of this resource by robots.