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

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.

Related

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.

How to make Perl script executable?

I have given a perl script file executable permission on a Unix based system, but when I try to execute the file I get a "command not found" error. I have #!usr/local/bin/perl at the start of the script file.
At the top of your script, replace the #!usr/local/bin/perl with #!/usr/bin/perl notice the root '/' prior to the usr/
And you can try perl <filename> keyword to run the file instead of ./<filename>, using perl keyword should run the script regardless of the shebang line at the top of your script.
replace your first line #!usr/local/bin/perl with #!usr/bin/perl(which is called Shebang). Then you can run the file with perl <Path To File>.

What is the difference between "perl test.pl" and "./test.pl"?

I have observed that there are two ways of executing a perl program:
perl test.pl
and
./test.pl
What is the exact difference between these two and which one is recommendable?
I will rephrase slightly what other answers stated.
The first case will run the program called "perl" - presumably, a Perl language interpreter, and pass the value "test.pl" to it as the first parameter. Please note that this will do one of 3 things, depending on what "perl" is and what "test.pl" is:
If "perl" does not exist as an executable in your $PATH or a shell alias (check by running which perl), your shell will try to find a non-existing executable, and fail with perl: Command not found error.
If "perl" is an executable in your path (or a shell alias) that is NOT actually a Perl interpreter program, that will get executed instead. As example, try this in csh:
alias perl echo
which perl # Will print "perl: aliased to echo"
perl test.pl # Will print "test.pl". NOT what you intended!
unalias perl
This will execute your "perl" alias and simply echo the word "test.pl"
If "perl" is an executable in your path that IS a real perl interpreter, it will pass "test.pl" to it as a first parameter. In that case, Perl interpreter will treat this parameter (as it doesn't start with a "-") as the name of a file containing Perl code to execute and try to read the file in, compile it as Perl code and execute it.
Note that, since the program being run is actually "perl" and "test.pl" is just a text file being read in, "test.pl" does NOT need to have the "execute" Unix file permission.
The second case, shell will try to find a file called "test.pl" in your current directory, and - if it exists AND is executable - try to execute it as a program.
If the file does not exist OR if the execute bit on it is not set, the shell will fail with "command not found" error.
If the file has the execute bit set, shell (or actually process loader in Unix kernel) will try to execute it. The rules by which Unix executes a given executable file is governed by the first 2 bytes of the file, aka "magic number".
For a VERY good in-depth coverage of how magic numbers work, see "How does the #! work?" question on SO.
In a special case where the "magic number" is "#!" (aka "shebang"), the loader will read the first line of the file, and treat the contents of that line (sans the first 2 bytes) as a command to run instead of the given executable file; and append the path to the executable file as one more parameter to the command it read from shebang line. As examples:
if "test.pl" is a text file with a first line of #!/bin/sh -x, the kernel will execute /bin/sh -x ./test.pl.
if "test.pl" is a text file with a first line of #!/usr/bin/perl, the kernel will execute /usr/bin/perl ./test.pl.
if "test.pl" is a text file with a first line of #!perl, the kernel will execute perl ./test.pl.
if "test.pl" is a text file with a first line of my $var = 1; (or any other first 2 bytes it doesn't know what to do with), it will either error out or (at least on RedHat Linux) will pretend that there was an implied #!/bin/sh shebang and try to execute the file as Bourne Shell script. Which will of course fail since it was Perl code, not shell script
In the first case you are starting the perl interpreter and asking it to use your file and run it.
In the second case you are asking your shell to execute your file. This requires that the file starts with
#!/<path to perl>/perl
and that the file has the execute bit set.
The best method to use is the one that fits your usecase the best.
The first one will always run the script as the perl code.
The second one will do it only in case the perl is specified in she-bang. Otherwise it will run it as shell code or whatever is specified in she-bang (if there is no she-bang at all it will run as current shell code).
The first one will be executed even noexec mount option is enabled.
The second one will fail in that case.
The same stuff with execute bit. The first one will work if +x isn't setted, the second will fail.
The first executes the program using the perl that is found first in your $PATH. The second uses whatever shebang line in the program says.
If u set the executable permissions to the file , then you can run the file by ./ or else run using perl filename.pl
perl test.pl
Specify to the shell that you want the current Perl executable (as is found in your $PATH) to execute the test.pl file that is located in your $PATH.
Run which perl to quickly see what version of perl is the default
Run echo $PATH to see where the '.' (current directory) is. ALL directories BEFORE the '.' will be checked FIRST for the test.pl file! Use ./test.pl instead so the shell looks in the current directory only...Unless you want it to hunt in the $PATH for the test.pl file.
./test.pl
Specify to the shell that you want the test.pl file, in the current directory, to be run by the executable as specified inside the test.pl file at the line with the she-bang (line that starts with #!).

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

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

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 ~/)