Skip to content

Instantly share code, notes, and snippets.

@ericychoi
Last active February 19, 2017 05:10
Show Gist options
  • Select an option

  • Save ericychoi/b84d0b5bce54a5bcb8d4541b8a886d28 to your computer and use it in GitHub Desktop.

Select an option

Save ericychoi/b84d0b5bce54a5bcb8d4541b8a886d28 to your computer and use it in GitHub Desktop.
Perl bin script template
#!/usr/bin/env perl
use strict;
use warnings;
use Getopt::Long;
use Term::ANSIColor;
# usage:
#
my $SCRIPT = 'foo.pl'
my ($dryrun, $verbose, $foo);
GetOptions(
'dryrun!' => \$dryrun,
'verbose!' => \$verbose,
) or die "usage: $SCRIPT (--dryrun)\n";
my @commands = @ARGV;
if ($dryrun) {
print "dryrun mode:\n";
}
my $cmd .= "ls -al";
$cmd .= " $foo " if $foo;
do_cmd($cmd);
exit 0 if !$more_cmds;
sub do_cmd {
my ($cmd) = @_;
if ($dryrun) {
print "$cmd\n";
return;
}
open CMD, '-|', $cmd or die $@;
my $line;
while (defined($line=<CMD>)) {
# print colored("hello world\n", 'green');
print $line;
}
close CMD;
die "command: '$cmd' returned non-zero status\n" if $?;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment