in reply to Service monitor with Perl and PerlSvc

package PerlSvc; use Win32::Service; use Win32::TieRegistry(Delimiter=>"/"); use DBI; use DBD::ODBC; use Win32::EventLog;

use strict; is missing.

# the short name by which your service will be known (cannot be 'my')

Please document *why* something is not possible, instead of just "can't do this".

${$_}{'CurrentState'}

I think $_->{'CurrentState'} is easier to read. (I'd even leave out the quotes.)

$dsn="driver=\{SQL Server\}\;Server=$srv\;UID\=$uid\;PWD\=$pwd\;";

It's clearer and much easier to read if you don't escape things that don't need escaping: $dsn = "driver={SQL Server};Server=$src;UID=$uid;PWD=$pwd;";

$exec=$dbh->prepare("..."); $exec->execute;

Do check the return values. In a module, it's better to turn PrintError off and handle errors yourself.

$exec=$dbh->prepare("... values ('$cn', '$sname', $state)");

Don't interpolate arbitrary values in a query. Use placeholders instead.

$exec->finish;}else{

Why does the else not have its own line?

} }else

No two closing curlies can be in the same column in proper indented code.

unless (${$_} eq "1"){

Are you sure you want a string equality test? Not a numeric one? Or maybe even a boolean test? (unless ($$_) { ... })

$dsn="driver=\{SQL Server\}\;Server=$srv\;UID\=$uid\;PWD\= +$pwd\;";

You're repeating code. It's better to put that string in a more global variable. I personally like having a sub 'db' that returns a dbh: sub db { DBI->connect("dsn here") }

} sleep(5); } }

Again: this should never happen.

Win32::Service::GetServices("", \%tmp); ... if (Win32::Service::GetStatus("",$_,\%{$_})){

You sometimes have a space after comma, you sometimes don't. Whatever style you choose, be consistent. (I like having a space after commas)

"$DisplayName"

Those quotes should not be necessary. Use plain $DisplayName instead.

print "No help available.\n";

Very helpful :)

In general: learn about indenting, use strict, learn about writing modules, get to know Perl a little better. Good luck and have fun.

Juerd
- http://juerd.nl/
- spamcollector_perlmonks@juerd.nl (do not use).