Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Re: Extending a perl program with Scheme, Lua, or JS

by Laurent_R (Canon)
on Feb 08, 2019 at 00:02 UTC ( [id://1229580]=note: print w/replies, xml ) Need Help??


in reply to Extending a perl program with Scheme, Lua, or JS

Not entirely sure to understand what you really need and why you can't just perform the arithmetic operations with Perl. If it boils down to evaluating arithmetic expression, maybe you could use eval, but there might be some security problems, so you have to check that the expression to be evaluated is safe prior to doing it.

Another possible option that comes to my mind is to shell out to the bc Unix or Linux utility. bc can be used in non-interactive mode in various ways, including piping (echo "42/7" | bc), shell redirections, and Un*x heredocs.

Replies are listed 'Best First'.
Re^2: Extending a perl program with Scheme, Lua, or JS
by afoken (Chancellor) on Feb 08, 2019 at 05:43 UTC
    Another possible option that comes to my mind is to shell out to the bc Unix or Linux utility. bc can be used in non-interactive mode in various ways, including piping (echo "42/7" | bc), shell redirections, and Un*x heredocs.

    How to make things even worse. I smell shell injection: "Dear script, please evaluate 1 + $(rm -rf /) or 1 + ";rm -rf /;echo " for me." (echo "1 + $(rm -rf /)" | bc resp. echo "1 + ";rm -rf /;echo "" | bc) Shelling out ain't that easy if you want to do it right: The problem of "the" default shell

    And even if you shell out safely, the program you feed the input into may have security issues. See Morris_worm for a really old example, or CVE-2012-1165 for a younger one.

    Alexander

    --
    Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
      Yes, you're absolutely right, the type of security problems I mentioned about eval also apply to shelling out, I should have mentioned that explicitly. I actually don't like much the idea of shelling out, my point in that paragraph was only to say that if one decided to shell out, it might be simpler to use bc rather than heavy artillery such as an interpreter, compiler, or VM for Lisp, Lua, or JS.
      Thanks for the warning, but as mentioned in the OP, it's not a web app.

        That's OK. Injection attacks are equally plausible against non-web apps too. Indeed the very example to which Brother afoken drew your attention did not attack web apps, predating the web as it did. If you are writing scripts of any sort and not using taint mode you will reap what you sow.

        Forget attacks protect against stjpix typos that cost you data
Re^2: Extending a perl program with Scheme, Lua, or JS
by bcrowell2 (Friar) on Feb 09, 2019 at 20:58 UTC

    Not entirely sure to understand what you really need and why you can't just perform the arithmetic operations with Perl

    If other people are going to use it, I don't really want them to have to learn perl syntax. It looks messy and confusing to the uninitiated, with all the sigils, and the nonstandard mechanism for passing arguments to functions. I don't know, maybe it's just my perception, but say you show someone who has some basic coding experience this code:

    sub f {$x=shift; return $x*0.1}
    If they've never seen perl before, they're going to wonder what the heck the dollar signs are, and what shift is. The equivalent in Scheme is this:
    (define (f x) (* x 0.1))
    Here, I think the only possibly mysterious thing would be that you have to realize that it's prefix rather than infix notation. Maybe for zero-surprises pure vanilla syntax, JS wins:
    function f(x) {return x*0.1}

    However, the only JS interpreter that seems to meet my criteria is Rhino, and it seems to have unacceptable performance for my purposes if I try to start up an interpreter once for each operation:

    $ time rhino -e 'function f(x) {return x*0.1}; print(f(34))' 3.4000000000000004 real 0m0.240s user 0m0.292s sys 0m0.036s

    (Rhino is actually amazingly fast once it starts up -- in my experience it's sometimes faster than perl, and within a factor of 2 or 3 compared to C.)

    Another possible option that comes to my mind is to shell out to the bc Unix or Linux utility. bc can be used in non-interactive mode in various ways, including piping (echo "42/7" | bc), shell redirections, and Un*x heredocs.

    Nice suggestion, thanks. It's definitely nice in terms of performance and zero-surprises syntax:

    time echo "define f(x) {x*0.1}; print f(34)" | bc 3.4 0 real 0m0.002s user 0m0.000s sys 0m0.000s

    I think the only problem from my point of view might be that I will probably sometimes need to use data structures, which bc doesn't have. E.g., a common application would be that you want to drop the student's lowest quiz score, so you need to pass the list of quiz scores to the function. (Another issue would be that this would make it linux-only.)

Re^2: Extending a perl program with Scheme, Lua, or JS
by bcrowell2 (Friar) on Feb 14, 2019 at 02:55 UTC
    After originally implementing this in Guile, I've ended up switching to bc, because I couldn't figure out any clean way of preventing the possiblity of Trojan horse attacks if I was using Guile.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1229580]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (2)
As of 2024-04-26 00:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found