Operating System - HP-UX
1827769 Members
2760 Online
109969 Solutions
New Discussion

Re: PERL Problem update var and file

 
SOLVED
Go to solution
Ricardo Bassoi
Regular Advisor

PERL Problem update var and file


Hi All,

I have a problem with the script in the attachment.
The var $var_caps apears to be like this $var_caps=' 23.4'
and when the script compare at the if cmd with the $MAX_CAPS it didn??t update this var.

Can anybody helps ?

Regards,

Bassoi
If you never try, never will work
2 REPLIES 2
Rodney Hills
Honored Contributor

Re: PERL Problem update var and file

Try changing the "gt" to ">". "gt" is for string compare, ">" for numeric.

-- Rod Hills
There be dragons...
Ralph Grothe
Honored Contributor
Solution

Re: PERL Problem update var and file

Hi Ricardo,

I hope you don't mind if I suggest some "improvements" to your script.

But first, regarding your question.

Perl doesn't distinguish data types as compiled languages do.
(well, that's partly true, Perl distinguishes between scalars such as ordinary variables and references, lists such as arrays and hashes, and type globs)
Whether something is a string or a numeric depends only on the context.
Thus I wouldn't enforce a string context, as you did by quoting but simply leave the assigned value to $var_caps and $ MAX_CAPS untouched
(you correctly chomped $var_caps already)
So your condition should, as already mentioned, indstaed read:

if ($var_caps < $MAX_CAPS) {
# do something
}

A word of caution to your openings of files.
Always test the success of an open() statement!
The Perl idiom to do this is simple:

open HANDLE, "<$file" or die "open of $file failed: $!\n";

Always switch on warnings!

pre Perl 5.6 in the shebang:

#!/usr/bin/perl -w

from Perl 5.6 onwards use the warnings Pragma:

use warnings;

Always use the strict pragma, even if the compiler may reject your code at first:

use strict;

Then, I wouldn't use shell escapes such excessively as you did by the backtick operation.
Perl has *far more* parsing power than the cuts you have invoked through the shell can offer.
Also use as many Perl built-in functions (and later modules) as possible.
Perl has a localtime() function, so you can avoid the date invocation.

Also have a look at the POD

perldoc perl
perldoc perlsyn
perldoc perlfunc
perldoc perlop
perldoc perlopentut
perldoc perlstyle
Madness, thy name is system administration