Command Line Switches

    This is a UNIX construct, which tells a shell that executes the file directly what program to pass the rest of the input to.

    You can add any of Perl's command line switches to this line, and they will be treated as if they were part of the command line after the switches provided there. That is, if you had a program containing

    1. #!/usr/bin/perl -T

    as its first line, and executed it as

      both the and -T switches are used, but -l is used first. (This can matter in certain circumstances.)

      Perl's command line switches are documented in . Here are some of the most useful.

      perl -T

      Perl allows you to run in a special mode, called "taint" mode. While in taint mode, variables are expected to be sanitized ("untainted") before being used for an "unsafe operation".

      What is unsafe?

      • Running a program
      • Writing a file
      • Making a directory
      • …basically, anything that modifies the system.

      How do you untaint? Use a regular expression to match valid values, and then assign that match to a variable.

      You should have the goal of making your programs taint safe.

      This command-line switch allows you to check the given file for syntax errors. It also runs any code in BEGIN blocks and will check any modules you have used in your program.

      You should check your code's syntax with -c after every change.

      perl -e 'code'

      This command-line switch allows you to run code from the command line, instead of having to write your program to a file and then execute it.

      1. $ perl -e 'print "1\n"'
      2. 1

      This is highly useful for small programs, quick calculations, and in combination with other switches.

      Perl's switch allows you to run a program (usually specified with -e) against every line on standard input. These are equivalent:

      1. $ cat /etc/passwd | perl -e 'while (<>) { if (/^(\w+):/) { print "$1\n"; } }'
      2. root
      3. ...
      4. root
      5. ...

      If you combine the switch, Perl will edit your file in place. So, to convert a bunch of files from DOS to UNIX line endings, you can do this:

      perl -M

      Perl's -M switch allows you to use a module from the command line. There are several modules that prefer to be run this way (such as CPAN and ). It's also a convenient shortcut with -e if you need to include a module:

      1. $ perl -e 'use Data::Dumper; print Dumper( 1 );'
      2. $VAR1 = 1;
      3. $ perl -MData::Dumper -e 'print Dumper( 1 );'
      4. $VAR1 = 1;

      Try to load the module from the command line. The -e1 is just an empty program that exits immediately. If you get an error, the module must not be there:

      1. $ perl -MWWW::Mechanize::JavaScript -e 1
      2. Can't locate WWW/Mechanize/JavaScript.pm in @INC...
      3. BEGIN failed--compilation aborted.

      Returning without an error means it's installed.

      While you're at it, check the version:

      1. $ perl -MWWW::Mechanize -e'print $WWW::Mechanize::VERSION'

      Not all modules have a $VERSION variable, so this may not always work.


      Submit a PR to github.com/petdance/perl101