raj8 has asked for the wisdom of the Perl Monks concerning the following question:

After attempting to look for a certain entry in a file and then looking for a process, I have been trying to verify if they are both true and if not exit with the appropriate value. However that is not the case. This is what I have so far. Any hints would help. Thnks..
while(<BPCONF>) { (my $server) = unpack '@10 A10', $_; if (( $_ = "SERVER = cluster") && (${ps} != "deamon")) { print "Server entry is correct\n"; exit 110; } else { print "Server entry is incorrect\n"; exit 100; } };

Replies are listed 'Best First'.
Re: Grabbing Process Status..
by tadman (Prior) on May 09, 2002 at 01:03 UTC
    It looks like Perl, but it's heavily accented. Consider:
    while (<BPCONF>) { my ($server) = unpack ("@10 A10", $_); if (($server eq "SERVER = cluster") && ($ps ne "daemon")) { print "Server entry is correct\n"; exit (110); } else { print "Server entry is incorrect\n"; exit (100); } }
    Notes on the changes:
    • ${ps} is the same as $ps. The braces are used when you are trying to specify where the variable stops and other data starts. "$psfoo" references $psfoo, not $ps like "${ps}foo" would.
    • That is some truly bizarre indentation.
    • A single equals makes an assignment, not a comparison. A standard comparision is eq which tests for string equivalence. == tests for numerical equivalence.
    • The while{} block does not require to be terminated with a semicolon. You can put one there if you like, but it is functionally useless.
    • You are comparing against $_ and not your extracted $server variable.
    • $ps must be defined somewhere else in your code.
    • Instead of using unpack you might want to use a regular expression, or perhaps substr.
    Other than that, make sure you use strict and -w.

    Here's a few ways to get your $server value, all pretty much equivalent:
    my $server = substr($_, 10, 10); my ($server) = /^.{10}(.{10})/;
    Using a regular expression is generally better, especially if you're not sure precisely where the data is going to be, index wise. If you were looking for "SERVER", your code might resemble this:
    while (<BPCONF>) { if (/SERVER = cluster/ && $ps eq "daemon") { print "Server entry is correct\n"; exit (110); } else { print "Server entry is incorrect\n"; exit (100); } }
      Thanks very much for your help and thanks for the share of knowledge on the server entry. The first line in the file that I was opening was SERVER = cluster. Therefore, I was trying to get the cluster part even they goofed and put additional spaces before or after the = sign. Once again, thanks for the help.
        If you have no idea how many spaces are going to appear there, if any, maybe you could use this instead:
        if ((/SERVER\s*=\s*cluster/) ...
        This will test for zero or more (*) spaces (\s) between those three parts. They can put in 500,000 and it won't affect your program.
Re: Grabbing Process Status..
by mattriff (Chaplain) on May 08, 2002 at 23:55 UTC
    Could use some more information in order to help solve the problem, but two points I see offhand:

    • You probably mean "daemon" instead of "deamon"
    • When testing (non) equality of strings, you should use ne instead of != (which is for numerical comparison)

    - Matt Riffle

      so the comparison should work if I use eq and ne on the test?