http://www.perlmonks.org?node_id=1223577


in reply to Perl variables embedded in CSS in a CSS file not resolving correctly when the file is linked in

What you want is to use one of the many templating systems. See Template for a selection of those.

If you're hell-bent on rolling your own, just to keep the precious Perl variables, my suggestion is to declare them explicitly in a hash and to replace them with a tiny loop:

my %template_variables = ( 'navytext' => $navytext, # ... ); my $css = <<'CSS'; #tabmenu { color: #$navytext; } CSS sub fill_template { my( $str, $vars ) = @_; $str =~ s!\$(\w+)!$template{ $1 } || '$' . $1!ge; $str }; print fill_template( $css , \%template_variables );
  • Comment on Re: Perl variables embedded in CSS in a CSS file not resolving correctly when the file is linked in
  • Download Code

Replies are listed 'Best First'.
Re^2: Perl variables embedded in CSS in a CSS file not resolving correctly when the file is linked in
by craigt (Acolyte) on Oct 05, 2018 at 14:54 UTC

    Thanks for the suggestion Corion.