in reply to removing the last word

You can make use of File::Basename to accomplish your job.

use File::Basename; $dirname= dirname($filepath);

Prasad