background preloader

Command line (perl -e)

Facebook Twitter

Windows et UNIX (cmd et batch) Command-Line Options. Perl on the command line. While most of the Perl tutorial deals with scripts saved in a file, we'll also see a couple of examples of one-liners.

Perl on the command line

Even if you are using Padre or some other IDE that would let you run your script from the editor itself, it is very important to familiarize yourself with the command line (or shell) and be able to use perl from there. If you are using Linux, open a terminal window. You should see a prompt, probably ending with a $ sign.

If you are using Windows open a command window: Click on Start -> Run -> type in "cmd" -> ENTER You will see the black window of CMD with a prompt that probably looks like this: c:\> Perl version Type in perl -v. C:\> perl -v This is perl 5, version 12, subversion 3 (v5.12.3) built for MSWin32-x86-multi-thread Copyright 1987-2010, Larry Wall Perl may be copied only under the terms of either the Artistic License or the GNU General Public License, which may be found in the Perl 5 source kit. Printing a number.

Perl Training Australia - Perl on the command line (part 1) [ Perl tips index ] [ Subscribe to Perl tips ] Occasionally we find a task that only ever needs to be done once.

Perl Training Australia - Perl on the command line (part 1)

Perhaps we need to change a file so that all strings A002 become B005, or we want to find out how many times a particular IP address accesses the web-server today. In these cases, rather than use a throw-away script, we may be able to write our script directly onto the command line. Keep in mind as you do this though, that sometimes throw-away scripts turn into programs that become essential to the business.

If you think you're ever likely to run this same program again, or if it is non-trivial, write it into a program, comment it, use strict and warnings, as well as the appropriate modules and keep it. Using the execute switch (-e) to convert from epoch-time Let's say that you've got a timestamp in seconds from the epoch; the number of seconds since midnight, 1st January, 1970 GMT. The qq{\n} represents a newline character, which you may more commonly see written as "\n". Note. Command-line Options.

Perl has a wide range of command-line options or switches that you can use.

Command-line Options

The options are also called switches because they can turn on or turn off different behaviors. A thorough knowledge of the command line switches will enable you to create short one-time programs to perform odd little tasks. For example, the -e option lets you specify a line of code directly on the command-line instead of creating a script file. You use the -l option to change the line endings in a text file. How Are the Options Specified? The most frequent way to specify a command-line options is on the command line. Perl -I/usr/~john/include You can combine options with no arguments with the following switch. Run. Perl [ -sTtuUWX ] [ -hv ] [ -V[:configvar] ] [ -cw ] [ -d[t][:debugger] ] [ -D[number/list] ] [ -pna ] [ -Fpattern ] [ -l[octal] ] [ -0[octal/hexadecimal] ] [ -Idir ] [ -m[-]module ] [ -M[-]'module...' ] [ -f ] [ -C [number/list] ] [ -S ] [ -x[dir] ] [ -i[extension] ] [ [-e|-E] 'command' ] [ -- ] [ programfile ] [ argument ]...


The normal way to run a Perl program is by making it directly executable, or else by passing the name of the source file as an argument on the command line. (An interactive Perl environment is also possible--see perldebug for details on how to do that.) Upon startup, Perl looks for your program in one of the following places: With methods 2 and 3, Perl starts parsing the input file from the beginning, unless you've specified a -x switch, in which case it scans for the first line starting with #! And containing the word "perl", and starts there instead. The #! Because historically some operating systems silently chopped off kernel interpretation of the #!

Command-Line Options. Uncommon* but Useful Perl Command Line Options for One-liners. Parsing your script's command line. This tutorial describes how to deal with information passed in on the command line.

Parsing your script's command line

Not the switches passed to the Perl interpreter, but the switches and file names that are passed to your script. Let's assume that you have written your first hello world script, which looks something like this: #! /usr/local/bin/perl -w print "Hello, world\n"; Instead of greeting the world, you would like to greet whatever is passed in on the command line. . #! Commands (GNU/Linux UNIX) Error : Can't do inplace edit without backup.