path of perl scripts [closed] - perl

Closed. This question does not meet Stack Overflow guidelines. It is not currently accepting answers.
Questions concerning problems with code you've written must describe the specific problem — and include valid code to reproduce it — in the question itself. See SSCCE.org for guidance.
Closed 9 years ago.
Improve this question
I am following the book 'Google Hacks'. It has many ready-made perl scripts, but it does not clearly say exactly where I need to save those perl scripts. I think I have to invoke these scripts from command line, but cannot figure out the location where these scripts should be stored. I am using Strawberry perl.
Also, it would be helpful I can run perl scripts from local environment setting with Internet connection, for example, to scrap Google search results. Thank you
note: I am using Apache webserver and windows 7 operating system

Are they on your server, or are they on your local machine? I'm assuming they're on your local system:
On Unix/Mac/Linux, the setup is fairly straight forward:
Make sure that the shebang points to your Perl executable. I make it #! /usr/bin/env perl which will find the perl command in my path.
Put the scripts in a directory in your $PATH variable.
On Windows, the setup is a bit more involved:
You need to setup your %PATH% environment variable to point to include your Perl scripts. You can do this by going into the System Control Panel (the easiest way is to right click on Computer on your desktop, and select Properties. Then go into Advanced Settings, and click on the Environment Varables... button on the bottom. Path is one of the System Variables.
You need to associate your *.pl suffix with your Perl executable. The problem is that Microsoft keeps changing this. However, the following works for Windows 7 and earlier:
Find a Perl script and view it in Windows Explorer. Right click on it, and open Properties. In Properties, there's an Opens with selection. Click on the Change button and select your Perl interpreter as your interpreter. Make sure the Always use the selected program to open this kind of file checkbox is checked.
Finally, you may want to set %PATHEXT% to include .pl as one of the executables. This way, you can type your Perl program without having to type the suffix all of the time. On Unix/Linux/Mac, you don't need suffixes because the shebang will point to the correct interpreter, but Windows must have a suffix associated with the executable. Now, instead of typing foo.pl on the command line, you can just type foo.
Once this is done, you can simply type in the name of your executable Perl script from any directory in the Command Prompt terminal and run your program.

The 1st thing you need to do, is to make sure that the .pl (or as some use, .plx) extension is associated in the windows registry as a perl program and that perl programs should be run by your perl.exe. At this point you should be able to run perl programs that don't require command line parameters just by double clicking them in Windows Explorer. I know that the installer for ActiveState Perl does this for you by default. I can't vouch for Strawberry.
To run them on the command line, you can just name their complete path ( C:\Mypath\Myprog.pl) or put them in a directory named in your PATH environment variable.
If you're trying to run them via Apache, you'll need to configure Apache with a CGI-BIN directory and put your CGI compatible perl programs in that directory. Again, Apache on Windows depends on the file extension associations in the registry, so make sure you've done that 1st step.

Related

Adding perl script to module distribution and making it accessible globally

I'm wondering if there is a tutorial or simple way to add perl scripts which I've written to be accessible to the user globally.
for eg. you apps like Carton, Cpanm, much like how npm has an npm install -g option.
I tried placing my scripts in the bin directory of my perl package but its not working.
The App::* area of cpan contains installable tools and utilities. Looking at a small one, like App::p I saw that its structure looked like it contained a Makefile.PL that used ExtUtils::MakeMaker to define how to build test and install the relatively small perl script.
However, if you're talking about just taking a script you wrote and making it something executable by you, the user, then it's basically the same way all scripting works (apologies if you weren't asking this):
One Unix, Linux and Mac OS X when using a shell like bash:
Any file can be made executable to the user by running chmod u+x filename
An executable file should start with a magic number, for plain text files containing scripts, the shebang is that number. So start your files with (as covered in man perlintro):
#!/usr/bin/perl
use strict;
use warnings;
When specifying an executable file from the command line you can type in the full path, possibly with conveniences from your shell. EG the file is at $HOME/myscripts/runme.pl you can type that at the prompt, the expanded /home/username/myscripts/runme.pl (/Users/username/myscripts/runme.pl on Mac OS X) or ~/myscripts/runme.pl at a bash prompt. Or if runme.pl is in your current working directory: ./runme.pl
If you just type runme.pl sh and bash and many shells will search your PATH variable in order and run the first runme.pl in any of the specified paths.
So if echo $PATH gives you something like: /usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin and you want the example in point 3 to work with just runme.pl you should
Either move the file into one of those directories (not recommended).
Or add export PATH=$PATH:$HOME/myscripts to the bottom of your ~/.bashrc or ~/.profile file or likewise for your preferred shell. Be sure to open a new login shell session, EG close that terminal window and open a new one.
On Windows, this is an exercise left to the reader. ☺

How can I run vim from PowerShell just by typing vim?

I just installed vim. It's awesome. From PowerShell, I want to be able to launch it both via typing vim and by typing vim someFile.txt.
I can already open vim through the run dialog. Further, it's already in my system PATH. What must I do to launch it from PowerShell?
If it's already in the path, it should work.
But you can, alternatively, use an alias via New-Alias. If you add that to your profile, it will load every time.
To see how the run box behaves differently from powershell, and how you might get around that, see my answer here:
Run a program by name from PowerShell (similarly to the run box)

Eclipse PyDev use remote interpreter

is there a posibility to make eclipse PyDev use a remote Python interpreter?
I would like to do this, as the Linux Server I want to connect to has several optimization solvers (CPLEX, GUROBI etc.) running, that my script uses.
Currently I use eclipse locally to write the scripts, then copy all the files to the remote machine, log in using ssh and execute the scripts there with "python script.py".
Instead I hope to click the "run" button and just have everything executed within my eclipse IDE.
Thanks
Unfortunately no. You can remotely connect to your Linux server via Remote System Explorer (RSE). But can't use it as a remote interpreter. I use Pycharm. You can use the free Community Edition or the Professional Edition for which you have to pay for it. It is not that expensive and it has been working great for me.
As Adel says, this is probably not possible with the Remote System Explorer, or the normal Run button,
but you can automate the process you currently use. I had to do this for a few weeks when the fan was broken
in my laptop, and doing any significant computation there made it overheat and poweroff, so I just ran
everything on my work machine.
You can use the External Tools mechanism to run a short script that syncs your code to the remote server,
runs your script, then syncs back any output files to your local machine. My script looks like this,
is stored in $HOME/bin/runremote.sh, and is executable (chmod +x runremote.sh)
fp="$1" # Local path to the script we want to run--for now,
# this is the only command I pass in from Eclipse, but you could add others if so inclined.
# My home directory is a little different on my local machine than on the remote,
# but otherwise things are in the same place. Adjust as needed.
fp=`python -c "print '$fp'.replace('/home/tsbertalan', '/home/oakridge/bertalan')"`
# Run the synchronization. I use Unison, but you could use something else,
# like two calls to rsync, or a series of scp commands.
reposync >/dev/null # The redirection assumes your sync command will print errors properly on stderr.
cd='cd '`dirname $fp`
# I use a virtual environment on the remote server, since I don't have root access to install
# packages globally. But this could be any set-up command you want to run on the remote.
# A good alternative would be `source $HOME/.profile` or `~/.bashrc`.
act='source /home/oakridge/bertalan/bin/activate'
fname="`basename $fp`"
cmd="$act ; $cd ; python $fname"
# Run the command remotely. The -X forwards X11 windows, so you can see your Matplotlib plots.
# One difficulty with this method is that you might not see all your output just as it is created.
ssh bertalan#remote.server.edu -X "$cmd"
sleep 1
# My synchronization script is bidirectional, but you could just use rsync with the arguments flipped.
reposync >/dev/null
If you don't use linux or OSX locally, you'll probably have to use MinGW or Cygwin or whatever to get
this working. Or, since you appear to have a working Python interpreter, you could write an
equivalent script in Python, make it executable (by the file properties dialog in Explorer, I think),
and add a #!/path/to/python line at the top. I don't use Windows regularly, so I can't really help with that.
To use this in Eclipse, go to Run > External Tools > External Tools Configurations.... Add a new tools
whose Location is the path to your script, and whose first Argument is ${resource_loc}.
You can then use it with Run > External Tools > [first item], or bind it to a keyboard shortcut (I used F12)
by going to Windows > Preferences > Keys, and searching for "Run Last Launched External Tool". Presumably you'll
have to go through the menus first to make this the "Last Launched" external tool.

How can I run a Perl script with ActivePerl?

I just recently installed ActivePerl 5.12.2.1202 on my Windows XP in C:/Perl. I am new to Perl scripting.
I just want to run a Perl program which contains one print statement, which I saved in Notepad with the name ex.pl.
How can I run this Perl program?
Can I use an editor for typing a Perl script other than Notepad?
How do I use ActivePerl?
Run Perl program from command prompt
start->run>cmd (command prompt will appear), write perl full_path_of_your_script,
like
C:\> perl hello.pl #This assumes that perl is in your PATH environment variable.
There are many Perl Editors, you can
used for Perl scripts like DzSoft,
Perl Expess, Komodo Edit etc and also see http://www.perlmonks.org/?node_id=169668 and Perlfaq3- Windows Perl Editors for more detail.
Have a look at http://docs.activestate.com/activeperl/5.12/, for ActivePerl 5.12 documentation.
Perl programs (or any other program run by an interpreter) is run by passing the script as a command-line argument to the interpreter. For example, in this case:
perl.exe ex.pl
Padre the Perl IDE and Kephra are good editors for Perl.
As an alternative to ActivePerl, there is Strawberry Perl.
Perl programs are run using the Perl interpreter, perl.exe.
This is normally done from the command line:
C:\>C:\Perl\bin\perl ex.pl
If perl.exe is in your PATH environment variable that can be shortened to:
C:\>perl ex.pl
If you opted to have *.pl files associated with Perl during installation, you can also double-click on them from Windows Explorer.
If you have *.pl files associated with Perl and add .PL to your PATHEXT environment variable you can run them like any other executable:
C:\>ex
Perl programs are just text files. They can be edited with any text editor (Padre, Kokomo, vim, emacs, Notepad++, etc.). Use whichever one you like best.
ActivePerl is just a distribution of Perl. "Using" it usually means running perl.exe to execute your program. ActivePerl also includes the PPM (Perl Package Manager) utility to make it easier to install modules from CPAN, particularly if you don't have a C compiler available. Most experienced Perl developers prefer to use the cpan shell.
Just to add to the other answers, I use EPIC, the Perl eclipse plugin. I'm using Perl on a Windows 7 64 bit machine.
I still run scripts off the command line in windows using ActivePerl, but for development I like being able to dynamically step through the script line by line.
just to not paraphrase other answers and to be more helpful (even if the post is somehow old ) i recommend using the switch "-e" to run Perl scripts if they are composed of few statements e.g
Perl -e "print('hello')"
and of course this requires that the Perl executable is in the Path variable,if not and assuming Perl is under the folder c:\Perl , you can add it by taping :
set %PATH%=%PATH%;c:\Perl\bin
when it comes to the choice of text editor ,I'm still using Notepad++ for almost everything ,it come with some useful features like keywords highlighting and some auto-completion capabilities.

Perl execution from command line question

I replaced ActivePerl with Strawberry Perl on my WinXP last week.
I found I must run my Perl script with the command of perl myperl.pl; otherwise I only need run myperl.pl before install Strawberry. How can I only run myperl.pl as before?
I checked my environment configuration as below.
C:\> Path
C:\Program Files\ActiveState Komodo Edit
5\;C:\Perl\site\bin;C:\Perl\bin;C:\Perl\bin\;C:\Program Files\CodeSynthesis
XSD 3.2\bin\;%SystemRoot%\system32;%SystemRoot%;%SystemRoot%\System32\Wbem;
C:\Program Files\ATI Technologies\ATI Control Panel;C:\Program Files\Microsoft
SQL Server\90\Tools\binn\;C:\Program Files\Common Files\Thunder Network\KanKan
\Codecs;C:\strawberry\c\bin;C:\strawberry\perl\bin`
Strawberry Perl path already listed in the Path value after install successfully.
Anything I missed? Thank you for your suggestion.
Try from command prompt:
assoc .pl=PerlScript
ftype PerlScript=c:\strawberry\bin\perl.exe %1 %*
--
kmx
You need to associate .pl file extension with Strawberry Perl's executable (it's a Windows thing, not a Perl thing).
To do so, there are a couple of ways (you can google or ask on ServerFault for the best one or full list), but the one I usually use on XP is:
Open any folder Windows Explorer
Go into "Tools" menu, and click on "File Types" tab in the resulting dialog
Find "PL" extension in the list
If it's there, correct the associated executable to be Strawberry Perl's .exe by clicking on "PL" line and clicking "Change" button
If it's not in the list, click "New" button, type in PL extension in the form and click "OK". Then close the whole dialog, and re-open it again. The "PL" extension will now be in the list, so click on it and click "Change" button
In case I messed up, here's the official instructions from Microsoft:
http://support.microsoft.com/kb/307859
UPDATE
Please see kmx's answer - his method is all-command-line and as such seems much more preferable to me that GUI blundering... I confirmed that it works too (without parameters, at least)
You might need to put the .PL extension into the PATHEXT environment variable. This will make .pl files work with the PATH variable.
Type
set PATHEXT
to see if you're set up. If you're not, then go to My Computer->properties->Advanced and press the "Environment Variables" button on the bottom of the tab. There you can add .PL to the PATHEXT variable.
OR you could just set it in some batch file that you run to initiate cmd:
set PATHEXT=%PATHEXT%;.PL
For completeness here's 2 other methods that don't make a .pl script executable, but you could use them to make your perl script into an executable.
pl2bat which is suppose to create a wrapped version of your .pl into a .bat file which would be executable on a windows system. I haven't tried this so YMMV.
pp, part of the PAR::Packer module which I have used before works pretty well at rolling your perl scripts into executables.