Over a million developers have joined DZone.

New - Skeleton Code For New Perl Program

·
Template for a new Perl program - outputs commonly used skeleton code to save typing it again.

Usage:
new [-h] [-d] [-f] [-n]
where:
 -h - skip header (hash bang, warnings & strictness pragmas)
 -d - code for reading a directory (from perldoc -f readdir)
 -f - code for reading a file.
 -n - Perl 5.6 or newer lexical filehandles (defaults to old Perl TYPEGLOB)

This saves you retyping it, making a typo, and makes sure you've not forgotten anything.


#!/usr/bin/perl -s
#
# new - start a new perl program
#

my $indent = 0;
if ( ! $h ) {

    if ( $n ) {
print <)) {

    #

}
close $file_fh;

EOT
    }
    else {
$file = sprintf <<'EOT';

open (FILE, $file) || die "Can't read '$file': $!\n";
while (defined (my $line = )) {

    #

}
close FILE;

EOT
    }

    if ( $indent ) {

        my $ind = " " x $indent;
        $file =~ s/\n(.)/\n$ind$1/g;

        print 'for my $file ( @files ) {', "\n$file}\n";

    }
    else {

        print q~my $file = 'file.txt';~, $/;

        print $file;

    }

}

Topics:

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}