# First three lines of your subroutine, ir() with the prototype removed because it's not useful: sub ir { my $prefix = shift @_; $prefix=~ s/_$//; print $prefix,"\n"; } # A loop to test those first three lines of your code to see if they work: while() { chomp; ir($_); } # Sample input you provided: __DATA__ DCMS_DEMO_ DCMS_DEMO_new_block2_ DCMS_DEMO_new_block2_checklist_tmp_ DCMS_DEMO_new_block2_checklist_tmp_rev1_ #### DCMS_DEMO DCMS_DEMO_new_block2 DCMS_DEMO_new_block2_checklist_tmp DCMS_DEMO_new_block2_checklist_tmp_rev1