cat, Python, and Perl


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 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="")

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


further reading