Monday, January 3, 2011

perl: make multiline comments


1. Using Standard POD Blocks (=pod ... =cut)

This is the most common and robust way to write true multiline comments. Everything between =pod (or another pod directive) and =cut is completely ignored by the Perl interpreter.

Perl
print "Hello, World!\n";

=pod
This is a standard POD block comment.
You can write as many lines as you want here.
The Perl interpreter will ignore all of this.
=cut

print "Back to code execution.\n";

2. Using =head1 or Other POD Directives

Any POD directive can be used to start a block comment, provided it is closed with =cut. This is often used for descriptive section headings in scripts.

Perl
=head1 Developer Notes

Author: Purandaran
Date: 2026
Purpose: Utility script for processing records.

=cut

my $status = "Active";
print "Status: $status\n";

3. The Empty Subroutine Hack (sub { ... })

An anonymous block or an uncalled subroutine can act as a pseudo-comment block, though it is parsed by the compiler (so syntax errors inside will still be caught, unlike POD).

Perl
sub {
    # This is code that never gets executed,
    # acting effectively like a multiline comment.
    my $debug_mode = 1;
    print "Debugging...\n";
};

print "Running normal program.\n";

4. The Loop / False Condition Trick (if (0) { ... })

Wrapping code in a conditional block that never evaluates to true is commonly used to temporarily disable (comment out) large blocks of code.
Perl
print "Step 1\n";

if (0) {
    # Everything inside this block is ignored at runtime
    print "This won't print.\n";
    my $temp_var = 100;
}

print "Step 2\n";

No comments: