#  DaemonServiceFramework.pl
#  Example 8.7:
#  ----------------------------------------
#  From "Win32 Perl Scripting: Administrators Handbook" by Dave Roth
#  Published by New Riders Publishing.
#  ISBN # 1-57870-215-1
#
#  This script demonstrates a framework for a Win32::Daemon based
#  Win32 service.
#
print "From the book 'Win32 Perl Scripting: The Administrator's Handbook' by Dave Roth\n\n";

                
use Win32::Daemon;
Win32::Daemon::StartService();
while( SERVICE_STOPPED != ( $State = Win32::Daemon::State() ) )
{
  if( SERVICE_START_PENDING == $State )
  {
    # Add initialization code here
    Win32::Daemon::State( SERVICE_RUNNING );
  }
  elsif( SERVICE_PAUSE_PENDING == $State )
  {
    # Add pausing code here
    Win32::Daemon::State( SERVICE_PAUSED );
  }
  elsif( SERVICE_CONTINUE_PENDING == $State )
  {
    # Add resuming code here
    Win32::Daemon::State( SERVICE_RUNNING );
  }
  elsif( SERVICE_STOP_PENDING == $State )
  {
    # Add stopping code here
    Win32::Daemon::State( SERVICE_STOPPED );
  }
  elsif( SERVICE_RUNNING == $State )
  {
    # This is the core functionality of the service
    # Add code here to perform whatever work the
    # service must accomplish. Avoid any code that
    # blocks for long durations of time
  }
  sleep( 5 );
}
Win32::Daemon::StopService();
