Perl Madness

So I get paid overtime and it’s a pain to work out how much I’m owed each week at 1.5x and 2.0x so I wrote a Perl script to do it.

Screenshot_2017-08-25_12-42-51.png

Can’t figure out what is going on with the warning about line 95. Here is the source code:


#!/usr/bin/perl
=for comment
Copyright <2017> <Wilyarti Howard>

Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met:

1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer.

2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution.

3. Neither the name of the copyright holder nor the names of its contributors may be used to endorse or promote products derived from this software without specific prior written permission.

THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.

=cut

use warnings;
use strict;
use Scalar::Util qw(looks_like_number);

my @work_days = qw / Monday Tuesday Wednesday Thursday Friday /;
our $start;
our $finish;
our $leave = 0;
our $total = 0;
our $dbltime;
our $timehalf;
our $std_day = 7.5;
our $hrs = 0;
our $currentday;
our $daylen;

foreach (@work_days) {
	while (!valid($_)) {
		#print "$start $finish looping \n";
	}
	$currentday = $_;
	if (($start eq 0) && ($finish eq 0)) {
		$hrs = 0;
		calchrs();
	} else {
		$hrs = $finish - $start -1;
		calchrs();
	}
	#print "Total hrs: $total hrs: $hrs Sick Leave: $leave \n";
}
sub valid {
	my $line;
	my $line2;
	print "$_ start time: ";
	while (chomp($line = <STDIN>), !looks_like_number($line)) {
		print "\n";
		print "Please enter a number: ";
	}
	$start = $line;
	print "$_ finish time: ";
	while (chomp($line2 = <STDIN>), !looks_like_number($line2)) {
		print "\n";
		print "Please enter a number: ";
	}
	$finish = $line2;
	if (($start >= $finish) || ($start <= 0) || ($finish > 24)) {
		print "Error. Invalid inputs.\n";
		return 0;
	} else {

		return 1;
	}

}
sub  calchrs {
	if ($currentday eq "Friday") {
		$daylen = 8;
	} else {
		$daylen = $std_day;
	}
	if ($hrs > ($daylen + 3)) {
		$total += $daylen;
		$timehalf += 3;
		$dbltime += ($hrs - ($daylen +3));
	} elsif ($hrs > $daylen) {
		$total += $daylen;
		$timehalf += $hrs - $daylen;
	} elsif ($hrs == $daylen) {
		$total += $hrs;
	} elsif ($hrs < $daylen) {
		our $line3 = 0;
		$hrs += 1;
		print "Enter lunch break: ";
		while (chomp($line3 = <STDIN>), !looks_like_number($line3), ($line3 gt $hrs)) {
			print "\n";
			print "error enter valid number: ";
		}
		$hrs -= $line3;
		$leave += $daylen - $hrs;
		$total += $hrs;
	}
}

print " Total hrs: $total\n Total 1.5x: $timehalf\n Total 2.0x: $dbltime\n Leave: $leave\n";

BTW Perl is a real pleasure to program in. The biggest hang up was cause by Perl comparing numbers to
strings. I finally found a function called looks_like_number which helped solved the issue of accepting
letters and symbols as inputs.

Did I mention I love Perl?

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s