Perl Tutorial

Fundamentals

Input and Output

Control Flow

Arrays and Lists

Hash

Scalars

Strings

Object Oriented Programming in Perl

Subroutines

Regular Expressions

File Handling

Context Sensitivity

CGI Programming

Misc

Syntax of a Perl Program

Perl is a versatile and dynamic scripting language that has been popular for text processing, system administration, and web development. Its syntax is unique and allows for more than one way to do things, leading to its motto: "There's more than one way to do it."

In this tutorial, we will take a look at the basic syntax of a Perl program.

1. Shebang Line:

The first line of a Perl script usually starts with a shebang (#!). This tells the system that this file should be executed using Perl.

#!/usr/bin/perl

For more portability across systems, especially if Perl might reside in a different directory, you can use:

#!/usr/bin/env perl

2. Comments:

Comments in Perl start with a # symbol and extend to the end of the line.

# This is a comment
print "Hello, World!\n";  # This is also a comment

3. Statements & Semicolons:

Perl statements end with a semicolon.

print "Hello, World!\n";
print "Perl is fun.\n";

4. Variables:

  • Scalar Variables: Store single values (numbers, strings). They start with a $ sign.

    my $name = "Alice";
    my $age = 30;
    
  • Arrays: Store ordered lists of scalars and start with an @ sign.

    my @fruits = ("apple", "banana", "cherry");
    
  • Hashes (or Associative Arrays): Store key-value pairs and start with a % sign.

    my %data = ("John", 25, "Jane", 30);
    

5. Control Structures:

  • Conditional Statements:

    if ($age > 18) {
        print "Adult\n";
    } elsif ($age == 18) {
        print "Just turned 18!\n";
    } else {
        print "Not an adult.\n";
    }
    
  • Loops:

    # While loop
    while ($count < 10) {
        print $count, "\n";
        $count++;
    }
    
    # For loop
    for my $i (0..9) {
        print $i, "\n";
    }
    

6. Subroutines (Functions):

Declare with sub and then the subroutine name.

sub greet {
    my ($name) = @_;
    print "Hello, $name!\n";
}

greet("Bob");  # Calls the subroutine with "Bob" as argument

7. Modules and Libraries:

You can use external modules or libraries using the use statement.

use strict;  # Helps write better code by enforcing certain good practices
use warnings;  # Enables warning messages

8. Special Variables:

Perl has many built-in special variables. For instance, $_ is the default variable and many built-in functions operate on $_ if no variable is given.

$_ = "Hello";
print if /Hello/;  # Prints "Hello" because $_ contains "Hello"

9. Running a Perl Program:

To run a Perl program named script.pl:

perl script.pl

Summary:

The syntax of Perl is rich and flexible, allowing for concise and expressive programs. Familiarizing yourself with the basics, as described above, provides a foundation for diving deeper into Perl programming and leveraging its full potential.

  1. Perl script structure and organization:

    • Description: Organize Perl scripts with proper structure for clarity and maintainability.
    • Code:
      #!/usr/bin/perl
      use strict;
      use warnings;
      
      # Perl script structure and organization
      sub main {
          print "Hello, Perl!\n";
      }
      
      main();  # Call the main subroutine
      
  2. Perl script execution and entry point:

    • Description: Understand how Perl scripts are executed and define an entry point.
    • Code:
      #!/usr/bin/perl
      use strict;
      use warnings;
      
      # Perl script execution and entry point
      sub main {
          print "Executing main subroutine!\n";
      }
      
      main();  # Entry point for script execution
      
  3. Looping constructs in Perl script:

    • Description: Utilize looping constructs like for and while in Perl for repetitive tasks.
    • Code:
      #!/usr/bin/perl
      use strict;
      use warnings;
      
      # Looping constructs in Perl script
      sub for_loop_example {
          for my $i (1..5) {
              print "Iteration $i\n";
          }
      }
      
      sub while_loop_example {
          my $count = 1;
          while ($count <= 5) {
              print "Count: $count\n";
              $count++;
          }
      }
      
      for_loop_example();
      while_loop_example();