Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl: the Markov chain saw
 
PerlMonks  

How To Use Bootstrap Code in Perl Script?

by glennpm (Novice)
on Feb 15, 2016 at 22:06 UTC ( [id://1155303]=perlquestion: print w/replies, xml ) Need Help??

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

I'm using Bootstrap code that is embedded in a template and I got it to work in a standalone simple .HTM file, but I'm trying to "embed" the following lines of code in a perl script so that I can make the text boxes, buttons and tables mobile enabled. Does any guru know how to put these lines in an existing perl script and maybe give me a sample of what will work? I tried a few things that didn't work.

THE FOLLOWING WORKS in HTML; NEED TO KNOW HOW TO PUT SAME LINES IN A PERL SCRIPT

<head> <meta name="viewport" content="width=device-width, initial-scale=1, +maximum-scale=1"> <link type="text/css" rel="stylesheet" href="../hideo/bootstrap/css/ +bootstrap.min.css"> <link href='http://fonts.googleapis.com/css?family=Arvo' rel='styles +heet' type='text/css'> <link type="text/css" rel="stylesheet" href="../hideo/font-icons/cus +tom-icons/css/custom-icons.css"> <link type="text/css" rel="stylesheet" href="../hideo/css/layout.css +"> <link type="text/css" id="colors" rel="stylesheet" href="../hideo/cs +s/colors.css"> <link type="text/css" rel="stylesheet" href="../hideo/css/custom.css +"> <!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/tr +unk/html5.js"></script><![endif]--> <script src="hideo/js/modernizr-2.6.1.min.js"></script> <link rel="shortcut icon" href="../hideo/images/favicon.ico"> <link rel="apple-touch-icon" href="../hideo/images/apple-touch-icon. +png"> <link rel="apple-touch-icon" sizes="72x72" href="../hideo/images/app +le-touch-icon-72x72.png"> <link rel="apple-touch-icon" sizes="114x114" href="../hideo/images/a +pple-touch-icon-114x114.png"> <link rel="apple-touch-icon" sizes="144x144" href="../hideo/images/a +pple-touch-icon-144x144.png"> </head>
<body> <!-- RIGHT AFTER <body> TAG --> <script type="text/javascript" src="../hideo/js-plugin/jquery/jquery +-1.10.2.min.js"></script> <script type="text/javascript" src="../hideo/JQuery/jquery.cycle.all +.2.74.js"></script> <!-- JUST BEFORE </body> TAG --> <script type="text/javascript" src="../hideo/bootstrap/js/bootstrap. +js"></script> <script type="text/javascript" src="../hideo/js/custom.js"></script> </body>

Replies are listed 'Best First'.
Re: How To Use Bootstrap Code in Perl Script?
by tangent (Parson) on Feb 16, 2016 at 02:37 UTC
    You might want to try out using templates for this kind of work as it allows you to separate the HTML from your code. Have a look at HTML::Template and Template Toolkit - the former is probably easier to learn but the latter is very powerful. Using HTML::Template you would create a template file like this:

    example.tmpl

    <html> <head> <meta name="viewport" content="width=device-width, initial-scale=1 +, maximum-scale=1"> <link type="text/css" rel="stylesheet" href="../hideo/bootstrap/cs +s/bootstrap.min.css"> <!-- etc. fill in rest of head elements --> </head> <body> <!-- RIGHT AFTER <body> TAG --> <script type="text/javascript" src="../hideo/js-plugin/jquery/jque +ry-1.10.2.min.js"></script> <script type="text/javascript" src="../hideo/JQuery/jquery.cycle.a +ll.2.74.js"></script> <!-- this will be replaced --> <TMPL_VAR NAME=CONTENT> <!-- JUST BEFORE </body> TAG --> <script type="text/javascript" src="../hideo/bootstrap/js/bootstra +p.js"></script> <script type="text/javascript" src="../hideo/js/custom.js"></scrip +t> </body> </html>
    Then in your perl script you pass the file to the template engine, and populate it with the other content you want on the page:
    use HTML::Template; my $file = '/path/to/example.tmpl'; my $template = HTML::Template->new( filename => $file ); # do all the work to create the content my $content = "Start of Content"; # ... # pass content to the template $template->param( CONTENT => $content ); print "Content-Type: text/html\n\n"; print $template->output;
Re: How To Use Bootstrap Code in Perl Script?
by golux (Chaplain) on Feb 16, 2016 at 02:43 UTC
    Hi glennpm,

    Whatever else you do (and a HEREDOC is perfectly fine for generating the HTML, as is Template Toolkit), the one thing you have to do differently with CGI is print headers, followed by at least a single blank line.

    For example:

    #!/usr/bin/perl use strict; use warnings; # CGI headers (note two newlines, one for a blank line) print "Content-type: text/html\n\n"; # Now anything else you do is HTML ... print "<body style='background:cyan'>\n"; print " <center>\n"; print " <h1>Hello world!</h1>\n"; print " </center>\n"; print "</body>\n";

    BTW, another style I've gotten in the habit of in place of HEREDOCS is to quote a block of text with qq{ ... }, putting colons ':' at the beginning of each line for visual ease of reading, and finally reformat the text to remove the colons and print it out. For example:

    # CGI headers (note two newlines, one for a blank line) print "Content-type: text/html\n\n"; # Another way to format HTML my $text = qq{ :<body style="background:cyan"> : <center> : <h1> Hello world! </1> : </center> :</body> }; output($text); # Subroutines sub output { my ($text) = @_; # Reformat the HTML text (remove leading colons ':') $text =~ s/(^\s+:)|((?<=\n)\s+:)|(\s+$)//g; # And output it print $text; }
    say  substr+lc crypt(qw $i3 SI$),4,5
Re: How To Use Bootstrap Code in Perl Script?
by Hadrianus (Novice) on Feb 16, 2016 at 01:48 UTC
    You could use a 'here document' construct
    #!/usr/bin/perl use strict; use warnings; # --- HERE document construct my $page = <<END_OF_HTML ; <!DOCTYPE html> <html lang="en"> <head> <meta charset="utf-8"> [snip some html] <title>Your title</title> <!-- Bootstrap --> <link href="css/bootstrap.min.css" rel="stylesheet"> </head> <body> <h1>Hello, world!</h1> [snip some html] <script src="js/bootstrap.min.js"></script> </body> </html> END_OF_HTML print $page; print "=" x 50; sub body_generate { my $tmp; $tmp = "<h1>Subroutines or functions can be called too!</h1>"; return $tmp; } my $subject = "Test for embedding variables in here document"; my $page2 = <<END_OF_HTML; <title>$subject</title> <body> @{ [ body_generate ] } <p>For details see 'Perl Cookbook', recipe 1.10 "Interpolating functio +ns and expressions within strings. </p> </body> END_OF_HTML print $page2;

    See http://www.stonehenge.com/merlyn/UnixReview/col12.html for what happens is you use the different types of quotes around the here document marker (END_OF_HTML)

Re: How To Use Bootstrap Code in Perl Script?
by jcb (Parson) on Feb 16, 2016 at 00:53 UTC

    If I recall correctly and your script is a CGI script, all you need is a few here documents in the right places.

    First, you will need to find where the script generates the HTML <head> block and add:

    print <<'EOT'; <meta name="viewport" content="width=device-width, initial-scale=1, +maximum-scale=1"> <link type="text/css" rel="stylesheet" href="../hideo/bootstrap/css/ +bootstrap.min.css"> <link href='http://fonts.googleapis.com/css?family=Arvo' rel='styles +heet' type='text/css'> <link type="text/css" rel="stylesheet" href="../hideo/font-icons/cus +tom-icons/css/custom-icons.css"> <link type="text/css" rel="stylesheet" href="../hideo/css/layout.css +"> <link type="text/css" id="colors" rel="stylesheet" href="../hideo/cs +s/colors.css"> <link type="text/css" rel="stylesheet" href="../hideo/css/custom.css +"> <!--[if lt IE 9]><script src="http://html5shim.googlecode.com/svn/tr +unk/html5.js"></script><![endif]--> <script src="hideo/js/modernizr-2.6.1.min.js"></script> <link rel="shortcut icon" href="../hideo/images/favicon.ico"> <link rel="apple-touch-icon" href="../hideo/images/apple-touch-icon. +png"> <link rel="apple-touch-icon" sizes="72x72" href="../hideo/images/app +le-touch-icon-72x72.png"> <link rel="apple-touch-icon" sizes="114x114" href="../hideo/images/a +pple-touch-icon-114x114.png"> <link rel="apple-touch-icon" sizes="144x144" href="../hideo/images/a +pple-touch-icon-144x144.png"> EOT

    Then you will need to add similar constructs in the appropriate places for the HTML <body> text.

Re: How To Use Bootstrap Code in Perl Script?
by Anonymous Monk on Feb 15, 2016 at 22:25 UTC
Re: How To Use Bootstrap Code in Perl Script?
by NetWallah (Canon) on Feb 16, 2016 at 18:13 UTC
    Just in case you find it usful, I'm offering some code I wrote recently, to interface with Bootstrap. It includes methods to generate panels, buttons, dropdowns, and alerts.

    I don't consider this useful/tested enough to deliver to CPAN, but it works for my uses.

    Yes - I realise it lacks documentation - but I'm hoping the code is clear enough to identify calling protocol.

            "Think of how stupid the average person is, and realize half of them are stupider than that." - George Carlin

Re: How To Use Bootstrap Code in Perl Script?
by poj (Abbot) on Feb 16, 2016 at 13:42 UTC
    put these lines in an existing perl script

    How many lines are there in the existing script ?
    If CGI, does it use an object-oriented style or a function-oriented style ?

    poj

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-04-23 23:22 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found