• Skip to primary navigation
  • Skip to main content
  • Skip to primary sidebar

The Urban Penguin

The Urban Penguin - Linux Training

  • Home
  • About
  • Live Online Courses
  • Shop
  • Programming
    • Master Editing Text Files Using VIM
    • Learn Shell Scripting with BASH
    • PERL Scripting in Linux
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • Learn C Programming using Linux and the Raspberry Pi
    • General Java Tutorials
    • Java 7 OCA Exam 1ZO-803
  • OS Tutorials
    • Red Hat and CentOS Training
      • Red Hat Enterprise Linux System Administration 1 – RH124
      • RHCSA – System Admin 2 – RH134
      • RHCE – EX294 – Automation With Ansible
    • Learning Ubuntu
    • LPI Training
      • LPI Linux Essentials
      • LPIC-1 Linux Administrator
      • LPIC-2 Certified Linux Engineer
      • LPIC-3 Senior Level Certification
        • LPIC-3 Exam 300 : Mixed Environments
        • LPIC-3 Exam 303 : Security
        • LPIC-3 Exam 304 : Virtualization and High Availability
    • Linux Technologies
      • Apache HTTPD Server
      • Learning PHP
      • Learning PUPPET
      • Learning SAMBA
      • Linux File-Systems
      • Monitoring with Nagios Core
      • MYSQL
      • openLDAP Directories on Linux
You are here: Home / Scripting / Perl / Simple PERL Invocation Options

Simple PERL Invocation Options

June 19, 2017 by The Urban Penguin

simple PERL invocation optionsPERL is not the newest of languages and that it most certainly true. However, just because something is not new it does not automatically mean it is depreciated. In this lesson we will look at using PERL and keeping the code simple and clean. To do this we will use simple PERL invocation options. These options can be used directly from the CLI or part of your shebang in a script.

PERL is great choice for scripting as it installed by default on most Linux distros, including minimal installs. Even though it does have a following that think the language is verbose we can show how through the use of simple PERL invocation options we can make the code very light and very simple. Firstly we can check PERL is installed and the version:

$ perl -v

Now lets speak a little computer and say the ubiquitous “Hello World”. We can execute code from the CLI with -e (not required in scripts). The options -w is a good one to use as it prints warnings when we may have made some errors.

ubuntu@alice:~$ perl -w -e 'print "Hello World";'
Hello Worldubuntu@alice:~$

Now the more experienced among you may have predicted this, without adding a new line to print the output and prompt are now on the same line. We would normally add in a newline when using print.

ubuntu@alice:~$ perl -w -e 'print "Hello World\n";'
Hello World
ubuntu@alice:~$

This though makes the code more verbose and cluttered, also, it is likely that we will have more than one print statement so the new line is going to be repeated. Then we don’t add the \n many times. We use simple PERL invocation options to do this for us, and just once. We use -l:

ubuntu@alice:~$ perl -wl -e 'print "Hello World";'
Hello World
ubuntu@alice:~$

Next we see how easy it to process and filter files. Whilst I can write verbose code to open a file, I don’t need to.

ubuntu@alice:~$ perl -wlp -e '' /etc/passwd

The option -p comes into play here where we can print each line from STDIN. This is the file /etc/passwd in this case. Notice the code block for PERL is empty, just two single quotes. If we want to do more than just print the file we can work with the content. Here we only print the line that contains the ubuntu user. We avoid using an if statement for the more simple logical and. We also swap the -p option for -n. We don’t want to print all lines just the lines that we match, so we code this with our own print statement:

ubuntu@alice:~$ perl -wln -e '/ubuntu/ and print;' /etc/passwd
ubuntu:x:1000:1000:Ubuntu:/home/ubuntu:/bin/bash
ubuntu@alice:~$

The video follows, please enjoy.

Share this:

  • Click to share on Twitter (Opens in new window)
  • Click to share on Facebook (Opens in new window)
  • Click to share on LinkedIn (Opens in new window)
  • Click to share on Reddit (Opens in new window)
  • Click to share on Pinterest (Opens in new window)
  • Click to share on Tumblr (Opens in new window)
  • Click to print (Opens in new window)

Filed Under: Perl

Primary Sidebar

Newest Video

The Urban Penguin On Youtube

Categories

Pages

  • About The Urban Penguin
  • Contact Us
  • Shop
    • Basket
    • Checkout
    • My Account
  • LPI Training from The Urban Penguin
    • Live and Pluralsight hosted courses
    • Complete Linux Essentials
    • LPIC-3 Senior Level Certification
      • LPIC-3 Exam 300 : Mixed Environments
      • LPIC-3 Exam 303 : Security
      • LPIC-3 Exam 304 : Virtualization and High Availability
    • LPIC-2 Certified Linux Engineer
    • LPIC-1 Linux Administrator
    • LPI Linux Essentials for Raspberry Pi
    • LPI Linux Essentials
  • Operating System Tutorials
    • Linux Foundation Training
    • Solaris 11 OCA 1ZO-821
    • Learning Ubuntu
    • Learning SUSE
    • Red Hat and CentOS Training
      • RHCE – EX294 – Automation With Ansible
      • RHCSA – System Admin 1 – RH124
      • RHCSA – System Admin 2 – RH134
  • Scripting – the power of repetition!
    • Java 7 OCA Exam 1ZO-803
    • General Java Tutorials
    • Learn C Programming using Linux and the Raspberry Pi
    • Ruby Scripting in Linux
    • Scripting with PowerShell
    • PERL Scripting in Linux
    • Learn Shell Scripting with BASH
    • Master Editing Text Files Using VIM
  • Linux Technologies
    • Learning PUPPET
    • openLDAP Directories on Linux
    • Monitoring with Nagios Core
    • Linux File-Systems
    • Learning SAMBA
    • Apache HTTPD Server
    • Learning PHP
    • MYSQL
  • OpenStack
    • Pluralsight
    • Udemy
    • Raspberry Pi Tutorials
    • Citrix Videos
  • Online Instructor-led Courses
    • Red Hat Enterprise Linux System Administration 1 – RH124
    • SELinux Masterclass
    • Bash Scripting Masterclass
    • Nftables Masterclass

© 2021 The Urban Penguin · All Rights Reserved