CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
Search Posts SEARCH
Who's Online WHO'S
Log in LOG

Home: Perl Programming Help: Beginner: Re: [sdlan75] How to read only the first line: Edit Log

Veteran / Moderator

May 25, 2013, 8:44 AM

Views: 3622
Re: [sdlan75] How to read only the first line

Here's a skeleton of the script I'd work with.


use 5.10.1;
use strict;
use warnings;

@ARGV == 2 or die "wrong number of args\n";
my ($option, $file) = @ARGV;

my %actions = (
'-n' => \&print_name,
'-s' => \&print_salary,
'-b' => \&print_bonus,
usage => \&print_usage,

my $action = $actions{$option} || $actions{usage};

sub parse_file {
# code that parses the file and returns the data in a hash
# hash keys would be 'name', 'salary', and 'bonus'.

sub print_name {
my %employees = parse_file($_[0]);


sub print_salary {
my %employees = parse_file($_[0]);


sub print_bonus {
my %employees = parse_file($_[0]);


sub print_usage {
print "Invalid arg was passed\n",
"Usage: $0 [-n -b -s] filename\n";

The parsing of the file could be done outside of the "print" subs to eliminate the duplication of that statement in each sub.

(This post was edited by FishMonger on May 25, 2013, 8:45 AM)

Edit Log:
Post edited by FishMonger (Veteran) on May 25, 2013, 8:45 AM

Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives