Last updated on December 28th, 2022 at 09:25 am

In this tutorial we will see how to insert HTML within a perl script. This example below is just showcasing how a variable defined in perl can be called within an HTML page itself.

#!/usr/local/bin/perl -wT
use CGI;
my $query = CGI->new();
my $perl_var = "Load Me Inside HTML";
print <<"STARTHTML";
<html> <head><title>YOUR PERL VARIABLE</title></head> <body> <h3>Your perl variable is -> $perl_var</h3> <p></p> <a href="http://127.0.0.1">Click here to go back</a> </body>
STARTHTML

We are just simple adding a variable named my $perl_var = “Load Me Inside HTML”;

Make a note of the STARTHTML variable I have added. You are free to add any according to your wish. Just make sure you add that at the end of the HTML script as shown above.

This << symbol is known as here-doc. It grabs everything from the immediate line up until an end marker. This is presented to the standard input. The end marker line is controlled by the text following theĀ << (in our case it is STARTHTML )

More details on here-doc can be found here.

Command Line Output

~# ./test_html.pl
<html> <head><title>YOUR PERL VARIABLE</title></head> <body> <h3>Your perl variable is -> Load Me Inside HTML</h3> <p></p> <a href="http://127.0.0.1">Click here to go back</a> </body>
~#

Note: Since this is command line output it is just a bunch of HTML but when the same page is loaded in a browser you will be able to see the page itself like the screenshot below

Last but not the least, make sure to update the perl interpreter location in the script according to your configuration (/usr/local/bin/perl)

Leave a Reply

Your email address will not be published. Required fields are marked *