How do I execute a Perl program on Windows without calling perl on the command-line? - perl

I have written a small script in Perl which I am executing on Windows with ActivePerl as below:
C:\Documents and Settings\Administrator> perl io.pl io.pl
#!/usr/bin/perl
use warnings;
sub test6 {
while (defined($_ = <>)) {
#chomp($_);
print $_;
}
}
test6;
As you can see, the code is similar to the Unix cat command:
C:\Documents and Settings\Administrator> perl io.pl io.pl
If I want to execute this script without the perl keyword on the command line, what needs to be done? I want the script to be executed as:
C:\Documents and Settings\Administrator> ./io.pl io.pl

The .pl extension needs to be associated with the Perl interpreter, for one thing. This is easily done by trying to open the script from Windows Explorer -- when you're asked what program to use to open it, browse to perl.exe. And make sure the "always use this program..." box is checked.
Windows likes to check the current directory first, so you don't need to have the "./" in there.

i think specifying the perl install directory location at the start of the perl script should do it.
#!<perl install directory\bin\perl > rather then #!usr/bin/perl

Related

Execute a perl script which doesnt have the .pl extension from another perl script which have the .pl extension

How to execute a perl script with a file named "first" (no extension) from another perl script called second.pl on Windows?
File contents of first:
#!/usr/bin/perl5.8.4 -w
>> Some code
File contents of second.pl:
#!/usr/bin/perl
use strict;
use warnings;
system "first";
So my problem is when I execute the file "first" from the Windows command line, it works
but if I try to run it from my file "second.pl", using system "first";
it fails with the below error:
can't exec "first" : Not a directory at "second.pl" at line 6
Windows uses the file extension to work out how to run a file. As you have no extension the OS is thinking you are trying to open a directory.
You can fix this by changing system "first"; to
system $^X, "first"
This will get Windows to run the current Perl interpreter again, passing your script name as the first parameter, which gets Perl to run it.

Unable to execute perl script in cmd without invoking perl.exe

I am trying to execute a perl script to delete file1.txt in a directory.
When I execute the perl script using command prompt, I faced an error : Use of uninitialized value in chdir at C:/Debug/test.pl line 7.
Example:
C:\Debug>test.pl C:\Debug
However if I invoke perl in front of test.pl, the perl script is executed successfully and file1.txt was deleted.
Example:
C:\Debug>perl test.pl C:\Debug
Please find test.pl code as follow:
use strict;
use warnings;
use File::Copy;
my ($working_dir) = #ARGV;
chdir $working_dir or die "Can't change directory$!";
unlink "file1.txt";
I have mks_toolkit v8.7.5 with perl.exe installed. The .pl extension is associated with "C:\Program Files (x86)\MKS Toolkit\mksnt\perl.exe" "%1" %*.
I have no problem execute hello.pl (Hello World) without invoking perl in front of it.
Example:
C:\Debug>hello.pl
Anyway, users who face this problem can try to check the properties of your file, make sure the file is not read only and has full admin access.

execute shell commands from perl script

I want to rename *.DIF files to *.SUC files
But the following script is giving "sh: bad substitution" error
I cannot use "rename" becuase my OS is not Linux.
$com="for i in *.DIF; do mv \$i \${i/DIF/SUC}; done;";
print $com."\n";
print `$com`;
Output :
for i in *.DIF; do mv $i ${i/DIF/SUC}; done;
sh: bad substitution
If you are having problems with Perl's rename, use File::Copy, a platform-independent module for moving and copying files. It is a core module, so it should be already part of your Perl installation.
If the system command works when you enter it in the shell, but not in Perl, the most likely reason is that Perl isn't using the shell you expect. We would need more information about your system to be sure, though.
There's no need to shell out for file operations that you can easily do within Perl.
The following renames all of your .dif extension files as .suc.
use strict;
use warnings;
use File::Copy qw(move);
move($_, s/dif$/suc/r) for glob('*.dif');
be default perl was using sh, instead of bash, which allows {//}
this question helped.
Finally I used :
open my $bash_handle, '| bash' or die "Cannot open bash: $!";
print $bash_handle 'for i in *.SUC; do mv $i ${i/SUC/DIF}; done;';

How to specify in the script to only use a specific version of perl?

I have a perl script written for version 5.6.1 and which has dependencies on Oracle packages, DBI packages and more stuff. Everything is correctly installed and works.
Recently perl version 5.8.4 got installed and because those dependencies are not correctly set so the script fails.
'perl' command points to /program/perl_v5.8.4/bin/perl -- latest version
So, when I have to run my perl script I have to manually specify in command prompt
/program/perl_v5.6.1/bin/perl scriptName.pl
I tried adding following lines in script:
/program/perl_v5.6.1/bin/perl
use v5.6.1;
But, this means script has to take Perl version > 5.6.1
I found couple of related question which suggested:
To export path. But, I want the script for all the users without have to export path
Specify version greater than. But, I want to use just one specific version for this script.
use/require commands. Same issue as above.
My question: How to specify in the script to only use a specific version of perl?
The problem is that the interpreter specified in the shebang line (#!/some/path/to/perl) is not used if perl script is called like this:
perl some_script.pl
... as the 'default' (to simplify) perl is chosen then. One should use the raw power of shebang instead, by executing the file itself:
./some_script.pl
Of course, that means this file should be made executable (with chmod a+x, for example).
I have in my code:
our $LEVEL = '5.10.1';
our $BRACKETLEVEL = sprintf "%d.%03d%03d", split/\./, $LEVEL;
if ($] != $currentperl::BRACKETLEVEL)
{
die sprintf "Must use perl %s, this is %vd!\n", $LEVEL, $^V;
}
These are actually two different modules, but that's the basic idea. I simply "use correctlevel" at the top of my script instead of use 5.10.1; and I get this die if a developer tries using the wrong level of perl for that product. It does not, however, do anything else that use 5.10.1; would do (enable strict, enable features like say, switch, etc.).

How can I run a Perl script on Mac OS X?

How do I run a Perl script on OS X?
You can run your Perl script by invoking the Perl interpreter and giving your file as input:
perl myprogram.pl
The easiest way to run a perl script is with the option:
perl myprogram.pl
However, you may find it more useful to add a shebang line at the top of the perl file.
#!/usr/bin/perl
print "Hello World!\n";
In order to execute this script, you need to add execute permissions to your program. Run:
chmod +x myprogram.pl
Now, in order to run your script, you can simply type:
./myprogram.pl
A good tutorial on Perl in OSX can be found here:
http://www.mactech.com/articles/mactech/Vol.18/18.09/PerlforMacOSX/index.html
A generic documentation on executing Perl code is of course perldoc perlrun.
To answer your question directly:
You can run a perl script on any Unix system by either having the code evaluated and executed from command line:
perl -e 'print "Hello World\n"';
Or you can save your Perl script to a file (customarily having .pl extension, say script1.pl and with the first line being #!/usr/bin/perl) and then you can execute it as any Unix program (after setting proper execute permissions)
/path/to/script/script1.pl
You can also execute a script from a file by running perl interpreter as the command and giving the script as a parameter (in this case execute permissions to the script are not needed):
perl /path/to/script/script1.pl
For some reason the whole directory didn't work for me but I just did
perl ~/Desktop/file.pl
(you could also use any folder that is in your user-folder after the ~/)