Reputation: 549
I have this code in a Mojolicious template:
my $lang = 'de'; # de, en, ...
% $col = 'internet' . '_' . $lang;
%== markdown($rs->$col); # outputs correctly the content of the column internet_de
Is there a way to combine the lines 2 and 3 into something like:
%== markdown($rs->'internet' . '_' . $lang); # syntax error at template ..., near "->'internet'"
Upvotes: 0
Views: 130
Reputation: 39158
Use a ref-deref. This is analogue to the baby cart.
$rs->${\"internet_$lang"}
$rs->${\('internet_' . $lang)}
Upvotes: 3
Reputation: 126722
I don't understand why you are striving to make your code even more concise. It is already hard to read, and golfing it further can only make it worse. In particular, you have a prime example of how comments can be used to make code less readable. And what are those %
and %==
doing there? Your code won't compile with those in place
You can use a scalar variable to provide the method name, but not a general expression
One improvement I would make is to use interpolation instead of string concatenation
my $lang = 'de';
my $method = "internet_$lang";
markdown($rs->$method);
Upvotes: -2
Reputation: 2331
Readabbility is quite an issue, so i would add a helper function to your Module:
sub access_it {
my ($obj, $fun) = @_;
return $obj->$fun
}
after that you should be able to use
%== markdown(access_it($rs,'internet' . '_' . $lang));
Upvotes: 1