Usr bin perl windows

1. Есть много способов: можете писать руками путь/к/перлу/perl скрипт.pl (точнее, у вас в качестве пути может быть что-нибудь вроде C:\Program Files\Some Perl\bin), можете добавить путь к перлу в переменную окружения %PATH% и запускать скрипты командой perl скрипт.pl, можете настроить ассоциацию перловых исходников с интерпретатором — вариантов масса. А ещё был когда-то cygwin, где перл конечно же был, а ещё в свежих версиях Windows 10 есть линуксовая подсистема, где тоже перл должен быть.

2. #!/usr/bin/perl — так называемый shebang, особый комментарий, где после решётки с восклицательным знаком пишется путь к интерпретатору (это может быть не только перл) и, если надо, дополнительные параметры (например, #!/usr/bin/perl -F' ' -nla). В юниксоподобных системах это имеет значение — при запуске скрипта без указания команды вызова интерпретатора операционная система как раз в shebang и смотрит, чтобы понять, как же выполнить скрипт — бывают скрипты и без расширений. Под Windows shebang скорее всего роли не играет.

3. Чтобы перл больше ругался, используйте

use warnings;
use strict;

Вместо первой из этих строк можете добавить ключ -w при запуске перлового интерпретатора.

4. IDE с поддержкой перла существуют. Лучшее из того, что попадалось мне под Windows — Komodo IDE. Есть упрощённая бесплатная версия — Komodo Edit: это уже не IDE (нет отладчика, юнит-тестов и ещё кучи всякой всячины), а просто навороченный редактор с поддержкой перла.

Posted by: ibmadmin | January 21, 2012

How To Run PERL Program in Windows And UNIX OS

Shebang Line

The PERL programming help system administrators and middleware administrators to simplify their day-today administration work.  Under the UNIX environment, the program execution (i.e. shell or C or PERL program) is determinded by the first line of the program.  This is called “the shebang line” in Shell / Perl culture, or alternately the “sharpbang” or the “hash-bang”. The following are the example to run a program in bash shell or sh shell or PERL programs using  Shebang line.

#!/usr/bin/bash

#!/usr/bin/sh

#!/usr/bin/perl

If the programmer did not specify the shebang line in their program first line, then we need to run the program using the following standard. We are considering FirstProgram.sh, SecondProgram.sh and ThridPERLProgram.pl programs without shebang line. So we need to following the below method to run these programs.

$/usr/bin/bash  FirstProgram.sh

$/usr/bin/sh SecondProgram.sh

$/usr/bin/perl ThridPERLProgram.pl

Consider the suituation where we need to develop the PERL program in a Windows environment and run the same programs in UNIX environment. In this case, we need to keep the shebang line in the Windows environment, but we may installed the PERL in C:\Strawberry\perl\bin folder.

So it is difficult to keep the PERL program first line as #!C:\Strawberry\perl\bin\perl.exe and change in all files while moving into UNIX environment.

To resolve this issue, we can use hard link concept in Windows (link concept in UNIX, if you installed the PERL in a different location other than /usr/bin/perl).

Assume we have installed Strwberry PERL in C:\Strawberry\perl\bin directory structure. So to achieve /usr/bin/perl shebang line in Windows, create folder under C: as usr-> bin.

Inside the C:/usr/bin folder create a hard link .Note that this is not shortcut which we create it in our desktops and other area. Please refer the following links to know more about links concept

http://www.howtogeek.com/howto/16226/complete-guide-to-symbolic-links-symlinks-on-windows-or-linux/

How To Create Hard Link

In Windows 7:

Hard link creation in Windows 7 and Windows XP have different approach. In Windows 7, we can use fsutil or mklink commands in Windows 7. In this case, I have used fsutil to create symbolic link under C:/usr/bin folder. The sample command is given below.

C:\usr\bin>fsutil hardlink create perl “C:/Strawberry/perl/bin/perl.exe”

The FSUTIL utility requires that you have administrative privileges.

C:\usr\bin>

When we run the fsutil, we may get the “The FSUTIL utility requires that you have administrative privileges.” error message. This is because, we did not run the Command prompt as “Run As Administrator” option. So run the command prompt using “Run As Administrator” option and run the same command shown as below.

Correct procedure:

C:\usr\bin>fsutil hardlink create perl “C:/Strawberry/perl/bin/perl.exe”
Hardlink created for C:\usr\bin\perl <<===>> C:\Strawberry\perl\bin\perl.exe

C:\usr\bin>dir
Volume in drive C has no label.
Volume Serial Number is 3CDE-B3B8

Directory of C:\usr\bin

01/21/2012 08:38 PM <DIR> .
01/21/2012 08:38 PM <DIR> ..
05/15/2011 04:04 PM 14,336 perl
1 File(s) 14,336 bytes
2 Dir(s) 31,941,332,992 bytes free

C:\usr\bin>

Now, we got the “perl” executable under the C:/usr/bin folder. So using this option we can have #!/usr/bin/perl as the shebang line in Windows 7 OS also. Use the following link to know more about fsutil command.

http://technet.microsoft.com/en-us/library/cc788097(WS.10).aspx

In Windows XP:

Windows XP system don’t have the fsutil command to create hard links. So we need to use the NTFS Link software utility to create the hard links. NTFS Link utility can be downloaded from the sourceforge.net website. The website Link is given below.

http://sourceforge.net/projects/ntfslinkext/

Last Updated :
05 Oct, 2021

Prerequisite: Introduction to Perl

 
Before, we start with the process of Downloading and Installing Perl on Windows operating system, we must have first-hand knowledge of What the Perl Language is and what it actually does?. Perl is a general-purpose, high level interpreted and dynamic programming language. Perl was originally developed for text processing like extracting the required information from a specified text file and for converting the text file into a different form. Perl supports both procedural and Object-Oriented programming. Perl is a lot similar to C syntactically and is easy for the users who have knowledge of C, C++.

Perl programs can be written on any plain text editor like notepad, notepad++, or anything of that sort. One can also use an online IDE for writing Perl codes or can even install one on their system to make it more feasible to write these codes. Using an IDE makes it easier to write Perl codes because IDEs provides a lot of features like intuitive code editor, debugger, compiler, etc.
To begin with, writing Perl Codes and performing various intriguing and useful operations, one must have Perl installed on their System. This can be done by following the step by step instructions provided below:

Checking for a preinstalled Perl version:

Before we begin with the installation of Perl, it is good to check if it might be already installed on your system, because many software applications nowadays require Perl to perform their operations, hence a version of Perl might be included in the software’s installation package and hence there is no need to redownload and install the Perl when it already exists.
To check if your device is preinstalled with Perl or not, just go to the Command line(For Windows, search for cmd in the Run dialog( + R).
Now run the following command:

perl -v

If Perl is already installed, it will generate a message with all the details of the Perl’s version available, otherwise if Perl is not installed then an error will arise stating Bad command or file name

Downloading and Installing Perl:

Downloading Perl:
Before starting with the installation process, you need to download it. For that, all versions of Perl for Windows are available on perl.org

Download the Perl and follow the further instructions for installation of Perl.

Beginning with the Installation:

After completing the installation process, any IDE or text editor can be used to write Perl Codes and Run them on the IDE or the Command prompt with the use of command:

perl file_name.pl

Here’s a sample Program to begin with the use of Perl Programming:
Let’s consider a simple Hello World Program.

use strict; 

use warnings; 

print("Hello World\n"); 

Output:

Hello-World-Program

In Writing a PHP polyglot, I wrote about the Unix ‘shebang’ mechanism. Any script, being shell script, Perl, PHP or any other language, will be started by the shell; and any script can have a very special line in the beginning, that looks e.g. like this:

#! /usr/bin/perl -w

The #! means so much as: ‘use the following command to run this script’. And then the name of the program to use follows. Now the shell knows what to do with this program, and does that instead of trying to interpret the file itself.

Indeed, the rest of the article was about an amusing way to get something like that to work; read it if you want to know weird stuff about batch language.

A more general approach

Although writing a polyglot is fun, it hardly counts as a good solution. There is a much easier solution in Windows, via so-called ‘file type associations’. It can be done from your command line, and goes something like this: first, you tell Windows that a file ending in .pl is a Perl file:

c:\> assoc .pl=Perl

And then you explain what Windows should do with a file of that new type:

c:\> ftype Perl="C:\Perl\bin\perl.exe" "%1" %*

And now you run your Perl script:

c:\> pi.pl
3.14159265358979

There you go. Windows will remember from now on what to do with Perl files, and you don’t need any special code. Exactly the same thing could be done for PHP files, or for any other file type your system supports.

(What strikes me as odd is that assoc is used to tell the system what file type a file with a certain extension is, and ftype then associates this file type to a command. But that’s probably just me.)

This might actually be better than a shebang

There is an interesting advantage to this mechanism above the polyglot way, or even the shebang concept: these are systems settings. That means that on a correctly configured system, it will always work. In the PHP polyglot, the full path name to PHP was specified, because it is not on your path by default. In a shebang, it is actually required that a full path is used. But what if your system doesn’t have PHP in /usr/bin/php, but in /home/dnicolaas/myownphp/php? Then the script doesn’t work and needs editing. Of course it could work if you could specify a command without a full path, but changing all version of Unix is outside the scope of this article.

A clever workaround?

The problem was recognised by Unix experts, and in some scripts you will see this instead:

#! /usr/bin/env perl -w

Here, the full path to perl isn’t specifed. So what is /usr/bin/env? It is not a program that knows how to interpret a language specified in its second parameter;  it just runs all its arguments as a command in a copy of the environment. In other words, it runs

perl -w

in a new shell. And since this new environment will just look up perl in the path, this gets around the requirement that perl gets specified with a full path. It exchanges it for the requirement that /usr/bin/env exists, of course, but that usually does. Except that on some systems it doesn’t, so this workaround is not perfect.

… or maybe not?

There are two things that are not so nice about the file type associations: they require your to give your scripts a unique extension, and they even require you to type that extension when you want to run the program. When I want to know the value of Ï€, I don’t need to know which programming language I used; I’d rather type

pi

than

pi.pl

Another disadvantage is that if I get a script from a Unix user (or even a whole collection of scripts,) I have to rename them before I can use them. The first problem can be solved in Windows by setting the PATHEXT variable:

set PATHEXT=%PATHEXT%;.pl

This turns .pl into a so-called executable extension; Windows now knows that files with the .pl extension can be typed on the command line without the extension, much like files ending in .com, .exe and .bat.

The second problem cannot be solved so easily. Renaming files acquired from somewhere else is never nice: it is work, it requires thinking when upgrading to a new version, and it might break installers. Is there anything we can do about that?

Running extensionless scripts

So let’s assume a Unix programmer gave us a little Perl script that reads like this:

#! /usr/bin/perl
use Math::Trig;

print "The trip around a planet with radius $ARGV[0] is ";
print pi * 2 * $ARGV[0];
print "\n";

It’s called circ, and you can invoke it as follows:

$ circ 6371
The trip around a planet with radius 6371 is 40030.1735920411

At least, that is what your Unix friend said. But all you get is this:

c:\> circ 6371
'circ' is not recognized as an internal or external command,
operable program or batch file.

But couldn’t we use ftype and assoc to associate the empty extension?

c:\> assoc .=Perl
.=Perl

c:\> ftype Perl="C:\Perl\bin\perl.exe" "%1" %*
Perl="C:\Perl\bin\perl.exe" "%1" %*

c:\> circ 6371
'circ' is not recognized as an internal or external command,
operable program or batch file.

Why didn’t this work? Because we didn’t specify the empty extension as an executable extension. Windows faithfully adds all executable extensions to ‘circ’ to make it run, but it doesn’t add the empty extension if you don’t tell it to:

c:\> set PATHEXT=%PATHEXT%;.
c:\> circ 6371
The trip around a planet with radius 6371 is 40030.1735920411

So there we are! Now we can run any Perl script that we get from our Unix friends!

Another extensionless script

So let’s assume a Unix programmer gave us a little PHP script that reads like this:

#! /usr/bin/php
<?php
print "The volume of a planet with radius $argv[1] is ";
print pi() * 4 * pow($argv[1],3) / 3;
print "\n";
?>

And it’s called volu, and meant to be run like this:

$ volu 6371
The volume of a planet with radius 6371 is 1.08320691685E+012

Ouch. That won’t work, will it? Since we associated the empty extension with Perl, Windows will insist on running Perl on this script. And it isn’t Perl.

Perl tries, I must admit. But Perl doesn’t know we’re running on Windows, so Perl tries to run the program specified in the shebang line:

c:\> volu 6371
Can't exec /usr/bin/php at volu line 1.

But hey! That may be the solution. What if we associated the empty extension with a program that reads shebang lines, and runs the Windows equivalent of the command specified in the shebang line?

A Windows shebang

So let’s try this:

c:\> assoc .=shebangfile
.=shebangfile

c:\> ftype shebangfile=shebang.bat "%1" %*
shebangfile=shebang.bat "%1" %*

c:\> set PATHEXT=%PATHEXT%;.

Now we have arranged that every extensionless file will be run with shebang.bat, so this:

c:\> volu 6371

will turn into this:

shebang.bat c:\volu 6371

Now the only thing we need to do is create shebang.bat, and put it in your path (your c:\WINDOWS directory is fine.) What shebang.bat does is exactly the same as what the shell on Unix does: it reads the first line of the file, extracts the command from it, and runs it with the whole command line as arguments. Since we don’t want to know where on Windows Perl or PHP are installed, and we don’t mind being a little better than the original shebang solution, shebang.bat just extract the last bit (the command name) from the path and runs that, effectively assuming it’s on your path. So the above command translates to:

php c:\volu 6371

which works like a charm.

Here’s shebang.bat:

@echo off
rem shebang.bat - Unix shell behaviour from windows.
rem Use
rem     assoc .=shebangfile
rem     ftype shebangfile=shebang.bat "%1" %*
rem     set pathext=%pathext%;.
rem and put this somewhere in your path.
rem Author: Dion Nicolaas <dion@nicolaas.net>
rem http://whitescreen.nicolaas.net/programming/windows-shebangs
rem 
rem Get the first line of the file
set /p line=<%1
rem Remove all quotes from the string, they shouldn't be there anyway
set line=%line:"=%
rem turn each part of the path into a quoted string, separated by spaces
set line="%line:/=" "%"
rem set first to the first part ("#!"), last to the last part (e.g. perl -w)
for %%i in (%line%) do call :firstlast %%i
rem if it was a shebang line, set command accordingly, else use "type"
set command=type
if "%first%"=="#!" set command=%last%
if "%first%"=="#! " set command=%last%
rem Run command on the command line
%command% %*
goto :EOF
:firstlast
rem Get first and last token, unquote in the process (which will also strip 
rem spaces). In Unix scripts 'perl -w' is much more likely than "the language
rem processor" (long file name with spaces)
if "%first%"=="" set first=%~1
set last=%~1

There’s nothing very special about this batch file. It’s just that batch language is not very good at text processing.

There is one special case that wasn’t mentioned before: What if a file doesn’t have a shebang line? On Unix, a script without a shebang line is assumed to be a shell script, but only if it’s permissions show it is an executable file. On Windows that would be a bit dangerous, because we just designated every extensionless file executable: that includes README and TODO and other files that are probably just text. So if a file doesn’t start with ‘#!’, we just run it through ‘type’, which effectively displays it on the screen.

Epilogue

One more thing: changing PATHEXT from your command line is temporarily. If you want this to work permanently, you need to edit your environment differently: Click ‘Start’ / ‘Control Panel’ / Double-click ‘System’ / ‘Advanced’ tab / button ‘Environment Variables’. Then find ‘PATHEXT’ in the ‘System Variables’, ‘Edit’ it, click a few OKs and open a new CMD window. Your old, already open windows will NOT magically get the new version of PATHEXT, but all your new ones will.

This version of shebang.bat only support shebang lines like this:

#!/usr/local/bin/php
#! /usr/local/bin/perl -w

When run with a file that starts with

#! /usr/bin/env perl

it will fail:

'env' is not recognized as an internal or external command,
operable program or batch file.

But env.bat is very easy to implement on Windows. Just store it somewhere on your path as well:

@%*

This looks like cursing, but it actually means: run %* (all parameters to this batch), but don’t echo it (the @ suppresses output.) This might not catch all subtleties of the Unix ‘env’, but for our purpose it will do just fine.

Key Takeaways

  • To test Perl, create a simple script that prints ‘Hello World’ to see if it works.
  • On Windows, run the ‘Hello World’ script in the command prompt to test your Perl installation.
  • On Unix/Linux, copy the script to your home directory and set permissions to test Perl.

In order to test our fresh installation of Perl, we’ll need a simple Perl program. The first thing most new programmers learn is how to make the script say ‘Hello World’. Let’s look at a simple Perl script that does just that.

 #!/usr/bin/perl
print "Hello World.\n";

The first line is there to tell the computer where the Perl interpreter is located. Perl is an interpreted language, which means that rather than compiling our programs, we use the Perl interpreter to run them. This first line is usually #!/usr/bin/perl or #!/usr/local/bin/perl, but depends on how Perl was installed on your system.

The second line tells the Perl interpreter to print the words ‘Hello World.‘ followed by a newline (a carriage return). If our Perl installation is working correctly, then when we run the program, we should see the following output:

 Hello World.

Testing your Perl installation is different depending on the type of system you are using, but we’ll take a look at the two most common situations:

  1. Testing Perl on Windows (ActivePerl)
  2. Testing Perl on *nix Systems

The first thing you’ll want to do is make sure you’ve followed the ActivePerl Installation tutorial and installed ActivePerl and the Perl Package Manager on your machine. Next, create a folder on your C: drive to store your scripts in — for the sake of the tutorial, we’ll call this folder perlscripts. Copy the ‘Hello World’ program into C:\perlscripts\ and make sure the filename is hello.pl.

Getting a Windows Command Prompt

Now we need to get to a Windows command prompt. Do this by clicking on the Start menu and selecting the item Run…. This will pop up the run screen that contains the Open: line. From here, just type cmd into the Open: field and press the Enter key. This will open (yet another) window which is our Windows command prompt. You should see something like this:

 Microsoft Windows XP [Version 5.1.2600] (C) Copyright 1985-2001 Microsoft Corp. C:\Documents and Settings\perlguide\Desktop>

We need to change to the directory (cd) that contains our Perl scripts by typing in the following command:

 cd c:\perlscripts

That should make our prompt reflect the change in the path like so:

 C:\perlscripts>

Now that we’re in the same directory as the script, we can run it simply by typing its name at the command prompt:

 hello.pl

If Perl is installed and running correctly, it should output the phrase ‘Hello World.’, and then return you to the Windows command prompt.

An alternate method of testing your Perl installation is by running the interpreter itself with the -v flag:

 perl -v

If the Perl interpreter is working correctly, this should output quite a bit of information, including the current version of Perl you are running.

Testing Your Installation

If you are using a school or work Unix / Linux server, chances are Perl is already installed and running — when in doubt, just ask your system administrator or technical staff. There are a few ways we can test our installation, but first, you will need to complete two preliminary steps.​

First, you must copy your ‘Hello World’ program to your home directory. This is usually accomplished via FTP. 

Once your script has been copied to your server, you will need to get to a shell prompt on the machine, usually via SSH. When you have reached the command prompt, you can change into your home directory by typing the following command:

 cd ~

Once there, testing your Perl installation is very similar to testing on a windows system with one extra step. In order to execute the program, you must first tell the operating system that the file is OK to execute. This is done by setting the permissions on the script so that anyone can execute it. You can do this by using the chmod command:

 chmod 755 hello.pl

Once you’ve set the permissions, you can then execute the script by simply typing its name.

 hello.pl

If that doesn’t work, you might not have your home directory in your current path. As long as you are in the same directory as the script, you can tell the operating system to run the program (in the current directory) like so:

 ./hello.pl

If Perl is installed and running correctly, it should output the phrase ‘Hello World.’, and then return you to the Windows command prompt.

An alternate method of testing your Perl installation is by running the interpreter itself with the -v flag:

 perl -v

If the Perl interpreter is working correctly, this should output quite a bit of information, including the current version of Perl you are running.

Понравилась статья? Поделить с друзьями:
0 0 голоса
Рейтинг статьи
Подписаться
Уведомить о
guest

0 комментариев
Старые
Новые Популярные
Межтекстовые Отзывы
Посмотреть все комментарии
  • Система архивации данных windows server служба
  • Как отключить все защиты windows 10
  • Ошибка 0x800f0984 windows 10 как исправить
  • Как открыть веб камеру на windows 10
  • Microsoft sql server 2012 windows server 2012 r2