How to Embed Unix Commands in Perl

Perl is a computer programming language with extensive facilities for scripting and text manipulation. Perl programs get executed by an interpreter, so variables are dynamically typed. When writing a complex application it is useful to be able to invoke operating system commands so the programmer can concentrate on the core function of his code without having to re-implement from scratch functionality that is already provided by existing (and tested) operating system code. In particular, you can embed operating system commands when your Perl program is running on Unix.

Instructions

    • 1

      Marshal the arguments for the Unix command from the Perl code. The specific way of performing this step depends on the intended function of your Perl code. For example, for a program that creates a new directory under a given point in the filesystem hierarchy, include the following lines in your code:

      #!/usr/local/bin/perl

      #

      $pointInFilesystem = $ARGV[0];

      $nameNewDir = $ARGV[1];

      The first command-line argument to your Perl application is the point where the new directory will get created; the second argument is the name the new directory will have.

    • 2

      Assemble the Unix command into a single string variable. For example, for the directory-creation application, include the following lines in your code:

      $unixCommand = "mkdir $pointInFilesystem"."/".$nameNewDir

      String variable "$unixCommand" contains a legal invocation to Unix's "mkdir" command.

    • 3

      Invoke the Unix command by using Perl's "system" command. For example, for the directory-creation application, include the following line in your code:

      system($unixCommand);

      When "system" gets executed, it will create a new process and instruct it to execute system's argument -- in this case to create a new directory. Your Perl program will resume when the process created by "system" exits. You can invoke any other Unix or shell command using "system."

Related Searches:

References

Comments

You May Also Like

  • Embedded SQL Tutorial

    Embedded SQL is when you combine the computing power of programming language with the database capabilities provided by SQL. Statements written in...

  • How to Remove Duplicates From Array Perl

    Perl and many other programming languages use arrays to store a list of data. However, Perl also supports a specific data structure...

  • How to Embed Perl in HTML

    The convenience of embedding scripting code in HTML documents for web development is undeniable. Having a powerful language like Perl working just...

  • How to Write Portable Perl Code

    Writing portable Perl code has plenty of advantages. One main advantage is that you can write it once and run it on...

  • How to Remove the Perl File Extension

    Perl is a highly versatile scripting language used by programmers, system administrators and designers to create a wide variety of programs for...

  • How to Calculate the Date in Unix

    The Unix date command has been designed to display the system date and time, read directly from the operating system's kernel clock....

  • How to Create Variables in PERL

    As a portable, simple and versatile language, PERL has become popular for working on web-based scripts and programs. Originally developed for UNIX,...

  • How to Change a File Name in Perl

    Perl is an open source programming language available for all operating systems. It can also be used as a command line scripting...

  • MySQL Perl DBI Tutorial

    One of the most powerful features of Perl is its ability to process, parse, manipulate and format text and data, making it...

  • How to Manipulate Images in Perl

    Perl was built for text manipulation, but it also has external libraries for manipulating raster images. A popular library from which you...

Related Ads

Featured