For those of you that read the IRC Log Of Horror I posted last night,
or are just familiar with MakeMaker, you see a lot of this sort of
thing:

    my(@m);
    push @m,
qq[POD2HTML_EXE = $pod2html_exe\n],
qq[POD2HTML = \$(PERL) -we "use File::Basename; use File::Path qw(mkpath); 
%m=\@ARGV;for (keys %m){" \\\n],
q[-e "next if -e $$m{$$_} && -M $$m{$$_} < -M $$_ && -M $$m{$$_} < -M '],
 $self->{MAKEFILE}, q[';" \\
-e "print qq(Htmlifying $$m{$$_}\n);" \\
-e "$$dir = dirname($$m{$$_}); mkpath($$dir) unless -d $$dir;" \\
-e "system(qq[$$^X ].q["-I$(PERL_ARCHLIB)" "-I$(PERL_LIB)" $(POD2HTML_EXE) 
].qq[$$_>$$m{$$_}])==0 or warn qq(Couldn\\047t install $$m{$$_}\n);" \\
-e "chmod(oct($(PERM_RW))), $$m{$$_} or warn qq(chmod $(PERM_RW) $$m{$$_}: $$!\n);}"
];

    return join('', @m);

Nor normally you'd take the first big mess and put it into a here-doc,
but the problem is the mixed string quotings.

If you squint at it long enough you'll find that there's only two
variables in there.  $pod2html_exe and $self->{MAKEFILE}.  So here's a
simple solution:

    my $m = sprintf <<'MAKE_TEXT', $pod2html_exe, $self->{MAKEFILE};
POD2HTML_EXE = %s
POD2HTML = \$(PERLRUN) -we "use File::Basename; use File::Path qw(mkpath);" \\
-e "%%m=@ARGV;while (($p,$h) = each %%m){" \\
-e "  next if -e $$h && -M $$h < -M $$p && -M $$h < -M '%s';" \\
-e "  print qq(Htmlifying $$h\n);" \\
-e "  $$dir = dirname($$h); mkpath($$dir) unless -d $$dir;" \\
-e "  system(q[$(PERLRUN) $(POD2HTML_EXE) ].qq[$$p > $$h])==0 " \\
-e "    or warn qq(Couldn\\047t install $$h\n);" \\
-e "  chmod(oct($(PERM_RW))), $$h " \\
-e "    or warn qq(chmod $(PERM_RW) $$h: $$!\n);" \\
-e "}"
MAKE_TEXT

because there are so few interpolated variables, and so few hashes
used inside, you can get away with sprintf + a here-doc.  The result
is something that looks a lot more like the final result:

I also cleaned up the formatting.  Even though the program is a series
of -e's, its now layed out more like normal Perl code.

The $$m{$$_}'s and $$_ have been eliminated in favor of named
arguments which, while short, at least hint at what they hold ($p for
POD, $h for HTML).  And $$p is much easier to read than $$m{$$_}.

The "$^X -I$(PERL_ARCHLIB) -I$(PERL_LIB)..." is replaced by the
$(PERLRUN) macro which figures that out for you.

Finally, I noticed that MM_Win32->htmlifypods is exactly the same as
MM_Unix->htmlifypods except the Unix one uses -e '...' and the Win32
one uses -e "...".  Given how silly it is to override a method just
because of a quote, I'll just fix up the MM_Unix one to be cross
platform and throw out the MM_Win32 duplicate.

I suspect there's a lot more of this sort of thing floating around.


-- 

Michael G. Schwern   <[EMAIL PROTECTED]>    http://www.pobox.com/~schwern/
Perl Quality Assurance      <[EMAIL PROTECTED]>         Kwalitee Is Job One
Death?  Its like being on holiday with a group of Germans.

Reply via email to