in reply to Newbie question under time constraint

Here you go. My Paypal for the twenty. Please note that a public offer of a reward is legally binding.
package Employee; use strict; use warnings; sub new{ my ($class, $args) = @_; my $self = bless { name=> $args->{name}, hourlywage => $args->{hourlywage}, hours => $args->{hours} }, $class; } sub pay { my $self = shift; return $self->{hours} * $self->{hourlywage}; } sub gist { my $self = shift; return "Employee info: name: $self->{name} hours: $self->{hours} w +age: $self->{hourlywage}\n" } 1; package EmployeeList; use diagnostics; use strict; use warnings; sub new{ my ($class) = @_; my $self = bless { EmployeeList => [] }, $class; return $self; } sub add_Employee { my $self = shift; my $Employee = shift; push @{ $self->{EmployeeList} }, $Employee; } sub gist{ my $self = shift; my @gist = map { $_->gist } @{ $self->{EmployeeList} }; return join "", @gist; } sub average_hourly_wage{#not even sure where to begin, don't know how +to initialize or access the hash at this point my $self = shift; my $wages; my $count; foreach my $employee ( @{ $self->{EmployeeList} }) { $wages += $employee->{hourlywage}; $count++; } return $wages / $count; } sub find { my $self = shift; my $name = shift; foreach my $Employee ( @{ $self->{EmployeeList} } ) { return $Employee if $Employee->{name} eq $name; } } 1; #use Employee; #use EmployeeList; print "Enter the name of the file to open: "; my $input_file = <STDIN>; chomp $input_file; open(my $file_handle, "<", $input_file) or die "failed to open < input +_file: $!\n"; my $EmployeeList = new EmployeeList(); while (my $row = <$file_handle>){ my @splitrow = split ' ', $row; my $Employee1 = Employee->new({ name =>$splitrow[0], hourlywage =>$splitrow[1], hours =>$splitrow[2] }); $EmployeeList->add_Employee( $Employee1 ); } print $EmployeeList->gist(); print $EmployeeList->average_hourly_wage(), "\n"; print $EmployeeList->find("holli")->pay, "\n";


holli

You can lead your users to water, but alas, you cannot drown them.

Replies are listed 'Best First'.
Re^2: Newbie question under time constraint
by choroba (Cardinal) on Aug 04, 2019 at 22:01 UTC
    Great! Here's the same rewritten into Moose.
    #!/usr/bin/perl use warnings; use strict; use feature qw{ say }; { package Employee; use Moose; use Function::Parameters; use namespace::clean; has name => (is => 'ro', isa => 'Str'); has hourly_wage => (is => 'ro', isa => 'Int'); has hours => (is => 'ro', isa => 'Int'); method pay () { $self->hours * $self->hourly_wage } method gist () { "Employee info: name: " . $self->name . " hours: " . $self->hours . " wage: " . $self->hourly_wage . "\n" } __PACKAGE__->meta->make_immutable } { package EmployeeList; use Moose; use Function::Parameters; use List::Util qw{ sum0 }; use namespace::clean; has employee_list => (is => 'ro', isa => 'ArrayRef[Employee]', traits => ['Array'], handles => { add_employee => 'push', list_employees => 'elements', }, ); method gist () { join "", map $_->gist, $self->list_employees } method average_hourly_wage () { sum0(map $_->hourly_wage, $self->list_employees) / $self->list_employees } method find ($name) { (grep $_->name eq $name, $self->list_employees)[0] } __PACKAGE__->meta->make_immutable } print 'Enter the name of the file to open: '; chomp( my $input_file = <STDIN> ); open my $file_handle, '<', $input_file or die "Failed to open $input_file: $!\n"; my $employee_list= 'EmployeeList'->new; while (my $row = <$file_handle>) { my @splitrow = split ' ', $row; my $employee = 'Employee'->new( name => $splitrow[0], hourly_wage => $splitrow[1], hours => $splitrow[2] ); $employee_list->add_employee($employee); } print $employee_list->gist; say $employee_list->average_hourly_wage; say $employee_list->find('holli')->pay;
    map{substr$_->[0],$_->[1]||0,1}[\*||{},3],[[]],[ref qr-1,-,-1],[{}],[sub{}^*ARGV,3]
Re^2: Newbie question under time constraint
by Your Mother (Archbishop) on Aug 04, 2019 at 22:38 UTC
    My Paypal for the twenty. Please note that a public offer of a reward is legally binding.

    Yeah, maybe in small claims court. And taking money to do someone’s homework if this is what it smells like is… Well, no penalties for you but the cat could maybe lose a course credit or even be expelled depending on school rules; paying you means his legal name is on record so… Enjoy your $20!

A reply falls below the community's threshold of quality. You may see it by logging in.
A reply falls below the community's threshold of quality. You may see it by logging in.