Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

How to avoid eval()?

by emilbarton (Scribe)
on Sep 16, 2011 at 07:26 UTC ( [id://926338]=perlquestion: print w/replies, xml ) Need Help??

emilbarton has asked for the wisdom of the Perl Monks concerning the following question:

Hello, is there a way to process Perl commands in a readline program without using eval() ? I'm facing problems with this function on recursive subs actually.

Replies are listed 'Best First'.
Re: How to avoid eval()?
by moritz (Cardinal) on Sep 16, 2011 at 08:00 UTC

    eval is what you use for the most general case of executing perl commands that you have in a string. Why do you want to avoid it?

    As it is, the question is a bit like "I'd like to make a phone call, but without using a phone".

    If you only want to allow certain operations, several techniques exist, the best choice depends on what exactly you want to do. For example for only evaluating mathematical expressions safely, modules like Math::Expression::Evaluator exist.

Re: How to avoid eval()?
by choroba (Cardinal) on Sep 16, 2011 at 07:35 UTC

      I didn't want to enter in too much detail and as they usually recommand not trusting eval(), I thought it would suffice.

      The problem is that I loose the well ordering of my recursive set when calling my function through an eval() while it is kept when using a dedicated program to perform the same function.

      In the past I went into much confusion for problems of this kind till I migrated my recursive functions to C.

        What you wrote doesn't make any sense (or maybe only in some context that is not apparent from this discussion thread)

        I don't see how using eval() would destroy any order, and there's no code to demonstrate it.

        Oh, and sets are unordered by definition.

        A reply falls below the community's threshold of quality. You may see it by logging in.

        I highly doubt that you mean what you say when you use words like Well Ordering a Recursive Set.

        Maybe you really want to show us the relevant part of your code as a self-contained program. Please also do show what input data you give it and what output you get and what output you expect.

        A reply falls below the community's threshold of quality. You may see it by logging in.
Re: How to avoid eval()?
by bart (Canon) on Sep 16, 2011 at 10:43 UTC
    There are two kinds of eval:
    1. eval STRING

      This takes a piece of string and executes it as Perl code. This is the bad kind.

    2. eval BLOCK

      This is Perl's equivalent for try/catch in other languages, and there's nothing wrong with it.

    What kind of eval are you talking about?

    update Duh! You're talking about executing user supplied code, so of course it's eval STRING.

    Well, there's no way to avoid executing user supplied code if you want to execute user supplied code.

    Either you eval the input as Perl, or you provide a small language (not Perl) and interpret it, in Perl. The latter is safer, but a lot of work to implement.

Re: How to avoid eval()?
by Anonymous Monk on Sep 16, 2011 at 07:46 UTC
Re: How to avoid eval()?
by BrowserUk (Patriarch) on Sep 16, 2011 at 09:47 UTC
Re: How to avoid eval()?
by ambrus (Abbot) on Sep 16, 2011 at 08:46 UTC

    It would also be nice if you told us what problems you're facing with recursive subs.

A reply falls below the community's threshold of quality. You may see it by logging in.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others studying the Monastery: (4)
As of 2024-03-19 04:56 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found