Me again. I don't think I've ever written this many responses to a single OP. :-)
Use of s///r has been mentioned in more than one post.
I wondered if you knew that you could chain them.
You can also chain y///r and mix them up.
This type of extended construct is valid; although, I can't think of an immediate use for such a beast:
$str =~ s///r =~ y///r =~ s///r =~ tr///r
I saw where you'd written that you don't really have time to learn a completely new system.
The following may be useful in its own right;
however, it may get you halfway to writing a more formal template.
When you do get around to looking at templating systems
— and I do recommend you at least put that on your TODO list —
you'll probably notice the similarities between placeholders like __TOKEN__ here
and those used by templating systems, such as <% TOKEN %>.
In the following code, the main processing, including the s///r chaining, is all at the front;
the (messy) heredocs are written as theredocs and moved out of the way to the end of the code.
$ perl -E '
my @users = (
{ audience => "Management" },
{ audience => "Employee" },
{ audience => "Guest" },
);
generate_report($_) for @users;
sub generate_report {
my ($user) = @_;
say main_template()
=~ s/__AUDIENCE__/$user->{audience}/r
=~ s/__MNGT_REP__/mngt_rep($user)/er
=~ s/__EMP_REP__/emp_rep($user)/er
=~ s/__GUEST_REP__/guest_rep($user)/er;
}
# Theredocs (out of the way)
sub main_template {
<<EOF
<p>
Report for __AUDIENCE__.
</p>
__MNGT_REP____EMP_REP____GUEST_REP__
EOF
}
sub mngt_rep {
my ($viewer) = @_;
return "" unless $viewer->{audience} eq "Management";
return <<EOF
<div>
... Management Report ...
</div>
EOF
}
sub emp_rep {
my ($viewer) = @_;
return "" unless $viewer->{audience} eq "Employee";
return <<EOF
<div>
... Employee Report ...
</div>
EOF
}
sub guest_rep {
my ($viewer) = @_;
return "" unless $viewer->{audience} eq "Guest";
return <<EOF
<div>
... Guest Report ...
</div>
EOF
}
'
When run, that outputs:
<p>
Report for Management.
</p>
<div>
... Management Report ...
</div>
<p>
Report for Employee.
</p>
<div>
... Employee Report ...
</div>
<p>
Report for Guest.
</p>
<div>
... Guest Report ...
</div>
-
Are you posting in the right place? Check out Where do I post X? to know for sure.
-
Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
<code> <a> <b> <big>
<blockquote> <br /> <dd>
<dl> <dt> <em> <font>
<h1> <h2> <h3> <h4>
<h5> <h6> <hr /> <i>
<li> <nbsp> <ol> <p>
<small> <strike> <strong>
<sub> <sup> <table>
<td> <th> <tr> <tt>
<u> <ul>
-
Snippets of code should be wrapped in
<code> tags not
<pre> tags. In fact, <pre>
tags should generally be avoided. If they must
be used, extreme care should be
taken to ensure that their contents do not
have long lines (<70 chars), in order to prevent
horizontal scrolling (and possible janitor
intervention).
-
Want more info? How to link
or How to display code and escape characters
are good places to start.