cat, Python, and Perl

2011-03-15

I came across a tutorial that showes how to implement a simple cat in Perl. I've modified their example for the sake of simplicity:

sub cat {
    foreach my $filename (@_) {
        open FILE, $filename;
        while (my $line = <FILE>) {
            print $line;
        }
    }
}
cat @ARGV;

Running this script (e.g. perl cat.pl file) will display those files as if you ran cat file.

I decided to see how the Python example would look like:

import sys
def cat(files):
    for filename in files:
        with open(filename) as FILE:
            for line in FILE:
                print(line, end="")
cat(sys.argv[1:])

Running this script (e.g. python3 cat.py file1 file2) will give exactly the same result as above.

notes

further reading