You'd use Win32::GUI's Hook method and provide a callback (or maybe even use Win32::API to wrap your own hook),
or you'd use Wx and provide a wxApp::OnQueryEndSession/wxApp::OnEndSession.
If you're using Win32::DAEMON, you should use RegisterCallbacks ...
MJDsays "you can't just make shit up and expect the computer to know what you mean, retardo!"