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

I'm trying to run some scripts from the Windows command prompt. perl.exe is in my path, and the script is in another directory. For example:
c:\data> perl c:\perl\test.pl

I'd like to add c:\perl to some environment variable so running the following command would work:

c:\data> perl c:\perl\test.pl

I've tried setting LIB, LIB_PATH, and LD_LIBRARY_PATH to c:\perl with no luck. Is there an easy way to do this? I figure if I can do this in Java with CLASSPATH, then I Perl may have an equivalent.

Thank you.

Replies are listed 'Best First'.
Re: Running Perl Scripts From Another Windows/DOS Directory
by halley (Prior) on May 18, 2003 at 01:12 UTC

    If you used ActiveState's distribution of Perl, it associates .pl with perl.exe. You should be able to just type "test.pl" or at the worst, "start test.pl" when in that directory, and it should run.

    Once that's true, then the environment variable you want to adjust is the plain executable searching PATH.

    However, if you can't get the associations worked out properly, you might want to look at the docs for perlrun and the PERLLIB environment variable. PERLLIB works the most similar to Java's CLASSPATH or Unix's LD_LIBRARY_PATH. It prefixes PERLLIB onto Perl's @INC array. It's probably better to set that as a last resort for modules, not main scripts.

    --
    [ e d @ h a l l e y . c c ]

Re: Running Perl Scripts From Another Windows/DOS Directory
by Zaxo (Archbishop) on May 18, 2003 at 01:13 UTC

    PATH is the environment variable you are looking for.

    After Compline,
    Zaxo

Re: Running Perl Scripts From Another Windows/DOS Directory
by krisahoch (Deacon) on May 18, 2003 at 01:27 UTC
    svsingh,

    What exactly are you trying to do here?

    c:\data> perl c:\perl\test.pl
    
    I'd like to add c:\perl to some environment variable so running the following command would work: 
    
    c:\data> perl c:\perl\test.pl
    
    The two different things that you are trying to do look exactly the same.

    Are you trying to

    • Be able to type the name of the file which is in the current directory and have the file execute with perl?
    • Type perl test.pl and have test.pl execute regardless of the directory that you are in?
    • Both
    • Niether?
    Please let us know so that we can help.


    Thank you,

    Kristofer Hoch

    Si vos can lego is, vos es super erudio

      Sorry, I messed up on that. The second command line should be
      c:\data> perl test.pl
Re: Running Perl Scripts From Another Windows/DOS Directory
by WhiteBird (Hermit) on May 18, 2003 at 01:33 UTC
    What Windows OS? On my Win2k system the registry setting has the default directory of perl as
    C:\Perl\
    The path to perl or "BinDir" is
    C:\Perl\bin\perl.exe
    If there are other settings, I don't know where they would be. However, I can be in one directory and run code from another by typing:
    C:\DirectoryOne>perl C:\DirectoryTwo\Somescript.pl
    and it works. (Note that the "C:\DirectoryOne>" above is the command line.)Your example confuses me--unless your test.pl script actually is in your top level Perl directory. And that seems like an untidy way to do things. My personal opinion is to get my personal scripts in a directory outside of the main Perl directory. (YMMV)
Re: Running Perl Scripts From Another Windows/DOS Directory
by cciulla (Friar) on May 18, 2003 at 04:04 UTC

    At minimum, your "PATH" environment variable should include "c:\perl\bin". Although you did mention that perl.exe is in your path -- please double check.

    If you want to run your "test.pl" script anywhere on your system, ensure that the script's directory also resides within your "PATH" environment variable.

    Assuming you're running some variant of NT (e.g, Win2k or WinXP), you can also add ".pl" onto your "PATHEXT" environment variable saving you some precious keystrokes and be able to run your script just like a ".bat", ".cmd", or whatnot.

    Unfortunately, this doesn't work with Win9x -- I don't know about WinME, but I would hazzard to guess that it suffers from the same limitation1 -- so your milage may vary.

    1 Some may argue that running any operating system from MicroSoft is inherently limited.
Re: Running Perl Scripts From Another Windows/DOS Directory
by Zero_Flop (Pilgrim) on May 18, 2003 at 05:39 UTC
    As mentioned if you have ActiveState then the .pl extention should be tied to perl.exe already. This will allow you to select the script with the mouse or to run the script with some thing like c:\path\name.pl or if you are in the directory just name.pl
    One thing I do is to create a directory c:\bin. Then add this to your search "PATH". I then dump any compleate script that I want to have access to all the time in that directroy. So no matter were I am, I have myscript.pl available as myscript.pl thisoption
Re: Running Perl Scripts From Another Windows/DOS Directory
by CombatSquirrel (Hermit) on May 18, 2003 at 08:58 UTC
    Windows sometimes behaves a bit strangely with directories. Are you sure that it doesn't find perl.exe and not just misinterpret the parameter? I sometimes had problems with that; it all worked when I used C:\data> perl "C:\perl\test.pl".
    Otherwise you should, as suggested, add the directory C:\Perl\bin to your path by entering the following in the command prompt: C:\data> PATH "C:\Perl\bin"; %PATH%
Re: Running Perl Scripts From Another Windows/DOS Directory
by hawtin (Prior) on May 18, 2003 at 09:52 UTC

    Another thing to look for is that the backslashes are not messing you about. In ActiveState I always use / inside the script (despite being on Windows).

    Try setting the command you are going to execute into a string and printing it

    $verbose = 1; ... { my $cmd_str; $cmd_str = "perl c:\\perl\\test.pl"; print "$cmd_str\n" if($verbose); system $cmd_str; }

    Untested code