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.
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