#!/usr/bin/perl use strict; use warnings; use Cwd 'abs_path'; sub in_dir { $_ = shift; m|(.*)/.*$|; ## we take advantage of greediness $1 || "/"; } my $self = abs_path $0; my $stripped = in_dir in_dir $self; print "$self -> $stripped\n"; __OUTPUT__ $ # there's a copy in my home and other in / $ ./a.pl /home/hue/a.pl -> /home $ /a.pl /a.pl -> / $ cd / $ ./a.pl /a.pl -> /