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

Perl | print operator

The print function is one of the most commonly used functions in Perl, allowing you to output data to the console or a file. This tutorial will walk you through the basics of using the print operator in Perl.

Basic Usage

  1. Simple Print: To print a string or variable to the console:

    print "Hello, World!";
    
  2. Printing Variables: You can print variables by simply placing them inside the print statement:

    my $name = "Alice";
    print "Hello, $name!";
    
  3. Printing Multiple Items: print can handle multiple items separated by commas:

    my $name = "Alice";
    my $age = 30;
    print "Name:", $name, " Age:", $age;
    

Special Characters

  1. Newline (\n): To print a newline, you can use the \n character:

    print "This is the first line.\nThis is the second line.";
    
  2. Tab (\t): To print a tab space:

    print "Name:\tAlice";
    

Printing to a File

  1. File Handles: To print to a file, you'll need to use a file handle:

    open(my $fh, '>', 'file.txt') or die "Could not open file 'file.txt' $!";
    print $fh "Writing this to the file.";
    close($fh);
    

    In the above code:

    • We open file.txt for writing (> means write mode).
    • We then print to the file using the $fh file handle.
    • Finally, we close the file handle.
  2. File Handle with Select: You can set a file handle as the default for print:

    open(my $fh, '>', 'file.txt') or die "Could not open file 'file.txt' $!";
    select $fh;
    print "This will go to the file.";
    select STDOUT;  # Switch back to standard output
    close($fh);
    

Formatting

While print itself doesn't offer formatting like printf in C, Perl does have printf and sprintf functions for formatted output. For instance:

my $name = "Alice";
my $age = 30;
printf "Name: %s, Age: %d", $name, $age;

Combining with Other Functions

print can be combined with other functions to produce dynamic output:

  1. Array Elements:

    my @fruits = ("apple", "banana", "cherry");
    print "I love $fruits[1]s!";
    

    This will print: I love bananas!

  2. Expressions:

    my $result = 5 + 5;
    print "The result is $result";
    

Conclusion

The print function is a fundamental tool in Perl for outputting data. While it offers a straightforward mechanism for printing to the console and files, combining it with other functions and features in Perl provides powerful capabilities for displaying data in various ways.

    print "Hello, World!\n";  # Outputs: Hello, World!
    
      open my $file_handle, '>', 'output.txt' or die "Cannot open file: $!";
      print $file_handle "Writing to file\n";
      close $file_handle;
      
      print "Also printing to STDOUT\n";
      
        my $name = "John";
        my $age = 25;
        printf "Name: %-10s Age: %d\n", $name, $age;  # Outputs: Name: John       Age: 25
        
          my $first_name = "John";
          my $last_name = "Doe";
          print "Full name: " . $first_name . " " . $last_name . "\n";  # Outputs: Full name: John Doe
          
            my $variable = "Hello";
            print $variable, " World!\n";  # Outputs: Hello World!
            
            print "The answer is ", 42, "!\n";  # Outputs: The answer is 42!
            
              print "This will not end with a newline";
              print "This will be on the same line\n";