If it were me, I would avoid modifying the scalar ref and just return the changed value:
sub _convert_html { my ($self, $filename, $coderef) = @_; my $html = $self->slurp($filename); $html = $coderef->( $html );
However, if you want to change helper arguments, you should know that all helper arguments are passed by reference to Perl ( @_ elements @_ smoothed to subtitle arguments). So your sub converter might look like this:
sub { $_[0] =~ s/<html>/<xml>/ }
But if you really want to work with $_ , for example, in your desired code example, you need to make _convert_html() as follows:
sub _convert_html { my ($self, $filename, $coderef) = @_; my $html = $self->slurp($filename); $coderef->() for $html; $self->save_to_file($filename, $html); }
for is an easy way to correctly localize $_ . You can also do:
sub _convert_html { my ($self, $filename, $coderef) = @_; local $_ = $self->slurp($filename); $coderef->(); $self->save_to_file($filename, $_); }
source share