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

I have a script that runs as a Windows Service using the Win32::Daemon module.

It works absolutely fine when it is just running on it's own. However, now I want to call a subroutine in a custom module I wrote. Let's call this module MyParser.pm

The file is located in the same folder as MyService.pl

When I add the the following line use MyParser; the service fails to startup anymore. I can get around that using use lib "$RealDir/";, though I don't understand why I have to.

But the service still automatically stops when I add MyParser->parse($myinput) into the SERVICE_RUNNING code block.

Any suggestions on what my problem could be?

Replies are listed 'Best First'.
Re: Problems with using Modules in script
by tuxz0r (Pilgrim) on Nov 29, 2007 at 01:03 UTC
    If you are running as a daemon/service, does Windows change the root directory of your program once it starts? And, I don't think the "current directory", even if it is the same as the program for your service, is in @INC by default. So, you'll need use lib "$RealDir/"; to find your module.

    Secondly, you'll need to call your parse routine as, MyParser::parse($myinput), not with the arrow syntax.

    ---
    echo S 1 [ Y V U | perl -ane 'print reverse map { $_ = chr(ord($_)-1) } @F;'
    Warning: Any code posted by tuxz0r is untested, unless otherwise stated, and is used at your own risk.

Re: Problems with using Modules in script
by ikegami (Patriarch) on Nov 29, 2007 at 01:07 UTC

    The file is located in the same folder as MyService.pl

    That's irrelevant. What matters with where the file is located relative to the current directory (assuming you're relying on . in @INC). You can synchronize the two by adding the following at the top of your script.

    use File::Basename qw( dirname ); use File::Spec::Functions qw( rel2abs ); BEGIN { chdir(dirname(rel2abs($0))); }

    Or like you said, you can tell Perl where to look by adding the following at the top of your script.

    use File::Basename qw( dirname ); use File::Spec::Functions qw( rel2abs ); use lib dirname(rel2abs($0));

    Note that FindBin does funky stuff, thus its absence from the above snippets.

Re: Problems with using Modules in script
by aquarium (Curate) on Nov 29, 2007 at 03:15 UTC
    BEGIN { push @INC, "d:/full/path/to/additional/lib/dir"; } use MyParser; etc..
    the hardest line to type correctly is: stty erase ^H