For Programmers: Free Programming Magazines  


Home > Archive > PERL CGI Beginners > March 2005 > Re: need logic and one error help









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Re: need logic and one error help
Steven Schubiger

2005-03-28, 3:55 am

On 28 Mar, T Raymond wrote:

> #!/usr/bin/perl -w


Refactor with use warnings instead of -w.
-w leaks warnings to all scopes, i.e. external modules,
whereas warnings keeps itself to the local block.
See perldoc warnings therefore.


> print header,


What is header supposed to act on?
If it's the method declared in CGI.pm, which I'd assume,
you need to create an object by saying my $q = new CGI and
replace header with $q->header.


> start_html(-title=>'Employee Search'),
> h1('Teresa Raymond's Employee Search');
>
> &search_form unless param;


& implies passing @_ to search_form(); unneeded and very
dangerous, in most cases.


> my $filename='employee.dat';


This is not C, so whitespaces between the variable, the assign
operator and the value is greatly recommended. See man perlstyle
therefore.


> my $fn_weight;
> my $ln_weight;
> my $weight;
> my $ot_hours;


One can argue, whether declaring those variables in several
or a single line may add up to readability. I prefer the grouped variant
in a single line, unless the variables have any familiarity at all.


> die "cannot locate $filename\n";


Replace with "die "$filename: $!\n";
$! contains the exception message emitted by many functions
on failure.


> for (@rows)


Are you sure, you want to have $_ evaluated instead of, say, my
$single_row? Aware of possible variable stomping?


> if (my @matches = $fname =~ m/^$first_name/ig)
> {


Conditions usually read CONDITION () {
}


> if ($employees{$_}{$fname}=~m/^$first_name/ig)


Have you checked the definedness of $first_name via defined()?


> sub sub_calc_ot_pay
> {
> my $hourly_wage = $_[0];
> my $ot_hours = $_[1];


my ($hourly_wage, $ot_hours) = @_;


--
The trouble with having an open mind, of course,
is that people will insist on coming along and trying to put things in it.
-- Terry Pratchett
Charles K. Clarkson

2005-03-28, 8:55 am

Steven Schubiger <mailto:steven@accognoscere.org> wrote:

: On 28 Mar, T Raymond wrote:
:
: : print header,
:
: What is header supposed to act on?
: If it's the method declared in CGI.pm, which I'd assume,
: you need to create an object by saying my $q = new CGI and
: replace header with $q->header.

The OP is using the function oriented version of CGI.pm.
This is homework and perhaps his class hasn't gotten to
objects yet.


[snip]
: : die "cannot locate $filename\n";
:
: Replace with "die "$filename: $!\n";
: $! contains the exception message emitted by many functions
: on failure.

Omit the "\n", it suppresses important information. I
prefer this idiom. The quotes around the filename have saved
me hours. :)

open FH, $file or die qq(Cannot open "$file": $!);


HTH,

Charles K. Clarkson
--
Mobile Homes Specialist
254 968-8328

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com