How to use Pearl within a HTML based on Mason

I would like to use perl within my HTML file to get some informations from my Database. Apparently I am not able to get things working since the error messages are confusing me a lot. Could someone show me how I am able to call the information of my Database with perl ?
1 answer

The Mason approach is based on MVC, therefore you need to define the database connection within your Base.mc as follows:

<%init>
my $conn = DATABASE_NAME::DBI->conn();
my $sconn = DATABASE_NAME::DBI->session_conn();


Afterwards e.g. in your index.mi you need to define another init section in which you can execute your actual query to gather informations from your Database:

<%init>
my $dbh = Ws21::DBI->dbh();
my $sth;
my @articles;
if (!defined $.search) {
$sth = $dbh->prepare("SELECT SOME QUERY FROM DATABASE_NAME");
$sth->execute();
}

Then you are able to call this information with perl from within your html code section like this:

% for my $article (@articles) {
...
% }

For this call it is very important that you start the perl section with % at the very beginning of the line. If it will be started with leading spaces or tabs, it will throw an exception.

Comments

Thanks for this solution, since I thought Perl cannot be run in HTML and in most cases this is misunderstood with just embedding the Perl code in HTML as:

some header lines...

Perl script.

Parinaz Momeni ... - Tue, 12/21/2021 - 23:46 :::

You can embed perl in html as you can do it with php. I already worked with perl and mason and the database connection should work as explained

Nino Ziegelwanger - Wed, 12/22/2021 - 10:20 :::