Computer Sciences Department logo

CS 368-1 (2010 Summer) — Day 5 Code Sample

These are the code samples that we built in class.

Hello, World! in a Subroutine

#!/usr/bin/perl

use strict;
use warnings;

sub say_hello {
    my $name = shift;
    $name .= "!";
    print "Hello, $name\n";
}

say_hello("world");

my $name = <STDIN>;
chomp($name);
say_hello($name);
print "original name: '$name'\n";

Slurp (Read In) a File

#!/usr/bin/perl

use strict;
use warnings;

sub slurp {
    my $filename = shift;
    open(INPUT, $filename) or die "help!: $!\n";
    my @contents = <INPUT>;
    close(INPUT);
    return @contents;
}

my @file_contents = slurp('day5b.pl');
print "number of lines: " . scalar(@file_contents) . "\n";