Perl
$cars{$make}{$model}++;
foreach my $make ( sort keys (%cars)) {
my $carcnt = "";
foreach my $model ( sort { $model{$a} <=> $model{$b} } keys %{ $cars{$make} } ) {
$carcnt = sprintf("%0.5d", $cars{$make}{$model});
print "$make $model = $carcnt\n";
}
}
========================================================================================
%hash = (
"element1" => "value1",
"element2" => "value2"
);
========================================================================================
open (FILE, "$file");
while (<FILE>) {
chomp ($_);
}
close (FILE);
========================================================================================
perl -MFile::Find -le 'find sub{push @m, $File::Find::name if/\.pm$/}, $_ for @I
NC; print for @m'
========================================================================================
for (my $argc = 0; $argc <= $#ARGV; $argc++) {
if ($ARGV[$argc]) {
$opt = $ARGV[$argc];
$opt =~ s/--//; # Get rid of 2 dashes
$opt =~ s/-//; # Get rid of 1 dash
$opt = substr($opt,0,1); # cut the first char
}
if ($opt eq 'c') {
$arg_cron = "true";
} elsif ($opt eq 'e') {
$arg_event = $ARGV[++$argc];
$check_thing = check_thingy($arg_event);
if ($check_thing eq "false") {
usage();
exit 1;
}
} elsif ($opt eq 'h') {
usage();
exit 0;
} elsif ($opt eq 'l') {
$arg_lp = $ARGV[++$argc];
$check_lp = check_lp($arg_lp);
if ($check_lp eq "false") {
error("Print $arg_lp not found.", 105);
}
} else {
usage();
exit 1;
}
}
========================================================================================
sub usage {
print "\nusage: $prog [-c] [-e entity] [-h] [-l printer_id]\n\n";
print "Options:\n";
print " -c flag indicating this report is being run as a cron job\n";
print " -e entity run report on a single specified entity\n";
print " -h usage information\n";
print " -l printer_id print to a specified printer id\n";
print "\n";
}
========================================================================================
use Sys::Hostname;
my $hostname = hostname();
========================================================================================
# CONSTANTS - DATE
use constant DAYS_OF_THE_WEEK => ["Sun", "Mon", "Tue", "Wed", "Thur", "Fri", "Sat"];
use constant MONTHS => ["Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul",
"Aug", "Sept", "Oct", "Nov", "Dec"];
my $dow = DAYS_OF_WEEK->[0]; # Sun
my $month = MONTHS->[3]; # Apr
========================================================================================
my_func("param");
sub my_func {
my $param = shift;
return $param;
}
========================================================================================
$body = "time: $cron_time" . "\n" . $cron_subject . "\n";
open(MM, qq(|/bin/mail -s "$subject" $receipient));
print MM $body;
close MM;
========================================================================================
open (MAILFILE,">msg.txt") or error("Could not write email!", 100);
print MAILFILE "To: " . $user . "\n";
print MAILFILE "Subject: testing mail\n";
print MAILFILE "It works!\n";
close (MAILFILE);
system ("/usr/sbin/sendmail -t < msg.txt");
========================================================================================
use Spreadsheet::WriteExcel;
my $workbook = Spreadsheet::WriteExcel->new("new_file.xls") or die "Could not create file $!";
my $worksheet = $workbook->addworksheet("sheetA");
my ($x, $y);
foreach my $fields ( sort keys (%records)) {
$x = 0;
my ($name, $title, $salary) = split(/_/, $fields,3);
$worksheet->write($y, $x++, $name);
$worksheet->write($y, $x++, $title);
$worksheet->write($y, $x++, $salary);
$y++;
}
$workbook->close();
Tech-Notes






