Examples of simple Perl programs

Examples adapted from
Computer Science & Perl Programming: Best of the Perl Journal
Edited by Jon Orwant
O'Reilly, 2003
Chapter 7: What is Truth, by Nathan Torkington

  1. #!/usr/bin/perl -w
    print $x
  2. 	
    #!/usr/bin/perl -w
    print $x 
    Note: blank line at top of file
  3. #!/usr/bin/perl -w
    $x = 7;
    print $x;
  4. #!/usr/bin/perl -w
    $x = 7;
    print "$x\n";
  5. #!/usr/bin/perl -w
    if (defined $x) {
    print "x has value $x\n";
    } else {
    print "x is undefined\n";
    }