The Perl Toolchain Summit needs more sponsors. If your company depends on Perl, please support this very important event.

NAME

Nile::App - App base class for the Nile framework.

SYNOPSIS

DESCRIPTION

Nile::App - App base class for the Nile framework.

object()

    $obj = $app->object("Nile::MyClass", @args);
    $obj = $app->object("Nile::Plugin::MyClass", @args);
    $obj = $app->object("Nile::Module::MyClass", @args);

    #...

    $app = $obj->app;
    $request = $app->request;
    $response = $app->response;
    

Creates and returns an object. This automatically adds the method app to the object and sets it to the current context so your object or class can access the current instance.

start()

    $app->start;

Set the application startup variables.

mode()

    my $mode = $app->mode;

Returns the current application mode PSGI, FCGI or CGI.

config()

See Nile::Config.

router()

See Nile::Router.

lang()

See Nile::Lang.

lang()

See Nile::Lang.

uri_mode()

    # uri mode: 0=full, 1=absolute, 2=relative
    $app->uri_mode(1);

Set the uri mode. The values allowed are: 0= full, 1=absolute, 2=relative

uri_for()

    $url = $app->uri_for("/users", [$mode]);

Returns the uri for specific action or route. The mode parameter is optional. The mode values allowed are: 0= full, 1=absolute, 2=relative.

debug()

    # 1=enabled, 0=disabled
    $app->debug(1);

Enable or disable debugging flag.

bm()

    $app->bm->lap("start task");
    ....
    $app->bm->lap("end task");
    
    say $app->bm->stop->summary;

    # NAME          TIME        CUMULATIVE      PERCENTAGE
    # start task        0.123       0.123           34.462%
    # end task      0.234       0.357           65.530%
    # _stop_        0.000       0.357           0.008%
    
    say "Total time: " . $app->bm->total_time;

Benchmark specific parts of your code. This is a Benchmark::Stopwatch object.

file()

See Nile::File.

xml()

See Nile::XML.

setting()

See Nile::Setting.

mime()

See Nile::MIME.

dispatcher()

See Nile::Dispatcher.

logger()

Returns Log::Tiny object.

log()

    $app->log->info("application run start");
    $app->log->DEBUG("application run start");
    $app->log->ERROR("application run start");
    $app->log->INFO("application run start");
    $app->log->ANYTHING("application run start");

 Log object L<Log::Tiny> supports unlimited log categories.

start_logger()

    $app->start_logger();

Start the log object and open the log file for writing logs.

stop_logger()

    $app->stop_logger();

Stops the log object and close the log file.

timer()

    # start the timer
    $app->timer->start;
    
    # do some operations...
    
    # get time elapsed since start called
    say $app->timer->lap;

    # do some other operations...

    # get time elapsed since last lap called
    say $app->timer->lap;

    # get another timer object, timer automatically starts
    my $timer = $app->timer->new;
    say $timer->lap;
    #...
    say $timer->lap;
    #...
    say $timer->total;

Returns Nile::Timer object. See Nile::Timer for more details.

run_time()

    # get time elapsed since app started
    say $app->run_time->lap;

    # do some other operations...

    # get time elapsed since last lap called
    say $app->run_time->lap;

Returns Nile::Timer object. Timer automatically starts with the application.

var()

See Nile::Var.

env()

    $request_uri = $app->env->{REQUEST_URI};

Plack/PSGI env object.

request()

See Nile::Request.

response()

See Nile::Response.

plugin()

See Nile::Plugin.

helper()

    # add helper method to the framework
    
    $app->helper($method => $coderef);
    
    # add method "echo"
    $app->helper("echo" => sub{shift; say @_;});

    # access the helper method normal from plugins and modules
    $app->echo("Helper echo example.");

attr()

    # add attr to the framework
    
    $app->attr($name => $default);
    
    # add attribute "PI"
    $app->attr("PI" => 4 * atan2(1, 1));
        
        # or
        $app->attr("PI" => sub{4 * atan2(1, 1)});

    # get the attribute value
    say $app->PI;

    # set the the attribute value to new value
    $app->PI(3.14159265358979);

ua()

    my $response = $app->ua->get('http://example.com/');
    say $response->{content} if length $response->{content};
    
    $response = $app->ua->get($url, \%options);
    $response = $app->ua->head($url);
    
    $response = $app->ua->post_form($url, $form_data);
    $response = $app->ua->post_form($url, $form_data, \%options);

Simple HTTP client. This is a HTTP::Tiny object.

uri()

    my $uri = $app->uri('http://mewsoft.com/');

Returns URI object.

charset()

    $app->charset('utf8');
    $charset = $app->charset;

Set or return the charset for encoding and decoding. Default is utf8.

freeze()

See Nile::Serializer.

thaw()

See Nile::Deserializer.

module()

    # load module Nile::Module::Home::Contact and create a new object
    $contact = $me->module("Home::Contact");

    # to get another new instance
    $contact1 = $me->module("Home::MyModule")->new();
    # or
    $contact2 = $contact->new();

    # if you are calling from inside the Home module, you can just use
    $contact = $me->module("Contact");

    # of course you can load sub classes
    $send = $me->module("Home::Contact::Send");

    # if you are calling from inside the Home module, you can just use
    $send = $me->module("Contact::Send");

    # all the above is the same as
    use Nile::Module::Home::Contact;
    $contact = Nile::Module::Home::Contact->new();
    $contact->main() if ($contact->can("main"));

Load modules classes.

hook()

See Nile::Hook.

filter()

See Nile::Filter.

session()

See session plugin Nile::Plugin::Session.

detect_user_language()

    $user_lang = $app->detect_user_language;

Detects and retuns the user langauge.

view()

Returns Nile::View object.

database()

Returns Nile::Database object.

theme_list()

    @themes = $app->theme_list;

Returns themes names installed.

lang_list()

    @langs = $app->lang_list;

Returns languages names installed.

dump()

    $app->dump({...});

Print object to the STDOUT. Same as say Dumper (@_);.

is_loaded()

    if ($app->is_loaded("Nile::SomeModule")) {
        #...
    }
    
    if ($app->is_loaded("Nile/SomeModule.pm")) {
        #...
    }

Returns true if module is loaded, false otherwise.

cli_mode()

    if ($app->cli_mode) {
        say "Running from the command line";
    }
    else {
        say "Running from web server";
    }

Returns true if running from the command line interface, false if called from web server.

utf8_safe()

    $str_utf8 = $app->utf8_safe($str);

Encode data in utf8 safely.

encode()

    $encoded = $app->encode($data);

Encode data using the current "charset".

decode()

    $data = $app->decode($encoded);

Decode data using the current "charset".

instance_isa()

    $app->instance_isa($object, $class);

Test for an object of a particular class in a strictly correct manner.

Returns the object itself or undef if the value provided is not an object of that type.

abort()

    $app->abort("error message");

    $app->abort("error title", "error message");

Stop and quit the application and display message to the user. See Nile::Abort module.

Bugs

This project is available on github at https://github.com/mewsoft/Nile.

HOMEPAGE

Please visit the project's homepage at https://metacpan.org/release/Nile.

SOURCE

Source repository is at https://github.com/mewsoft/Nile.

SEE ALSO

See Nile for details about the complete framework.

AUTHOR

Ahmed Amin Elsheshtawy, احمد امين الششتاوى <mewsoft@cpan.org> Website: http://www.mewsoft.com

COPYRIGHT AND LICENSE

Copyright (C) 2014-2015 by Dr. Ahmed Amin Elsheshtawy احمد امين الششتاوى mewsoft@cpan.org, support@mewsoft.com, https://github.com/mewsoft/Nile, http://www.mewsoft.com

This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.