Hello, World-3!

While PET can be used as some sort of an “active pages” system, something similar to ASP/PHP/JSP, clearly it is not the recommended usage.  What you will most likely be using -- or should be using at least -- is MVC-style.

Here are some examples.  For details, see MVC-style Programming!  Also take a look at http://en.wikipedia.org/wiki/Model-view-controller.

Summary
Hello, World-3!While PET can be used as some sort of an “active pages” system, something similar to ASP/PHP/JSP, clearly it is not the recommended usage.
Basic substitution -- a “Hello, World!”Let’s see the simplest example possible.
A login pageLet’s log in a user using a basic form.
Global substitutionsJust another example to show another feature.

Basic substitution -- a “Hello, World!”

Let’s see the simplest example possible.  In your “index.pet”, you have this line:

...
[% subst.msg %]
...

In your “MyApp.pm” you have

...
sub ACTION_index {
    my $self = shift;

    return {
        'msg'   =>  'Hello, World!'
    };
}
...

How does this work?

The template index.pet is associated with a method in MyApp.pm called ACION_index.  When run, PET automatically calls that method, and the returned hashref will be put in the variable “subst”, which you can access in your template as subst.msg.  This nicely separates your program logic from your view (HTML).

Note : this is not a perfect MVC, but more on that later.

A login page

Let’s log in a user using a basic form.  (We will be using some built-in PET stuff here, also).

In your login.pet, you have this

...
[% IF subst.msg %]
<div class="warning">[% subst.msg %]</div>
[% END %]
...
<form method="post">
    <input type="text" value="" size="12" name="username" />
    <input type="password" value="" size="12" name="password" />
    <input type="submit" value="Login" name="login" />
</form>
...

In your “MyApp.pm” (which is a configurable module) you have

...
sub ACTION_login {
    my $self = shift;

    $Disp->setConf('general/fillinform' => 1);

    if ($Form->{'login'}) {
        my $ok = $self->logInUser('username' => $Form->{'username'},
            'password' => $Form->{'password'});
        if ($ok) {
            return '/internal_page.pet';
        } else {
            return { 'msg' => 'Wrong username or password!' };
        }
    }
}
...

That’s it!  The first “if” line checks if the login button was pressed, then calls logInUser (a method you have to write) to see if the user could be logged in using the given authentication data.  If it was a successful login, a string is returned, which PET understands as a redirect, and redirects you to an internal page.  If there was an error, the ‘msg’ variable is set, which is displayed on the same page.

Note the setConf line, which tells PET to use HTML::FillInForm -- that is, to write back all data into the form so the user does not have to fill it in again!  (How convenient :))

Global substitutions

Just another example to show another feature.  What if you want to display the time on all of your pages?  It’s inconvenient to put the very same substitution on each page.  Fortunately, PET provides a shortcut.

In your main module MyApp.pm, put in the following

sub getGlobalSubst {
      my $self = shift;

      return {
      'LOCALTIME' => localtime(time)
  };
}

Then in any of your .pet pages you can include this

...
[% gsubst.LOCALTIME %]
...

The getGlobalSubst method should return a hashref, which populates the gsubst variable accessible everywhere.  This is called before calling the method associated with the page, so you can even use the variables created here in your code.

While PET can be used as some sort of an “active pages” system, that is clearly not the recommended usage.
Close