#!/usr/bin/perl use strict; # https://perlmonks.org/?node_id=11156715 use warnings; use Path::Tiny; sub delete_comments_from_file { path($_)->edit( sub { s/#.*//g } ) for @_; } my $file = '/tmp/foobar.tmp'; path($file)->spew( <slurp; print '-' x 50, "\n"; delete_comments_from_file( $file ); # NOTE print path($file)->slurp; #### one # a comment two no comment three # more comments; -------------------------------------------------- one two no comment three