#! /usr/bin/perl -wl
# mytime3+ script
# Tim Maher, [email protected], www.MinimalPerl.com
# Improvement to mytime3, from p. 386 of Minimal Perl book.
(undef, $minute, $hour)=localtime ; # leave seconds undefined
#=====> Convert military time to "civilian" time <=====#
$am_pm='AM';
$hour >= 12 and $am_pm='PM'; # hours 12-23 are afternoon
$hour > 12 and $hour=$hour-12; # 13-23 ==> 1-11 (PM)
$hour == 0 and $hour=12; # convert day's first hour
$minute < 10 and $minute="0$minute"; # convert "5" to "05", etc.
print center_line("The time is $hour:$minute $am_pm.");
sub center_line {
# returns argument centered within field of size $cl_width
use Text::Tabs; # imports expand(); converts tabs to spaces
if ( @_ != 1 or $_[0] eq "" ) { # needs one argument
warn "$0: Usage: center_line(string)\n";
$newstring=undef; # to return "undefined" value
}
else { # $cl_width can be defined for custom field width
defined $cl_width and $cl_width > 2 or $cl_width=80;
$string=shift; # get sub's argument
$string=expand $string; # convert tabs to spaces
$string =~ s/^\s+//; # remove leading whitespace
$string =~ s/\s+$//; # remove trailing whitespace
# calculate indentation
$indent=($cl_width - length $string )/2;
$padding=' ' x $indent;
$newstring="$padding$string";
}
return $newstring; # return centered string, or undef
}