How can I modify a scalar reference passed to a subroutine reference
Posted
by Mark
on Stack Overflow
See other posts from Stack Overflow
or by Mark
Published on 2010-05-17T23:01:54Z
Indexed on
2010/05/17
23:20 UTC
Read the original article
Hit count: 311
I have a function to convert documents into different formats, which then calls another function based on the type document. It's pretty straight forward for everything aside from HTML documents which require a bit of cleaning up, and that cleaning up is different based on where it's come from. So I had the idea that I could pass a reference to a subroutine to the convert function so the caller has the opportunity to modify the HTML, kinda like so (I'm not at work so this isn't copy-and-pasted):
package Converter;
...
sub convert
{
my ($self, $filename, $coderef) = @_;
if ($filename =~ /html?$/i) {
$self->_convert_html($filename, $coderef);
}
}
sub _convert_html
{
my ($self, $filename, $coderef) = @_;
my $html = $self->slurp($filename);
$coderef->(\$html); #this modifies the html
$self->save_to_file($filename, $html);
}
which is then called by:
Converter->new->convert("./whatever.html", sub { s/<html>/<xml>/i });
I've tried a couple of different things along these lines but I keep on getting 'Use of uninitialized value in substitution (s///)'. Is there any way of doing what I'm trying to do?
Thanks
© Stack Overflow or respective owner