Patient monks,

I come again with another in a continuing line of questions -- How do I find the path of the script running as a Win service? I have the following scenario.

  1. foo.pl is converted to foo.exe using pp.
  2. foo.exe is then copied to another computer, say, under E:\programs\foo\foo.exe
  3. The Win command sc is used to create a service called foo.
I want foo, the service, to figure out the path of foo.exe.

Here is what I have tried --

File::Spec->rel2abs(curdir()), $FindBin::Bin, Cwd->cwd() all suffer from the same problem thusly: the path returned is the ostensible path to the service, of the form "C:\Windows\System32", not "E:\programs\foo", the path to foo.exe that is being run as that darned service.

I finally implemented the following solution --

@path = split(/\\/, $0); pop(@path); $path = join('\\', @path);
The above works, however, is there a less ugly solution?

I have a related question --

BEGIN { my $foo = 'bar'; } print $foo;
doesn't work for obvious reasons. However, neither does
my $foo = 'bar'; BEGIN { print $foo; }
Why? Does BEGIN have its own namespace? How can I declare something once (like the $path to the script I am trying to figure out above), and have it be picked up in the BEGIN block as well as in the rest of the script or any other module that I might use in the script?

Many thanks.


In reply to finding the path of the script running as Win service by punkish

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.