Chapter 3.5: Preset

The input data are collected and stored into $self->{ARGS}. They are classified into 3 types:

  1. Input data in query, POST body and login cookie. These variables are saved in their original names. For example, if you pass user_id=1234 in query, then you will have $self->{ARGS}->{user_id} = 1234. Multiple values will be saved as array reference.
  2. Navigation data, starting with “g_“.
    • g_role: the role group of the visitor
    • g_action: the action
    • g_component: the component it acts on
    • g_server, g_scriptg_scriptfullg_query_string: current URL’s information.
    • g_json_url: the corresponding URL for JSON API.
  3. Supplementary data,  starting with “_g“. They are used for internal purpose. Unlike the above 2 types, they are not export to HTML template or API. Only if you dump $self->{ARGS} will you see them.

preset is the phase in which you can further do data process, such as adding new variables,  modifying existing ones, or enabling pagination. Since it inherits from the project, you should first call project’s preset. The usage is:

sub preset {
  my $self = shift;
  my $err = $self->SUPER::preset(@_);
  return $err if $err;

  my $ARGS = $self->{ARGS};
  my $reqs = $self->{R};
  my $action = $ARGS->{g_action};
  my $role = $ARGS->{g_role};

  do_something
  # enable pagination: $ARGS->{rowcounts} = 50;

  return;
}

If the run is successful, return nothing. If something goes wrong, return the error code or an explanation string. Genelet will stop immediately and pop up the error page.

If you need to access 3rd-part API, preset is a good phase to do that.