Beefy Boxes and Bandwidth Generously Provided by pair Networks
The stupid question is the question not asked

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 13:56 UTC ( #1223576=perlquestion: print w/replies, xml ) Need Help??

craigt has asked for the wisdom of the Perl Monks concerning the following question:

I'm working on a Windows platform using an Apache server and dHTML - HTML, CSS, JavaScript, Perl/CGI, and a number of APIs. I have a custom parameter file that's read into a CGI module. The parameters in that file allow a user to customize the geography and appearance of the application. The parameters in that file are embedded in the CSS, JavaScript, and Perl code in the CGI module. I define my Perl namespaces at the beginning of the module, with names like $navytext. I then read the custom parameter file into an array. The parameters in the file have the same names as the namespaces I define in the CGI module. I split the comment portion of each parameter line off and eval the parameter value into the CGI module where it is then used downstream in CSS, JavaScript, and Perl. A simplified code example follows.

my $navytext; # This variable appears as the line '$navytext = '000066 +' # Navy blue text.' in the custom parameters file below. . . . my $cpsfnx = $thepdir . 'custom.parms'; if (-e $cpsfnx) { my @cpslnsx = in_shared($cpsfnx); my $cpsln = ''; foreach $cpsln (@cpslnsx) { if (index($cpsln,'#') > 1) { ($cpsln,$tmp) = split ('#',$cpsln); } eval($cpsln); } } . . . <HEAD> <STYLE> #tabmenu { color: #$navytext; }

Notice in the example that the CSS with the Perl namespace $navytext resides right in the Perl module. This has all worked well for some time. Now I would like to remove the CSS from the Perl module, put it in a CSS file, and link the CSS file into the Perl module. When I do this, the Perl variable no longer resolves correctly to the HTML color and the CSS does not work. I have tried to quote the Perl variable in the CSS file in several ways with no luck. The question is can I link an external CSS file with embedded Perl variables into a CGI module so that the Perl variables resolve correctly?

  • Comment on 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: Perl variables embedded in CSS in a CSS file not resolving correctly when the file is linked in
by Corion (Pope) on Oct 05, 2018 at 14:02 UTC

    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 );

      Thanks for the suggestion Corion.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1223576]
Approved by haukex
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (1)
As of 2021-02-25 05:35 GMT
Find Nodes?
    Voting Booth?

    No recent polls found