Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask
 
PerlMonks  

Win32::Daemon::State returns a 0 or 1

by azaragoza (Acolyte)
on Jan 05, 2009 at 19:18 UTC ( #734254=perlquestion: print w/replies, xml ) Need Help??

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

I'm trying to run this piece of code, but it failes to return the state as in the code, instead it returns a 0 (zero) or 1 (one). Please advice.
Win32::Daemon::StartService () or do { my $err = "Failed to start service\n" . Win32::FormatMessage (Win32::Daemon::GetLastError ());
while ( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) ) { print "Daemon Service State: $State\n"; if ( SERVICE_START_PENDING == $State ) { print "Daemon Service State is starting = $State\n"; }
When I run the code instead of returning something like, SERVICE_START_PENDING, it returns a zero or a one. Thanks for your help.

Replies are listed 'Best First'.
Re: Win32::Daemon::State returns a 0 or 1
by ww (Archbishop) on Jan 05, 2009 at 21:17 UTC
    And might that indicate "success" or "failure?"

    Have you run a compilation check?

    Have you read Perl script as windows service?

    Have you read the docs carefully? or the module's homepage? For example:

    error_control
    An integer specifying how the Service Control Manager is to react if the service fails to start. The default is SERVICE_ERROR_IGNORE, which in fact gets you an error log entry.
      The job runs fine no copile errors, it's just not returning the constant strings the documentation claims it should return. Ex: SERVICE_STOPPED, SERVICE_START_PENDING, SERVICE_PAUSE_PENDING, SERVICE_CONTINUE_PENDING, SERVICE_STOP_PENDING, and SERVICE_RUNNING. This strings are compared to the status returned to perform certain functions. I did not write the code I picked it up from the web and I'm trying to make it work for me. Thanks for your help.
Re: Win32::Daemon::State returns a 0 or 1
by zentara (Archbishop) on Jan 05, 2009 at 21:23 UTC
    I don't use Win32, but it seems pretty obvious that 1 means its running, and 0 means its not. Probably some list of constants are not being imported that define things like "SERVICE_START_PENDING". Maybe read the module docs, and there may be some addition to the use statement, like
    use Win32::Daemon::State qw(constants); # a wild guess, but typical
    also you can define it yourself,like
    use constant SERVICE_START_PENDING => 1;

    I'm not really a human, but I play one on earth Remember How Lucky You Are
      My understanding is that the module should be returning a string and not a number, but I'm not sure this is what I'm trying to find out. Is it suppose to return a string or a number, Thanks.
        I googled for the module, and apparently others have reported the same thing as a bug. Maybe the author, was just using those strings as constants in the equalities for clarity in the incomplete examples, but didn't show where he declared them as constants earlier in the script. As a matter of fact, this has to be the case, because the equalities are using numerical comparisons, i.e. == and != on the strings. So they would have to be constants, otherwise he would use eq and ne in the comparisons.

        I'm not really a human, but I play one on earth Remember How Lucky You Are
Re: Win32::Daemon::State returns a 0 or 1
by n3toy (Hermit) on Jan 05, 2009 at 22:12 UTC
    I have used Win32::Service and it returns several codes that I use a hash to get a word based status.
    my %statcodeHash = ( '1' => 'stopped', '2' => 'start pending', '3' => 'stop pending', '4' => 'running', '5' => 'continue pending', '6' => 'pause pending', '7' => 'paused' ); ... print "Status = $statcodeHash{$statusHash{"CurrentState"}}\n";
    Not sure what all Win32::Daemon returns. Maybe this gives you another option.

    Jamie

    UPDATE: fixed small spelling error

      You are right this could be another option, but it seems to only return two values '0' and '1', thanks.
Re: Win32::Daemon::State returns a 0 or 1
by Intrepid (Deacon) on Jan 06, 2009 at 01:08 UTC
    You (azaragoza) wrote:
    When I run the code instead of returning something like, SERVICE_START_PENDING, it returns a zero or a one. Thanks for your help.

    SERVICE_START_PENDING and the other CAPitalized items are capitalized because they are constants. They are preprocessor macros that are defined in the C/C++ API for the MS Windows systems under discussion.

    Perl customarily uses a convention to represent such preprocessor macros. Since Perl isn't C or C-like, it does not use a preprocessor. Instead, these tokens are exported from the module as constant subroutines, ones which have names that are in all-caps. See the constant.pm documentation. A constant subroutine is syntactically a subroutine but it always returns the same value (and so the Perl parser, which is a very smart parser, reduces the subroutine call to a simple constant in the compilation phase).

    You have been suitably punished for not using strict; in your code, and will go on being punished in many others ways as long as you reject or forget this basic level of Perl wisdom (punished by not being told by Perl that you were saying something nonsensical; your constants were being seen as barewords that were interpreted as sloppy unquoted strings because you neglected strict). If you had used strict you would have seen that some subroutines were undefined and therefore compilation was aborted.

    This satisfying form of Instance Justice (to quote Damian Conway) is visited on Perl users who try to get by with merely emulating what they think is the code needed to use a sophisticated interface module - faking it IOW - instead of getting serious and doing things the smart way at each level, from simple and basic like "use strict; use warnings;" to each level upwards in complexity and specialization. To do things the smart way you have to study and learn what the smart way is, and then put it into practice in each piece of code you write. You have to learn Perl in other words.

    HTH

      I humble myself, as unworthy of the great perl coding language and before you oh great indegnant one, but I down loaded this code from the internet and was trying to run it as is so I would not screw it up, but it's not working, so I so humbly asked your wisdom. I added "use strict;" and "use warnings;" and it gave no errors or warnings. Please do not chastise me for my ignorance of perl, I'm trying to learn it by downloading scripts from the web and then trying to apply them.
        If you're playing with the same script I was you may be having the same problem I did. :) You need to include the following: use Win32::Daemon; I had win32::Daemon; which, naturally, did not work. Also, your installation may well not have Daemon, in which case you will need to get it and install (through ppm).

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://734254]
Approved by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others imbibing at the Monastery: (4)
As of 2023-10-02 14:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found

    Notices?