#!/usr/bin/perl use warnings; use strict; my $file = shift || $0; my $pid = open(FH,"tac $file |") or die "$!\n"; my $count = 0; while (){ #do your line processing here print $count++ .' '. $_; }