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

I'm having some difficulty with capturing the info contained in $0 when using ActiveState Perl under Windows.

The environment:

Perl Version: v5.8.4 built for MSWin32-x86-multi-thread
Binary build 810
Built Jun 1 2004 11:52:21

PerlApp Version: 6.0.1 build 138990

OS: Microsoft Windows XP Version 5.1.2600

The script I'm using to reproduce the problem:

use Cwd; use FindBin qw($RealBin); $this_proc = $0; # Get name of this p +rogram $this_proc =~ s#.*\\##; # Just the name.ext $this_proc_path = $0; # Dir. path $this_proc_path =~ s#$this_proc##; $this_proc = $0; # Get name of this p +rogram $this_proc =~ s#.*\\##; $this_proc =~ s/\.pl//ig if ($this_proc =~ /\.pl/); $this_proc =~ s/\.exe//ig if ($this_proc =~ /\.exe/); printf("\$this_proc: $this_proc\n"); printf("\$this_proc_path: $this_proc_path\n"); printf("\$RealBin: $RealBin\n");
Now, when I run the script (p.pl), I get the expected output, viz:

$this_proc: P.PL $this_proc_path: C:\tmp\ $RealBin: C:/tmp
However, when I use PerlApp from the Perl Development Kit, I get the following output:
$this_proc: p $this_proc_path: $RealBin: C:/tmp
The command line I use to invoke PerlApp is:

perlapp --norunlib --clean --force --exe p.exe --perl C:\Perl\bin\perl +.exe p.pl

Is there a reason as to why the output is different... or should this be considered a bug? I remember reading there was a problem with build 804 (I think) but I don't know if it's supposed to be resolved.

Thanks.

John

Replies are listed 'Best First'.
Re: Problems with Contents of $0 when using ActiveState PerlApp
by thundergnat (Deacon) on May 26, 2005 at 00:51 UTC

    This is more likely an issue with PerlApp than perl in general. As such you may be more likely to get useful advice in Active's ASPN forums.

    Interestingly enough, putting - perlapp $0 - into Google and pressing "I'm Feeling Lucky" brings you to this post in the ASPN forums about this very situation.

    HTH

      Good clue, that article, thanks. Overkill, making a function out of it I 'spose, but this seems to work Ok as a script and as an .exe, even if it 'overworks' in its module loading:
      use File::Spec; use File::Basename; [...] sub GetProcInfo { my ($path, @args); if (defined $PerlApp::VERSION) { $path = PerlApp::exe(); } else { $path = File::Spec->rel2abs($0); } @args = fileparse($path,(".pl", ".exe")); return(@args); }
      ...and I know something like:
      (@args) = File::Spec->splitpath( $proc_path );
      could possibly be better still, except my regex is kinda bad and I can't work out how to split the type out of the file returned (I want to be able to use just the name and just the extension/type).

      Thanks again for the help.

      John

Re: Problems with Contents of $0 when using ActiveState PerlApp
by ikegami (Patriarch) on May 26, 2005 at 06:12 UTC
    Is there a reason as to why the output is different...

    $0 contains the command that started the script. Since the Perl script and compiled Perl script were started with different commands, it's only normal for $0 to be different. Perhaps __FILE__ would be more useful to you than $0?

Re: Problems with Contents of $0 when using ActiveState PerlApp
by ChrisR (Hermit) on May 27, 2005 at 19:28 UTC
    Here's another, somewhat ugly, solution that might work no matter how you created your exexcutable (perlapp, perl2exe, etc.).
    sub GetPathAndExe { use File::Spec qw(rel2abs); my $pathandexe; my $exe; my $path; if($0 =~/\.exe$/) { $pathandexe = File::Spec->rel2abs($0); } else { $pathandexe = $0; } ($path,$exe) = $pathandexe =~ /^(.*?)\\([^\\]+)$/; return ($path,$exe); }