Zero logo

Perl CLI

Perl scripts are executed in one of two ways; through a web sever where the output is sent to a browser, or through the command-line interface. Main difference between the two methods is the format of new lines. Output to a browser requires the HTML line break <br /> while the output to a command-window requires \n. This page provides a basic introduction to Perl CLI (command line interface).

Overview

Note: It is assumed you have installed one of the perl modules such as ZeroXIII_strawberry_perl_5_22_1_1.exe or ActivePerl

Uniform Server is portable, it does not make changes to your PC. As a consequence the standard command prompt (console window) cannot be used to run Perl CLI code. The path to perl.exe along with other environment variables are undefined. To run Perl CLI code on Uniform Server use the Server Console.

The Server Console button opens a command window (command prompt) with environment variables preset and paths configured for Uniform Server utilities including the Perl interpreter. Apart from cosmetics, it is identical to a standard command prompt.

Perl code is run from the command prompt either directly by typing lines of code or indirectly using code saved in a script file.

Command-window

Running scripts via a command-line is performed using the CLI interpreter perl.exe this program is located in folder C:\UniServerZ\core\perl\bin note UniController automatically tracks the installation and changes this path accordingly. In addition UniController sets several environment variables that are required for portability.

Check functionality:
  • Start UniController by double clicking on file UniController.exe
  • Start a command-window by clicking the Server Console button.

Type the following into the command-window and press enter:

perl.exe -v

Displays Perl version number, see image on right for example.

Note 1: You can clear the command-window by typing CLS
Note 2: For help type perl -h.
Note 3: You can open more than one command-window.

  Perl CLI intro

Perl Command-line - run code directely

The above demonstrates running Perl code directly using the CLI interpreter perl.exe the following example prints hello world:

perl.exe -e "print \"hello world\n\""

The interpreter program takes a number of parameters who’s function is:

perl.exe CLI interpreter program perl.exe If a file extension is not provided Windows will assume .exe hence you can use just perl
-e Parameter -e informs Perl, not to expect a file, next thing on the command-line is actual Perl code
"print \"hello world\n\""   Perl code to run. Note: Windows requires the code to be enclosed within quotes.

Note: If your code contains quotes to avoid problems these must be escaped using a backslash for example:

perl.exe -e "print \"hello world\n\""

Code examples

The following provide examples that you can cut and past into the command-window:

Examples 1:
perl -e "print 'Hello World!'" #Basic print
perl -e "print 'Hello World!'.\"\n\""  #Escape quotes
perl -e "print 'First line'.\"\n\";print 'Second line'"  #Run multi-lines
perl -e "$x = 1;$y = 4;print $x+$y" #Example calulation
perl -e "foreach $key (keys(%ENV)) {printf(\"%-24.24s: $ENV{$key}\n\", $key);}" #Print environment variables
perl -e "print $^X" #Full path to perl.exe location

Perl Command-line - run scripts

Entering code directly is ideal for short scripts, however for scripts containing several lines of code it is preferable to save these to a file and run this file from the Server Console.

The following examples shows how to run Perl scripts

Run basic Perl script:

Create script:

Create a test script shown on the right and save to
root folder UniServerZ with name test1.pl

  test1.pl
print "hello world\n";
Run script:

Run the script by typing the following into a command-window:

perl test1.pl
  Result1
C:\UniServerZ>perl test1.pl
hello world

C:\UniServerZ>

Pass arguments to script:

Create script:

Create a test script shown on the right and save to
root folder UniServerZ with name test2.pl

  test2.pl
print "$_\n" for @ARGV;
print "@ARGV[0]\n";
print "@ARGV[1]\n";
print "@ARGV[2]\n";
Run script:

Run the script by typing the following into a command-window:

perl test2.pl aaa bbb bbb
  • print "$_\n" for @ARGV; - Prints all arguments passed to script
  • @ARGV[0] - Prints first argument passed to script
  • @ARGV[1] - Prints second argument passed to script
  • @ARGV[2] - Prints third argument passed to script
  Result2

C:\UniServerZ>perl test2.pl aaa bbb cccc
aaa
bbb
cccc
aaa
bbb
cccc

C:\UniServerZ>

Environment variables - Read and display:

Create script:

Create a test script shown on the right and save to
root folder UniServerZ with name test3.pl

  test3.pl
foreach $key (keys(%ENV)) {
    printf("%-24.24s: $ENV{$key}\n", $key);
}

print "\n\n";
print "Environment variable PHP_INI_SELECT=".$ENV{"PHP_INI_SELECT"}."\n";
print "Environment variable PHP_SELECT=".$ENV{"PHP_SELECT"};

Run script:

Run the script by typing the following into a command-window:

perl test3.pl

All environment variables are displayed using:

 foreach $key (keys(%ENV)) {
    printf("%-24.24s: $ENV{$key}\n", $key);
} 

Individual environment variables PHP_INI_SELECT and PHP_SELECT are displayed using:

print "Environment variable PHP_INI_SELECT=".$ENV{"PHP_INI_SELECT"}."\n";
print "Environment variable PHP_SELECT=".$ENV{"PHP_SELECT"};
  Result3
PROCESSOR_REVISION      : 3a09
FP_NO_HOST_CHECK        : NO
PROGRAMFILES            : C:\Program Files (x86)
PROGRAMDATA             : C:\ProgramData
WINDIR                  : C:\WINDOWS
AP_SSL_PORT             : 443
SYSTEMDRIVE             : C:
US_ROOTF_WWW            : C:/UniServerZ/www
US_SERVERNAME           : localhost
PHP_INI_SELECT          : php_production.ini
ONLINESERVICES          : Online Services
TMP                     : C:\Users\Mike\AppData\Local\Temp
PROGRAMFILES(X86)       : C:\Program Files (x86)
USERDOMAIN_ROAMINGPROFIL: MPG
---- All variables and the following: ---------

Environment variable PHP_INI_SELECT=php_production.ini
Environment variable PHP_SELECT=php54
C:\UniServerZ>

Passing parameters

You can pass parameters between scripts such as a batch file and Perl for details see page Perl passing parameters.


--oOo--