package Lua; use strict; use warnings; use Carp qw( croak ); #use Inline; my $luadir = '.'; sub new { my( $class ) = @_; my $self = {}; return( bless( $self, $class ) ); } sub load { my( $self, $module ) = @_; my $src_file = $module . '.lua' unless( $module =~ m/.*\.lua$/ ); my $path = path_join( $luadir, $src_file ); open( my $fd, '<', $path ) or warn "$src_file is inaccessible" and return; my $luacode = do { local $/; <$fd> }; close( $fd ); ####################################################### my $code = sprintf( 'package Lua::%s;' . 'use Inline;' . 'Inline->bind( Lua => $luacode );' . 'return( grep { ! /^BEGIN$/ } keys %%Lua::%s:: )', $module, $module ); my @symbols = eval $code; package Lua; chomp $@ if( $@ ); warn "Error compiling $path: '$@'" and return if( $@ ); ####################################################### return( @symbols ); } sub run { my( $self, $module, $funcname, @args ) = @_; ###################################################### my $sub = sprintf( 'Lua::%s::%s( @args );', $module, $funcname ); my @retval = eval $sub; chomp $@ if( $@ ); warn "Error executing $module::$funcname: $@" and return if( $@ ); ###################################################### return( @retval ); } sub path_join { my $pathchar = ( $^O eq 'MSWin32' ) ? "\\" : '/'; return( join( $pathchar, @_ ) ); } 1; __END__