2

I am asked to make a form that get the width, unit, direction and a paragraph by using CGI.pmand forms from user. Then I need to apply width, unit and direction to the paragraph by CSS rules.

I was wondering if it is possible to write inline CSS in Perl using CGI.pm.

For example:

<p style=width:"user-width user-unit";text-align:"user-justification">
 paragraph</p> 
3
  • 2
    Its better not to mix View(HTML,CSS) and Controller (your CGI script doing logic) Commented Jun 18, 2013 at 8:05
  • My teacher asked me to do it as a practice Commented Jun 18, 2013 at 8:12
  • 1
    If you have to do it for class, then you have to do it for class, I guess... but you're practicing a bad habit. Using CGI.pm's HTML-generating functions is generally recognized as a Bad Idea these days. Using a templating system instead (such as Template::Toolkit or Xslate) is The Right Way To Do It, at least until something even better comes along. Commented Jun 18, 2013 at 8:58

1 Answer 1

6

Yes it's possible using -style parameter.

Example: print h1({-style=>'Color: red;'},'Welcome to Hell');

You can also create separate CSS file. Below is an example which uses separate stylesheet (style.css) and also inline CSS.

print start_html( -title=>'CGI with Style',
                      -style=>{-src=>'http://www.example.com/style/style.css',
                               -code=>$newStyle}
                     );
    print h1('CGI with Style'),
          p({-class=>'Tip'},
            "Better read the cascading style sheet spec before playing with this!"),
          span({-style=>'color: magenta'},
               "Look Mom, no hands!",
               p(),
               "Whooo wee!"
               );
    print end_html;

Source: CGI.pm docs

Sign up to request clarification or add additional context in comments.

4 Comments

HOW can I associate value which is given from user to css attributes?
Sorry I didn't get you. Give some example.
print p({-style=>'width:$cgi->param('width')'}); I want to use user input to set the width in css
It should work fine, isn't it? Make sure your $cgi is an object of CGI.pm module.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.