OpenResty::RestyScript - Perl wrapper for the restyscript compiler via IPC


OpenResty documentation Contained in the OpenResty distribution.

Index


Code Index:

NAME

Top

OpenResty::RestyScript - Perl wrapper for the restyscript compiler via IPC

NAME

Top

The restyscript compiler is written in Haskell and located at haskell/bin/restyscript. This is a Perl wrapper for interact with it via IPC::Run3.

METHODS

Top

$obj = OpenResty::RestyScript->new($type, $src)

Create a new OpenResty::RestyScript instance with the $type parameter indicating "view" or "action" and the $src parameter indicating the RestyScript source code.

$new_src = $obj->rename($old_var_name, $new_var_name)

Renames the variable specified by $old_var_name with the new name specified by $new_var_name, and returns the new source. R

($frags, $stats) = $obj->compile()

AUTHOR

Top

Agent Zhang (agentzh) <agentzh@yahoo.cn>

COPYRIGHT AND LICENSE

Top


OpenResty documentation Contained in the OpenResty distribution.

package OpenResty::RestyScript;

use strict;
use warnings;

#use Smart::Comments;
use FindBin;
use Carp qw(croak);
use IPC::Run qw(run timeout);
use JSON::XS;

my $json = JSON::XS->new->utf8;

my $ExePath = "$FindBin::Bin/../haskell/bin/restyscript";
my $ExePath2 = "$FindBin::Bin/restyscript";

sub new {
    my ($class, $type, $src) = @_;
    if (!-f $ExePath && !-f $ExePath2) {
        croak "Restyscript compiler cannot found at either $ExePath or $ExePath2";
    }
    if (!-f $ExePath) { $ExePath = $ExePath2 }
    if (!-x $ExePath) {
        croak "Restyscript compiler is not executable: $ExePath";
    }

    return bless {
        _src => $src,
        _type => $type,
    }, $class;
}

sub rename {
    my ($self, $var, $newvar) = @_;
    my ($stdout, $stderr);
    my $type = $self->{_type};
    my $stdin = $self->{_src};
    run [$ExePath, $type, 'rename', $var, $newvar], \$stdin, \$stdout, \$stderr, timeout(1);
    if (!$stdout) {
        die $stderr || "Failed to call \"$ExePath $type rename $var $newvar\": returned status code " . ($? >> 8) . "\n";
    }

    $stdout;
}

sub compile {
    my ($self) = @_;
    my ($stdout, $stderr);
    my $type = $self->{_type};
    my $stdin = $self->{_src};

    # Note that we can't use the "run ... or die ..." idiom here due to
    # some mysterious failures (status code ) on our
    # production machines (redhat 4)
    run [$ExePath, $type, 'frags', 'stats'], \$stdin, \$stdout, \$stderr, timeout(1);
    #warn "STDOUT: $stdout\n";
    #warn "STDERR: $stderr\n";
    if (!$stdout) {
        die $stderr || "Failed to call \"$ExePath $type frags stats\" returned status code " . ($? >> 8) . "\n";
    }

    ### $stdout
    my @ln = split /\n/, $stdout;
    return ($json->decode($ln[0]), $json->decode($ln[1]))
}

1;
__END__