#!/usr/bin/perl use strict; use warnings; # use #! from first argument which is a file: my ($file) = grep -f $_, @ARGV; # alternative: use #! from last argument: # my $file = $ARGV[-1]; open my $F, "<", $file or die "Error reading $file: $!"; exec $1, @ARGV if <$F> =~ /^#!(\S+)/; exit 1;