#!/usr/bin/perl

use 5.010;

use strict;
use warnings;

my $hard_way = 1;    # Use s/// to do everything in one regexp?

die "Usage: a3-activate.pl <filename>" unless @ARGV;
my $filename = shift;

open my $file, '<', $filename or die "Can't open '$filename': $!\n";

while ( my $sentence = <$file> ) {
    say activate($sentence);
}

close $file;

sub activate {
    my ($sentence) = @_;

    if ($hard_way) {
        # We can do this with just one regexp...
        $sentence =~ s/(.*) was (.*) by (.*)\./$3 $2 $1\./;
    }
    else {
        # ...but it might be easier to understand if we do it the long way.

        if ($sentence =~ /(?<object>.*) was (?<verb>.*) by (?<subject>.*)\./) {
            $sentence = "$+{subject} $+{verb} $+{object}.";
        }
    }

    return $sentence;
}
