Beginner’s Guide: How to Execute Perl Scripts

Okay, here’s a comprehensive article on executing Perl scripts, designed for beginners, with a word count approaching 5000 words. This article is structured to be easily digestible and covers a wide range of scenarios and best practices.

Beginner’s Guide: How to Execute Perl Scripts

Table of Contents

  1. Introduction to Perl and Scripting

    • What is Perl?
    • What is a Script?
    • Why Use Perl?
    • Interpreted vs. Compiled Languages
  2. Setting Up Your Perl Environment

    • Checking for Existing Perl Installation
      • Windows
      • macOS
      • Linux
    • Installing Perl
      • Windows (Strawberry Perl, ActivePerl)
      • macOS (Homebrew, MacPorts)
      • Linux (Package Managers – apt, yum, dnf, pacman)
    • Verifying Installation
  3. Creating Your First Perl Script

    • Choosing a Text Editor
    • Writing the “Hello, World!” Script
    • Saving the Script (File Extensions: .pl)
    • Understanding the Shebang Line (#!/usr/bin/perl or #!/usr/bin/env perl)
  4. Executing Perl Scripts: The Basics

    • Method 1: Using the perl Command
      • Navigating to the Script’s Directory (Command Prompt/Terminal)
      • Running the Script: perl script_name.pl
      • Understanding Standard Output (STDOUT)
    • Method 2: Making the Script Executable (Unix-like Systems)
      • Adding Execute Permissions: chmod +x script_name.pl
      • Running the Script Directly: ./script_name.pl
      • The Importance of the Shebang Line in Executable Scripts
    • Method 3: Using a Perl IDE (Integrated Development Environment)
      • Examples: Padre, Komodo Edit, VS Code with Perl extensions
      • Advantages of using an IDE
  5. Understanding Command-Line Arguments

    • Accessing Arguments with @ARGV
    • Example: Processing Multiple Files
    • Using shift to Process Arguments
    • Error Handling with Command-Line Arguments
  6. Input and Output

    • Standard Input (STDIN)
      • Reading from the Keyboard: <STDIN>
      • Reading from Files using Redirection: perl script.pl < input.txt
      • Piping Data to a Script: cat input.txt | perl script.pl
    • Standard Output (STDOUT)
      • Printing to the Console: print and printf
      • Redirecting Output to a File: perl script.pl > output.txt
      • Appending Output to a File: perl script.pl >> output.txt
    • Standard Error (STDERR)
      • Printing Error Messages: warn and die
      • Redirecting Error Output: perl script.pl 2> error.txt
      • Combining Standard Output and Error Redirection
  7. Working with Files

    • Opening Files: open(my $fh, '<', 'input.txt')
    • Reading from Files: while (my $line = <$fh>) { ... }
    • Writing to Files: open(my $fh, '>', 'output.txt') and print $fh "Data\n";
    • Appending to Files: open(my $fh, '>>', 'output.txt')
    • Closing Files: close($fh);
    • Error Handling with File Operations (using or die)
    • Using File Test Operators (-e, -f, -d, -r, -w, -x, etc.)
  8. Modules and Libraries

    • What are Perl Modules?
    • Using Modules: use ModuleName;
    • Finding Modules: CPAN (Comprehensive Perl Archive Network)
    • Installing Modules: cpan ModuleName (or cpanm ModuleName)
    • Example: Using File::Basename to Extract Filenames
  9. ** Running Perl One-Liners**

  10. What is Perl One-Liner
  11. -e: Execute code from the command line
  12. -n and -p: Implicit loop around input lines.
  13. Example

  14. Debugging Perl Scripts

    • Using print Statements for Debugging
    • The Perl Debugger (perl -d script.pl)
      • Setting Breakpoints (b)
      • Stepping Through Code (n, s)
      • Inspecting Variables (x)
      • Continuing Execution (c)
    • Using use warnings; and use strict;
  15. Best Practices and Common Errors

    • Code Style:
      • Indentation
      • Comments
      • Meaningful Variable Names
    • Common Errors:
      • Syntax Errors (missing semicolons, unmatched parentheses)
      • Undefined Variables (using use strict; to catch these)
      • File I/O Errors (checking return values of open)
      • Incorrect Permissions (for executable scripts)
    • Error Handling:
      • Using eval to Trap Errors
      • Using die to Exit Gracefully
  16. Advanced Execution Scenarios

    • Running Perl Scripts as Cron Jobs (Scheduled Tasks)
    • Embedding Perl in Web Servers (CGI, mod_perl)
    • Using Perl with Databases (DBI module)
    • Calling External Programs from Perl (system, exec, backticks)
  17. Conclusion and Further Learning


1. Introduction to Perl and Scripting

  • What is Perl?

    Perl (Practical Extraction and Report Language) is a high-level, general-purpose, interpreted, dynamic programming language. It’s known for its powerful text processing capabilities, making it particularly well-suited for tasks like log file analysis, data manipulation, system administration, and web development. While its popularity has waned somewhat with the rise of languages like Python, Perl remains a valuable tool with a rich history and a large, active community.

  • What is a Script?

    A script is a sequence of instructions written in a scripting language (like Perl) that tells a computer what to do. Unlike compiled programs (like those written in C++ or Java), scripts are typically interpreted line by line at runtime. This means the Perl interpreter reads your script, translates it into machine-understandable instructions, and executes those instructions immediately.

  • Why Use Perl?

    • Text Processing: Perl excels at manipulating text. Its regular expression engine is incredibly powerful and deeply integrated into the language, making it easy to search, replace, and transform text data.
    • System Administration: Perl is often used for automating system administration tasks, such as managing files, processes, and users.
    • Rapid Prototyping: Because Perl is interpreted, you can quickly write and test code without a lengthy compilation process. This makes it ideal for prototyping ideas.
    • Cross-Platform Compatibility: Perl runs on a wide variety of operating systems, including Windows, macOS, Linux, and many others.
    • Large Community and Extensive Libraries (CPAN): Perl has a large and active community, and a vast repository of reusable code modules called CPAN (Comprehensive Perl Archive Network). This means you can often find pre-built solutions to common problems.
  • Interpreted vs. Compiled Languages

    • Interpreted Languages (like Perl, Python, Ruby): The source code is executed directly by an interpreter. This makes development faster (no separate compilation step) and often more portable, but interpreted languages can sometimes be slower than compiled languages.
    • Compiled Languages (like C, C++, Java): The source code is translated into machine code (binary executable) by a compiler before it can be run. This typically results in faster execution speeds, but the compilation process adds an extra step to development, and compiled programs may be less portable (often needing to be recompiled for different operating systems or architectures).

2. Setting Up Your Perl Environment

Before you can run Perl scripts, you need to have Perl installed on your system. Here’s how to check for an existing installation and how to install Perl if it’s not already present:

  • Checking for Existing Perl Installation

    Open a command prompt (Windows) or terminal (macOS/Linux) and type the following command:

    bash
    perl -v

    This command tells Perl to print its version information. If Perl is installed, you’ll see output similar to this:

    This is perl 5, version 32, subversion 1 (v5.32.1) built for x86_64-linux-gnu-thread-multi
    ... (copyright information) ...

    If you see an error message like “perl: command not found” (Linux/macOS) or “‘perl’ is not recognized as an internal or external command” (Windows), Perl is likely not installed or not in your system’s PATH.

    • Windows:

      1. Command Prompt: Press Win + R, type cmd, and press Enter.
      2. Type perl -v and press Enter.
    • macOS:

      1. Terminal: Open Launchpad, search for “Terminal”, and click it. Alternatively, use Spotlight Search (Command + Space) and type “Terminal”.
      2. Type perl -v and press Enter.
    • Linux:

      1. Terminal: The method to open a terminal varies depending on your Linux distribution (e.g., Ctrl+Alt+T on Ubuntu, or search for “Terminal” in your applications menu).
      2. Type perl -v and press Enter.
  • Installing Perl

    If Perl is not installed, you’ll need to install it. The installation process varies depending on your operating system.

    • Windows (Strawberry Perl, ActivePerl)

      • Strawberry Perl: This is generally the recommended distribution for Windows. It’s a portable, self-contained Perl environment.

        1. Download: Go to http://strawberryperl.com/ and download the appropriate installer (usually the 64-bit version).
        2. Run the Installer: Double-click the downloaded .msi file and follow the on-screen instructions. It’s usually best to accept the default settings. Make sure the option to add Perl to your PATH is selected.
        3. Verify: After installation, open a new command prompt and type perl -v to verify.
      • ActivePerl: This is another popular Perl distribution for Windows, but it’s generally less recommended than Strawberry Perl these days due to licensing and potential complexities.

        1. Download: Visit ActiveState’s website and follow instructions to download.
        2. Run Installer.
    • macOS (Homebrew, MacPorts)

      macOS usually comes with a system Perl, but it’s often an older version. It’s best to install a newer version using a package manager like Homebrew or MacPorts.

      • Homebrew (Recommended):

        1. Install Homebrew (if you don’t have it): Open a terminal and run:
          bash
          /bin/bash -c "$(curl -fsSL https://raw.githubusercontent.com/Homebrew/install/HEAD/install.sh)"
        2. Install Perl:
          bash
          brew install perl
        3. Verify: Open a new terminal and type perl -v.
      • MacPorts:

        1. Install MacPorts (if you don’t have it): Follow the instructions on the MacPorts website (https://www.macports.org/).
        2. Install Perl:
          bash
          sudo port install perl5
        3. Verify: Open a new terminal and type perl -v.
    • Linux (Package Managers – apt, yum, dnf, pacman)

      Most Linux distributions include Perl in their default repositories. You can use your distribution’s package manager to install it.

      • Debian/Ubuntu (apt):
        bash
        sudo apt update
        sudo apt install perl

      • Red Hat/CentOS/Fedora (yum or dnf):

        • Older systems (yum):
          bash
          sudo yum install perl
        • Newer systems (dnf):
          bash
          sudo dnf install perl
      • Arch Linux (pacman):
        bash
        sudo pacman -S perl

      • Verify:
        After installing, open a new terminal and type perl -v.

  • Verifying Installation

    After installing Perl (or if you believe it was already installed), always verify the installation by opening a new command prompt or terminal and running perl -v. This ensures that the Perl interpreter is correctly installed and accessible in your system’s PATH. The PATH is an environment variable that tells your operating system where to look for executable programs.

3. Creating Your First Perl Script

  • Choosing a Text Editor

    You can write Perl scripts using any plain text editor. However, using a text editor that provides syntax highlighting and other features for programming is highly recommended. Here are some popular options:

    • Notepad++ (Windows): A free and powerful text editor with syntax highlighting for many languages, including Perl.
    • Sublime Text (Windows, macOS, Linux): A popular, cross-platform text editor with a wide range of plugins.
    • Visual Studio Code (VS Code) (Windows, macOS, Linux): A free and highly extensible code editor from Microsoft. You’ll want to install a Perl extension for the best experience.
    • Atom (Windows, macOS, Linux): Another free, open-source, and highly customizable text editor.
    • Vim/Neovim (Windows, macOS, Linux): Powerful, command-line-based text editors favored by many experienced developers. They have a steeper learning curve but are extremely efficient once mastered.
    • Emacs (Windows, macOS, Linux): Another powerful, highly customizable text editor with a long history.
    • TextMate (macOS): A popular text editor specifically for macOS.
  • Writing the “Hello, World!” Script

    Let’s create a simple “Hello, World!” script to get started. Open your chosen text editor and type the following code:

    “`perl

    !/usr/bin/perl

    print “Hello, World!\n”;
    “`

  • Saving the Script (File Extensions: .pl)

    Save the file with a .pl extension. This is the conventional extension for Perl scripts. For example, you could save the file as hello.pl. The location where you save the file is important, as you’ll need to navigate to that directory in your terminal to run the script. It’s good practice to create a dedicated directory for your Perl projects.

  • Understanding the Shebang Line (#!/usr/bin/perl or #!/usr/bin/env perl)

    The first line of the script, #!/usr/bin/perl, is called the “shebang” line. It’s a special instruction for Unix-like operating systems (including macOS and Linux) that tells the system which interpreter to use to execute the script.

    • #!/usr/bin/perl: This specifies the absolute path to the Perl interpreter. This works if your Perl interpreter is installed in /usr/bin. However, the exact location of Perl might vary on different systems.

    • #!/usr/bin/env perl: This is a more portable way to specify the interpreter. The env command searches the user’s PATH for the perl executable and uses the first one it finds. This is generally the preferred approach as it’s more likely to work correctly across different systems, even if Perl is installed in a non-standard location.

    • Windows: The shebang line is generally ignored on Windows when you run a script using the perl command. However, it’s still good practice to include it for portability, and it can be relevant if you use certain tools or techniques that rely on it.

4. Executing Perl Scripts: The Basics

Now that you have a Perl script, let’s explore the different ways to execute it.

  • Method 1: Using the perl Command

    This is the most common and straightforward way to run a Perl script, and it works on all platforms (Windows, macOS, Linux).

    • Navigating to the Script’s Directory (Command Prompt/Terminal)

      Before you can run the script, you need to use the command prompt (Windows) or terminal (macOS/Linux) to navigate to the directory where you saved the script file (hello.pl in our example).

      • cd (change directory) command: Use the cd command to change directories.

        • cd path/to/your/script (replace path/to/your/script with the actual path)
        • Example: If your script is in a folder called perl_scripts on your Desktop, you might use:
          • Windows: cd C:\Users\YourUsername\Desktop\perl_scripts
          • macOS/Linux: cd /Users/YourUsername/Desktop/perl_scripts (or cd ~/Desktop/perl_scripts using ~ as a shortcut for your home directory)
      • dir (Windows) or ls (macOS/Linux) command: You can use the dir (Windows) or ls (macOS/Linux) command to list the files and directories in the current directory. This can help you verify that you’re in the correct location.

    • Running the Script: perl script_name.pl

      Once you’re in the correct directory, you can run the script using the perl command followed by the name of your script file:

      bash
      perl hello.pl

      This command tells the Perl interpreter to execute the code in hello.pl.

    • Understanding Standard Output (STDOUT)

      The output of the script (in this case, “Hello, World!\n”) will be printed to the console (your command prompt or terminal). This is called “standard output” or STDOUT. The \n in the print statement creates a newline character, moving the cursor to the next line after printing the text.

  • Method 2: Making the Script Executable (Unix-like Systems)

    On Unix-like systems (macOS and Linux), you can make a Perl script directly executable, so you don’t have to type perl every time you want to run it.

    • Adding Execute Permissions: chmod +x script_name.pl

      The chmod command is used to change the permissions of a file. The +x option adds execute permission.

      bash
      chmod +x hello.pl

    • Running the Script Directly: ./script_name.pl

      After adding execute permission, you can run the script directly by typing ./ followed by the script name:

      bash
      ./hello.pl

      The ./ is important. It tells the shell to look for the executable in the current directory. Without it, the shell might search your PATH, and if there’s another program with the same name, it might run that instead.

    • The Importance of the Shebang Line in Executable Scripts

      The shebang line (#!/usr/bin/perl or #!/usr/bin/env perl) is essential for executable scripts. When you run ./hello.pl, the operating system uses the shebang line to determine which interpreter to use. Without the shebang line, the system wouldn’t know it’s a Perl script.

  • Method 3: Using a Perl IDE (Integrated Development Environment)
    An IDE can simplify Perl development by providing features like code completion, debugging tools, and a built-in way to run scripts.

    • Examples:

      • Padre: A Perl-specific IDE.
      • Komodo Edit: A multi-language IDE with good Perl support.
      • VS Code with Perl extensions: Very popular due to customization options.
    • Advantages of using an IDE:

      • Code Completion: The IDE suggests code as you type, reducing errors and speeding up development.
      • Syntax Highlighting: Makes code easier to read.
      • Debugging Tools: Help you find and fix errors in your code.
      • Project Management: Helps you organize larger projects.
      • Integrated Execution: You can usually run your scripts directly from within the IDE, without having to switch to a separate terminal window.

    Typically, you’ll open your Perl script file in the IDE, and there will be a “Run” button or menu option that executes the script. The output will often appear in a dedicated output pane within the IDE.

5. Understanding Command-Line Arguments

Command-line arguments are values that you provide to a script when you run it from the command line. They allow you to make your scripts more flexible and reusable by passing in different data or options each time you run them.

  • Accessing Arguments with @ARGV

    In Perl, command-line arguments are stored in a special array called @ARGV. The first argument is $ARGV[0], the second is $ARGV[1], and so on.

    “`perl

    !/usr/bin/perl

    print “The first argument is: $ARGV[0]\n”;
    print “The second argument is: $ARGV[1]\n”;
    print “All arguments: @ARGV\n”;

    my $num_arguments = @ARGV;
    print “Number of arguments: $num_arguments\n”;
    “`

    Save this script as arguments.pl and run it with some arguments:

    bash
    perl arguments.pl hello world 123

    The output will be:

    The first argument is: hello
    The second argument is: world
    All arguments: hello world 123
    Number of arguments: 3

  • Example: Processing Multiple Files

    A common use of command-line arguments is to pass filenames to a script.

    “`perl

    !/usr/bin/perl

    foreach my $filename (@ARGV) {
    print “Processing file: $filename\n”;
    # Here you would add code to open and process the file
    }
    “`

    You could run this script like this:

    bash
    perl process_files.pl file1.txt file2.txt file3.txt

  • Using shift to Process Arguments

    The shift function removes and returns the first element of an array. It’s often used in a loop to process command-line arguments one at a time.

    “`perl

    !/usr/bin/perl

    while (my $arg = shift @ARGV) {
    print “Processing argument: $arg\n”;
    }
    “`

  • Error Handling with Command-Line Arguments

    It’s important to check if the expected number of arguments has been provided and to handle cases where they are missing or invalid.

    “`perl

    !/usr/bin/perl

    if (@ARGV < 2) {
    die “Usage: $0 \n”; # $0 is the script name
    }

    my $input_file = $ARGV[0];
    my $output_file = $ARGV[1];

    … rest of your code …

    “`

    This script checks if at least two arguments are provided. If not, it prints a usage message to standard error (using die) and exits the script.

6. Input and Output

Perl scripts often need to interact with the outside world by reading input and producing output. Perl uses the concepts of standard input (STDIN), standard output (STDOUT), and standard error (STDERR) for this.

  • Standard Input (STDIN)

    • Reading from the Keyboard: <STDIN>

      The <STDIN> operator reads a line of input from the standard input stream. When you run a script interactively, standard input is typically your keyboard.

      “`perl

      !/usr/bin/perl

      print “Enter your name: “;
      my $name = ;
      chomp($name); # Remove the trailing newline character

      print “Hello, $name!\n”;
      “`
      In this example, when the line ‘my $name = ‘ is reached, the code will pause and wait for keyboard input.

    • Reading from Files using Redirection: perl script.pl < input.txt

      You can redirect the standard input of a script to come from a file instead of the keyboard using the < operator on the command line.

      Create a file named input.txt with some text in it:

      This is line 1.
      This is line 2.

      Then create a Perl script read_input.pl:

      “`perl

      !/usr/bin/perl

      while (my $line = ) {
      print “Read: $line”;
      }
      “`

      Run the script with input redirection:

      bash
      perl read_input.pl < input.txt

      The script will read each line from input.txt and print it to the console.

    • Piping Data to a Script: cat input.txt | perl script.pl

      You can also use pipes (|) to send the output of one command to the standard input of another command. This is a powerful way to chain commands together.

      bash
      cat input.txt | perl read_input.pl

      This command uses cat to output the contents of input.txt, and then pipes that output to the standard input of read_input.pl. The result is the same as using input redirection.

  • Standard Output (STDOUT)

    • Printing to the Console: print and printf

      The print function is the most common way to print output to the console (standard output).
      printf allows for formatted output, similar to C’s printf.

      “`perl

      !/usr/bin/perl

      print “This is some text.\n”;
      print “This is “, “more “, “text.\n”; # print can take multiple arguments

      my $number = 42;
      printf “The answer is: %d\n”, $number; # Formatted output
      “`

    • Redirecting Output to a File: perl script.pl > output.txt

      You can redirect the standard output of a script to a file using the > operator. This will overwrite the contents of the file if it already exists.

      “`perl

      !/usr/bin/perl

      print “This will go into the file.\n”;
      “`

      Run the script with output redirection:

      bash
      perl output_to_file.pl > output.txt

      The text “This will go into the file.\n” will be written to output.txt.

    • Appending Output to a File: perl script.pl >> output.txt

      To append output to a file (add to the end without overwriting), use the >> operator.

      bash
      perl output_to_file.pl >> output.txt

      This will add another line to output.txt.

  • Standard Error (STDERR)

    • Printing Error Messages: warn and die

      Standard error (STDERR) is a separate output stream used for error messages and warnings. It’s important to use STDERR for errors so that they can be distinguished from normal output.

      • warn: Prints a warning message to STDERR but doesn’t stop the script.
      • die: Prints an error message to STDERR and terminates the script.

      “`perl

      !/usr/bin/perl

      warn “This is a warning.\n”;

      if (@ARGV == 0) {
      die “Error: No arguments provided.\n”;
      }

      print “This will only be printed if arguments are provided.\n”;
      “`

    • Redirecting Error Output: perl script.pl 2> error.txt

      You can redirect STDERR to a file using the 2> operator.

      bash
      perl error_example.pl 2> error.txt

      If you run without arguments, the error is written to a file.

    • Combining Standard Output and Error Redirection

      You can redirect both STDOUT and STDERR at the same time.

      bash
      perl script.pl > output.txt 2> error.txt

      This is the most common way to handle errors.

      You can also redirect STDERR to the same place as STDOUT using 2>&1:

      bash
      perl script.pl > output.txt 2>&1

      This sends both standard output and standard error to output.txt.

7. Working with Files

Perl provides powerful tools for reading from and writing to files.

  • Opening Files: open(my $fh, '<', 'input.txt')

    The open function is used to open a file. It takes three main arguments:

    1. Filehandle: A variable that will represent the opened file (conventionally, filehandles are often named $fh, but you can use any valid variable name).
    2. Mode: A string that specifies how the file should be opened:
      • <: Read mode (open for reading).
      • >: Write mode (open for writing, overwrites existing file).
      • >>: Append mode (open for writing, appends to existing file).
      • +<: Read and Write mode.
      • There are other modes, but these are the most common.
    3. Filename: The name of the file to open (can be a path).

    perl
    my $filename = 'input.txt';
    open(my $fh, '<', $filename) or die "Cannot open '$filename': $!";

  • Reading from Files: while (my $line = <$fh>) { ... }

    Once a file is opened in read mode, you can read its contents line by line using the <$fh> operator inside a while loop.

    perl
    while (my $line = <$fh>) {
    chomp($line); # Remove the trailing newline
    print "Line: $line\n";
    }

    This loop reads each line from the filehandle $fh, assigns it to the variable $line, and then executes the code inside the loop.

  • Writing to Files: open(my $fh, '>', 'output.txt') and print $fh "Data\n";

    To write to a file, open it in write mode (>) or append mode (>>). Then, use the print function, but instead of printing to STDOUT, you print to the filehandle.

    perl
    open(my $output_fh, '>', 'output.txt') or die "Cannot open 'output.txt': $!";
    print $output_fh "This is some data.\n";
    print $output_fh "This is more data.\n";
    close($output_fh);

  • Appending to Files: open(my $fh, '>>', 'output.txt')

    Appending is similar to writing, but it adds to the end of the file instead of overwriting it.

    perl
    open(my $append_fh, '>>', 'output.txt') or die "Cannot open 'output.txt': $!";
    print $append_fh "This is appended data.\n";
    close($append_fh);

  • Closing Files: close($fh);

    It’s important to close files after you’re finished with them using the close function. This releases the file resources and ensures that any buffered data is written to disk.

    perl
    close($fh);

  • Error Handling with File Operations (using or die)

    File operations can fail for various reasons (file not found, permission denied, disk full, etc.). It’s crucial to handle these errors gracefully. The most common way to do this is to use the or die construct after the open function:

    perl
    open(my $fh, '<', 'nonexistent_file.txt') or die "Cannot open 'nonexistent_file.txt': $!";

    This code attempts to open nonexistent_file.txt. If the open function fails, it returns a false value. The or operator then executes the die function, which prints an error message to STDERR and terminates the script. The $! variable contains the system error message, providing more information about why the open failed.

  • Using File Test Operators (-e, -f, -d, -r, -w, -x, etc.)

    Perl provides a set of file test operators that allow you to check various properties of files before you try to open or manipulate them. This can help you avoid errors.

    • -e: File exists.
    • -f: File is a regular file.
    • -d: File is a directory.
    • -r: File is readable.
    • -w: File is writable.
    • -x: File is executable.
    • -z: File has zero size.
    • -s: File has non-zero size (returns size in bytes).

    “`perl
    my $filename = “myfile.txt”;

    if (-e $filename) {
    print “$filename exists.\n”;

    if (-r $filename) {
        print "$filename is readable.\n";
    } else {
        print "$filename is not readable.\n";
    }
    
    if (-f $filename) {
       print "$filename is a regular file.\n";
    }
    

    } else {
    print “$filename does not exist.\n”;
    }
    “`

8. Modules and Libraries

  • What are Perl Modules?

    Perl modules are reusable pieces of code that provide specific functionalities. They are like libraries in other programming languages. Modules save you from having to write code from scratch for common tasks.

  • Using Modules: use ModuleName;

    To use a module, you use the use

Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top