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).


In reply to Re: Service monitor with Perl and PerlSvc by Juerd
in thread Service monitor with Perl and PerlSvc by perlsage

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.