Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation

Avoiding Hardcoded path of Perl.exe in coding

by toastbread (Novice)
on Jan 06, 2010 at 19:58 UTC ( #815972=perlquestion: print w/replies, xml ) Need Help??

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

Hi monks,

Just want to ask if there's a function or any other way I
can get the path of installed perl.exe to avoid typing the
hardcoded path in my perl source code?

Here is my current code(I've edited sample code in Perl Cookbook):

Win32::Process::Create($Win32::Process::Create::ProcessObj, 'C:/Perl/bin/perl.exe', #<--THIS LINE IS MY CONCERN 'perl', 0, # Don't inherit. DETACHED_PROCESS, # ".") or # current dir.

I'm trying to remove DOS Window from Perl Tk program which
I succesfully did but I couldn't find a built in funtion to
get the path of perl.exe to supply the 3rd parameter of

My perl application will be used in different PC and Perl
was installed in different paths and drives in some PCs. So
I need to not use the hardcoded path.

I am planning to use the "ftype perl" in command line using
system() to get the perl.exe path but I think it is better
to use perl's way since "ftype" is not available in other

I'd tried to search functions and Environment variables but
I couldn't find what I want.

I hope there is a way other than "ftype perl".

Please lend me the wisdom for this problem.


Replies are listed 'Best First'.
Re: Avoiding Hardcoded path of Perl.exe in coding
by Corion (Pope) on Jan 06, 2010 at 20:13 UTC

    Just use $^X, which contains the currently running Perl executable. You'll need to properly quote it if you're interpolating it into a command line, if $^X contains whitespace:

    my $perl = $^X; $perl = qq{"$perl"} if $perl =~ /\s/;
Re: Avoiding Hardcoded path of Perl.exe in coding
by BrowserUk (Pope) on Jan 06, 2010 at 20:06 UTC
    [0] Perl> print $^X;; C:\Perl64\bin\perl.exe
Re: Avoiding Hardcoded path of Perl.exe in coding
by thunders (Priest) on Jan 07, 2010 at 05:21 UTC

    On Mac OS X v10.6 $^X does not give an absolute path, Instead I just get "perl". On linux I get "/usr/bin/perl" for $^X, on windows I get "C:\Perl\bin\perl.exe" for $^X.

    If you happen to need the full path, and you want it to work cross platform, this should always give a full path, regardless of platform:

    use Config; print $Config{perlpath}
      From perlvar
      use Config; $secure_perl_path = $Config{perlpath}; if ($^O ne 'VMS') {$secure_perl_path .= $Config{_exe} unless $secure_perl_path =~ m/$Config{_exe}$/i;}
Re: Avoiding Hardcoded path of Perl.exe in coding
by MadraghRua (Vicar) on Jan 06, 2010 at 20:19 UTC
    You could try a few things:

  • Use system to run Perl -v and use that to figure out where Perl's libraries are. You could then walk up the library locations to test for a bin directory. For instance ActiveState Perl has a pretty standard installation, I'm sure other Perls do as well;
  • Use a standardized installation for Perl on all recipient machines - that way you known where it is always going to be. Make sure perl is part of your PATH variables. For instance if you run set through a system or cmd call, you can grep for PATH and then grep for the instance of your Perl installation. That's something you may have to organize with your IT department;
  • Use a packaging program to bundle up Perl with the associated program and modules you need to run your stuff. I use ActiveState's PDK PerlApp for this. Its pretty robust and I know I can create something that other IT groups can install on a large number of computers.

    Of course if you can run the app through the web you can avoid installation issues...

    Hope that helps.

    yet another biologist hacking perl....

      Thanks to your replies! =)

      "$^X" is working fine in my code. Now I can remove the part that uses "ftype perl". I did a file manipulation and Regex when I used ftype command just to get the path. And it took more lines.

      Thanks to suggestions of MadraghRua. I might use those someday when I run my own network of computers and do a large application using perl. Currently I'm a perl newbie but I think I could improve a lot from this site. This is my first pose and I've got what I want. Thanks again for your replies.


      P. S.
      At first, I nearly stop proceeding from this site because its hard to locate links and read texts. It's just purely text, lines and borders. I need to enlarge the fonts with the mouse just to read easily some text lines especially the codes since they are very small. But I'm becoming used to it. Still, it's more comfortable if it has nice design. Could I request to the elder monks to add some design and user interfaces to the pages?
        P. S. ...

        I'd suggest that you re-post your PS as a new node in PerlMonks Discussion. It might not get noticed by the right people tucked away down here.

        Someone will likely be able to tell you how to add some CSS to your settings to increase the font size(s) used.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://815972]
Approved by MadraghRua
Front-paged by MadraghRua
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (5)
As of 2019-10-19 17:59 GMT
Find Nodes?
    Voting Booth?