Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer
 
PerlMonks  

Style in CGI Header

by Anonymous Monk
on Sep 30, 2014 at 03:42 UTC ( [id://1102408]=perlquestion: print w/replies, xml ) Need Help??

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

I have code that prints html header this way with CGI.pm:

-style => {-code=> "$page_style", 'src'=>[$_css_path,$_css_path2]},#.. +.. I just copied that line out...

this is just part of it that goes in this header:
print start_html();

My question is, if I have a page that I want to pull in a different CSS is there a way to put them all in a array instead of the way I did it, where the files are all printed into there so they are all pulled in:
@whatever = ("$_css_path","$_css_path2"); -style => {-code=> "$page_style", 'src'=>@whatever}, #....
Then if I have another path, say in this tag: $_css_path3, how would I add it? push?

Please let me know how to make it work.
Thank you. :)
Ukn

Replies are listed 'Best First'.
Re: Style in CGI Header
by frozenwithjoy (Priest) on Sep 30, 2014 at 04:15 UTC

    Disclaimer: I've never used CGI.pm

    The snippet 'src' => [ $_css_path, $_css_path2 ] is using an anonymous array, whereas 'src' => @whatever is using a normal array. Perhaps it will work if you use an array reference to an anonymous array like this:

    $whatever = [ $_css_path, $_css_path2 ]; -style => { -code => $page_style, src => $whatever }, #....

    For the second part of your question, new paths can be pushed with:

    push @{ $whatever }, $_css_path3;

    Of course, this will need to be done before print start_html();

      So then would this work? I'm in a live environment, so I cannot just test it, it may break if I do it live...

      my @cssFiles = (); push(@cssFiles, {'src' => "$_css_path"}); push(@cssFiles, {'src' => "$_css_path2"}); # Then in the start_html() this line: -style => {-code=> "$page_style", 'src'=>@cssFiles},
      is that how it would work? It does not seem so, because the array has src = and then the -style field has 'src' = ...so seems like I have it wrong.

      Can you tell me how to do that right?

      Thanks,
      Rich

        There are some kinds of changes where you won't know if they work until you try it on a test system... or in this case a live system... this isn't one of them. CGI is (still) in the Perl core, so this one is particularly easy to test locally.

        use CGI ':standard'; my @cssFiles = ("src1"); push @cssFiles, "src2"; print start_html(-style=>{-code=>"code",-src=>\@cssFiles}); __END__ ... <link rel="stylesheet" type="text/css" href="src1" /> <link rel="stylesheet" type="text/css" href="src2" /> <style type="text/css"> <!--/* <![CDATA[ */ code /* ]]> */--> </style> ...

        So that's a yes, frozenwithjoy's suggestion should work, provided you use an array reference (-src=>\@cssFiles, not -src=>@cssFiles).

        If you had a test system, even if it's not an exact mirror of the live system but at least can execute your code, may have saved you the ~20 hours wait on a solution...

        Changing your code to follow my earlier suggestion would give something like this:

        my $cssFiles = []; push @{$cssFiles}, $_css_path; push @{$cssFiles}, $_css_path2; # Then in the start_html() this line: -style => { -code => $page_style, -src => $cssFiles },

        If you wanted to use an array instead of an array ref, you could try this:

        my @cssFiles; push @cssFiles, $_css_path; push @cssFiles, $_css_path2; # Then in the start_html() this line: -style => { -code => $page_style, -src => \@cssFiles },

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://1102408]
Approved by frozenwithjoy
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (5)
As of 2024-03-28 20:37 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found