Beefy Boxes and Bandwidth Generously Provided by pair Networks
laziness, impatience, and hubris
 
PerlMonks  

Win32::daemon and Perl

by becca23 (Novice)
on Jun 03, 2009 at 20:14 UTC ( [id://768149]=perlquestion: print w/replies, xml ) Need Help??

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

I am attempting to use Win32::daemon to create a windows service. To start off easy, i installed a windows service using the code found on the html documentation for Win32::daemon.
use Win32::Daemon; %Hash = ( name => 'PerlTest', display => 'Oh my GOD, Perl is a service!', path => 'c:\perl\bin\perl.exe', user => '', pwd => '', parameters =>'c:\perl\scripts\service.pl', ); if( Win32::Daemon::CreateService( \%Hash ) ) { print "Successfully added.\n"; } else { print "Failed to add service: " . Win32::FormatMessage( Win32: +:Daemon::GetLastError() ) . "\n"; }
This installed the service fine with no errors. Then I tried to use the example service code:
use Win32::Daemon; # Tell the OS to start processing the service... Win32::Daemon::StartService(); # Wait until the service manager is ready for us to continue... while( SERVICE_START_PENDING != Win32::Daemon::State() ) { sleep( 1 ); } # Now let the service manager know that we are running... Win32::Daemon::State( SERVICE_RUNNING ); # Okay, go ahead and process stuff... open (OUT, ">>C:\perl\scripts\out.txt"); print OUT "Hello, World!"; close OUT; # Tell the OS that the service is terminating... Win32::Daemon::StopService();
But I am getting an error saying that the service started and stopped, and the file is not being opened. Please Help!

Replies are listed 'Best First'.
Re: Win32::daemon and Perl
by almut (Canon) on Jun 04, 2009 at 02:33 UTC
    open (OUT, ">>C:\perl\scripts\out.txt");

    When you use double quotes, you also need double backslashes

    ">>C:\\perl\\scripts\\out.txt"

    Alternatively, use single quotes

    '>>C:\perl\scripts\out.txt'

    Also, with most IO, it's a good idea to do some error handling, e.g.

    open (OUT, '>>C:\perl\scripts\out.txt') or die "...some informative me +ssage...: $!";

    (I'm not sure whether it's best to die in this case... this is just an example — modify as appropriate.)

      One could also use forward slashes. DOS and Windows really don't care if the slash is forward or backward. Only some legacy programs assume that filenames can't contain forward slashes, and explicitly check for them instead of just passing them to the API functions.

      Alexander

      --
      Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        One could also use forward slashes.

        That's right.  It's even better actually, because in contrast to using single quotes and backslashes, you don't need to remember to take care of the special case when you have a path with a trailing separator, e.g.

        my $dir = 'c:\perl\scripts\'; # wrong my $dir = 'c:\perl\scripts\\'; # ok when last backslash is escaped vs. my $dir = 'c:/perl/scripts/'; # ok my $dir = "c:/perl/scripts/"; # ok

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others cooling their heels in the Monastery: (2)
As of 2024-04-20 12:29 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found