Useful Perl Snippets to stuff in your toolbox

Posted by kbenton on 2007-02-14 17:20

Here are some code snippets using interpolation that you may find useful to put in your toolbox. While these are specific to Perl, it's easy to see how they could be adapted to other languages. Note that these snippets ask you to fill in the blanks as you code and help speed up development by taking care of some of the mundane tasks (like adding comments at the end of a conditional block).

-------------------------------------------------------------------

Perl elsif:

elsif ([[%ask1:Condition:1]]) {
    # Do this when true
} ## END OF elsif ([[%ask1]])

-------------------------------------------------------------------

Perl foreach

foreach [[%ask1:IndexVariable:$i]] ([[%ask2:ArrayVariable:@list]]) {
    # Do something with [[%ask1]]
} # END OF foreach [[%ask1]] ([[%ask2]])

-------------------------------------------------------------------

Perl if/else

if ([[%ask1:Condition:1]]) {
    # True
}
else { # else from if ([[%ask1]])
    # Otherwise
} # END OF if ([[%ask1]])

-------------------------------------------------------------------

Perl sub (no POD)

##############################################################################
# [[%ask:Describe this subroutine:What does it do?]]
sub [[%ask1:Subroutine Definition:my_sub()]] {
    # Do something here
} ## END OF [[%ask1]]

-------------------------------------------------------------------

Perl sub (with POD)

##############################################################################
=item C<[[%ask1:Subroutine Definition:my_sub()]]>

[[%ask:Describe this subroutine:What does it do?]]

=cut

sub [[%ask1]] {
    # Do something here
} ## END OF [[%ask1]]

-------------------------------------------------------------------

Perl while

# What is ths while about?
while ([[%ask1:Condition]]) {
    # Code goes here
} ## END OF while ([[%ask1]])

-------------------------------------------------------------------

If you can think of others you think would be useful, please feel free to contribute them here. These snippets of code, created by myself, Kevin Benton are entered into the public domain.

kbenton | Mon, 2007-02-19 10:41

Some more snippets, specifically POD related:

Perl POD Sample Block
=head1 NAME
X<STUFF> X<some thing under fun facts>

STUFF - some thing under fun facts

=head1 DESCRIPTION

blah blah blah

=head2 Subsection

=over

=item fact

Fun fact

=back

=cut

-----------------------------------------

POD Formatting Codes

POD Italics: I&lt;&lt;&lt;[[%ask:Italic Text]]&gt;&gt;&gt;
POD Bold: B&lt;&lt;&lt;[[%ask:Bold Text]]&gt;&gt;&gt;
POD Code: C&lt;&lt;&lt;[[%ask:Code]]&gt;&gt;&gt;
POD Hyperlink: L&lt;&lt;&lt;[[%ask:Link Location]]&gt;&gt;&gt;
POD Escape: E&lt;&lt;&lt;[[%ask:Escape Code]]&gt;&gt;&gt;
POD Filename: F&lt;&lt;&lt;[[%ask:File Name]]&gt;&gt;&gt;
POD OneLiner: S&lt;&lt;&lt;[[%ask:Text that should not be broken across lines]]&gt;&gt;&gt;
POD IndexEntry: X&lt;&lt;&lt;[[%ask:Index Entry]]&gt;&gt;&gt;
POD ZeroEffect: Z&lt;&lt;&lt;&gt;&gt;&gt;
Stop interpreting PERL: __END__

---
Kevin Benton

zaven | Mon, 2007-03-19 13:10

Good stuff. I also find it useful to bind a quick key to "=pod\n=head2\n" and another to "=cut" to encourage myself to use more POD :-)

betoian | Tue, 2007-05-15 17:20

Hi,

This code could also work as a one-liner. It opens the filename specified when calling the script. Reads all its content in the $cnt scalar and then prints it as if it were a cat instruction. By uncommenting some lines it could even list each line with a linenumber.

It makes reading files as easy as typing cat.

perl.oslurp
my $cnt;   ## THE COMPLETE DEMANDED FILE CONTENT

{   # S.L.U.R.P. input file (STDIN)

    local $/;
    push(@ARGV,shift);
    $cnt=<>
}

## UNCOMMENT THE FOLLOWING LINES TO LIST IT WITH LINENUMBERS...

#my $nr=1; $cnt= join("\n",(
#map{
#     sprintf("%03i_ %-60s",$nr++,$_)## LNUM_ LINE...
#    }( split(/[\r\n]/, $cnt) )   ## SPLIT BY LF
#)
#  );

print($cnt,$/);   ## CAT: PRINT THE FILE CONTENT

People is born original
and dies as a copy
Alberto