1 package HTML::String::Overload;
4 use HTML::String::Value;
5 use B::Hooks::EndOfScope;
9 my ($class, @opts) = @_;
10 overload::constant q => sub {
11 HTML::String::Value->new($_[1], @opts);
13 on_scope_end { __PACKAGE__->unimport };
17 overload::remove_constant('q');
26 HTML::String::Overload - Use constant overloading with L<HTML::String>
30 use HTML::String::Overload;
32 my $html = '<h1>'; # marked as HTML
34 no HTML::String::Overload;
36 my $text = '<h1>'; # not touched
39 use HTML::String::Overload;
41 '<h1>'; # marked as HTML
44 my $text = '<h1>'; # not touched
48 This module installs a constant overload for strings - see
49 L<overload/Overloading constants> in overload.pm's docs for how that works.
51 On import, we both set up the overload, and use L<B::Hooks::EndOfScope> to
52 register a callback that will remove it again at the end of the block; you
53 can remove it earlier by unimporting the module using C<no>.
57 Due to a perl bug (L<https://rt.perl.org/rt3/Ticket/Display.html?id=49594>),
58 you can't use backslash escapes in a string to be marked as HTML, so
60 use HTML::String::Overload;
62 my $html = "<h1>\n<div>foo</div>\n</h1>";
64 will not be marked as HTML - instead you need to write
66 my $html = '<h1>'."\n".'<div>foo</div>'."\n".'</h1>';
68 which is annoying, so consider just using L<HTML::String> and doing
70 my $html = html("<h1>\n<div>foo</div>\n</h1>");
74 The import method we provide does actually take extra options for constructing
75 your L<HTML::String::Value> objects but I'm not yet convinced that's a correct
76 public API, so use that feature at your own risk (the only example of this is
77 in L<HTML::String::TT::Directive>, which is definitely not user serviceable).
81 See L<HTML::String> for authors.
83 =head1 COPYRIGHT AND LICENSE
85 See L<HTML::String> for the copyright and license.