in reply to Re: Creating a random generator
in thread Creating a random generator

I have looked into an gotten installed HTML::Template, which may do the same thing as Template::Toolkit. I still don't know how to make or use databases just yet, but I am going to get to it very soon, since I will more than likely need it before this is all over. I do not have MySQL and a lot of other things that everyone recommends on my server. I am already using use CGI in my scripts.

I am learning Perl by using it to do those projects. It is the best way for me to learn, since the subject matter holds my interest more than print "Hello World!\n\n"; amongst other things. I build scripts to have them torn down by my betters and shown what I did poorly or wrong. I am glad that there are people here who understand that some of us can't learn from the abstract, we need to put into a familiar subject right away to learn.

Thanks for dropping me a line.

Lady Aleena

"An it harm none, do as ye will."

Replies are listed 'Best First'.
Re^3: Creating a random generator
by UnstoppableDrew (Sexton) on Oct 07, 2007 at 03:04 UTC
    So I had some free time, and I haven't written any code for fun in a while, so I decided to put my money where my mouth is. Here's the skeleton of a solution for your proficiency tables. I created a mysql database named 'player', and create one table called 'proficiencies':
    mysql> describe proficiencies; +-------------+-------------+------+-----+---------+-------+ | Field | Type | Null | Key | Default | Extra | +-------------+-------------+------+-----+---------+-------+ | proficiency | varchar(50) | NO | | | | | s | int(11) | YES | | NULL | | | ra | varchar(20) | NO | | | | | cm | int(11) | YES | | NULL | | | class | varchar(30) | YES | | NULL | | | source | varchar(30) | YES | | NULL | | +-------------+-------------+------+-----+---------+-------+
    I scraped your a-c table and turned it into a text file delimited by pipe chars (since there are commas in the data):
    |Acting|1|Cha|0|Bd, Hu| |Adaptation|2|Int|0|Wa 10+, R 11+|HL| |Administration|1|Int|1|Pr|SM|
    I wrote a script to stuff the data into the db:
    #!/usr/bin/perl use strict; use DBI; use DBD::mysql; my $dsn = "DBI:mysql:database=player;"; my $dbh = DBI->connect($dsn, "root", "secretpassword"); open IN, "<table.txt" or die "Error - failed to open file for reading: + $!\n"; while ( <IN> ) { chomp; my ($null,$prof,$s,$ra,$cm,$class,$source) = split /\|/; my $sql =<<SQLDONE ; INSERT INTO proficiencies VALUES ( '$prof', $s, '$ra', $cm, '$class', +'$source' ); SQLDONE print "$sql\n"; my $sth = $dbh->prepare($sql); $sth->execute; } print "finished loading table\n";
    That was probably not the most efficient way to do it, but my dbi book is at work and all this is off the top of my head.

    Script to read the database and dump data into template toolkit template:

    #!/usr/bin/perl use strict; use DBI; use DBD::mysql; use Template; my $tt = Template->new(); my $dsn = "DBI:mysql:database=player;"; my $dbh = DBI->connect($dsn, "root", "secretpassword"); my @items; my $sql = qq|SELECT * from proficiencies ORDER BY proficiency ASC;|; my $sth = $dbh->prepare($sql); $sth->execute; while ( my @row = $sth->fetchrow_array() ) { push @items, \@row; } my $vars = { items => \@items }; print "Content-type: text/html\n\n"; $tt->process("prof.tmpl", $vars) or die $tt->error();
    Template toolkit template to display the data:
    <html> <head><title>Proficiencies</title> <body> <table border="1" cellspacing="2" cellpadding="2"> <tr> <th>Proficiency</th> <th>S</th> <th>RA</th> <th>CM</th> <th>Class(es)</th> <th>Source(s)</th> </tr> [% FOREACH row IN items %] <tr> <td align="left">[% row.0 %]</td> <td align="center">[% row.1 %]</td> <td align="center">[% row.2 %]</td> <td align="center">[% row.3 %]</td> <td align="left">[% row.4 ? row.4 : "&nbsp;" %]</td> <td align="left">[% row.5 %]</td> </tr> [% END %] </table> </body> </html>
    Live script can be run by hitting this url: http://marold.org/cgi-bin/showtable.cgi

      UnstoppableDrew;

      Before this goes too much further, MySQL is unavailable to me. It looks great, but I would need it to be pure Perl. Also, other things will be happening once I get the table to print, like having select boxes where the users can choose which proficiencies that they want to see.

      Great work! Have a splendid day...

      Lady Aleena

      "An it harm none, do as ye will."

Re^3: Creating a random generator
by UnstoppableDrew (Sexton) on Oct 07, 2007 at 18:40 UTC
    Hmm...I just looked at HTML::Template. You'd be way better off learning Template Toolkit over that.

      UnstoppableDrew;

      Are there significan't differences?

      Lady Aleena

      "An it harm none, do as ye will."

        I'd call the differences drastic. HTML::Template is close to a pure view layer. It has very few features and is meant only to display data handed to it. Template::Toolkit is a deep templating mini-language. I have mostly used TT for almost 20 years now and I like it but if you're going to learn something new… it's not what I'd call best practice. HTML::Template is going to be much less confusing and if you decide it's not enough, it will be easy to move up/over. Template::Alloy or probably Text::Xslate are places I'd look first once you're ready (or Mojo’s built-in stuff).

        Update… I didn't see this was over a decade old or I wouldn't have replied… I usually only browse by recent nodes. Maybe it got bumped?