in reply to finding the path of the script running as Win service

Parsing $0 might not be very pretty, but if it works, why not use it?

Anyway:

my $foo = 'bar'; BEGIN { print $foo; }
Your my statement is executed in 2 different stages: the declaration is run at "compile time" (as soon as the compiler sees it), and the assignment is run at "run time".

Begin blocks also run at compile time, so the order of execution of the above code would be something like:

my $foo; print $foo; $foo = 'bar';
This also explains why the code also runs under strict: BEGIN does not have its own namespace and lexical variables don't use namespaces anyway.

Replies are listed 'Best First'.
Re^2: finding the path of the script running as Win service
by punkish (Priest) on Jan 05, 2005 at 22:44 UTC
    thanks for the explanation re compile and run times. Now, if someone to be kind enough to explain what is Boris doing in the above regex... I don't like magic.

    ;-)

      The regex matches a backslash (\\) followed by any number of non-backslash characters ([^\\]*) followed by end-of-string ($). This forces the matched backslash to be the last backslash in the string, so the matched non-backslash characters are the filename ("foo.exe"). The matched characters ("\foo.exe") are replaced by the empty string, which gives the directory containing the script file ("E:\programs\foo").

      The OP accomplished this less efficiently by splitting the string on backslashes (("E:","programs","foo","foo.exe")), popping the last element of the array ("foo.exe", leaving ("E:","programs","foo")), and rejoining the array with backslashes ("E:\programs\foo").