eval

bash/ksh/scripting eval subshell quotes

 ̄綄美尐妖づ 提交于 2019-12-11 04:48:29
问题 I'm using ksh and having some trouble. Why does this code not run? [root]$ CMD="ls -ltr" [root]$ eval "W=$( $CMD )" [root]$ ksh: ls -ltr: not found. [root]$ echo $W But this works fine: [root]$ CMD="ls -ltr" [root]$ eval 'W=$('$CMD')' [root]$ echo $W 回答1: You need to escape the $(...) with a backslash to prevent it from being evaluated by the outside shell. The $(...) needs be preserved as is until it is handed off to the eval : $ CMD="ls -ltr" $ eval "W=\$( $CMD )" $ echo $W total 197092

Javascript: How to return or parse an object literal with eval?

浪尽此生 提交于 2019-12-11 04:46:39
问题 I have a little library that takes strings and constructs objects out of them. For example '-key val' creates {"key": "val"} . However I'm trying to extend the syntax of the input string to take simple object literals too, such as '-key "{key: 'val'}"' which should yield {"key" : {"key" : "val"}} however the result is only {"key" : "val"} . Why does eval only return "val" and not the entire object? And is there a safer alternative then my solution? // my code before the fix var arg = '{key:

Clojure: Expand a var in let binding

て烟熏妆下的殇ゞ 提交于 2019-12-11 04:25:18
问题 I want to reuse a set of local assignments in the let form of different functions. Let's say (def common-assign [x 10 y 20]) one way to do it is with eval : (eval `(defn ~'foo [] (let [~@common-assign ~'hello "world"]) balala)) The problem is that now you have to quote all other symbols, which is cumbersome. Is there any other clean way to do what I want? 回答1: (defmacro with-common [& body] `(let ~'[x 10, y 20] ~@body)) (with-common (+ x y)) 回答2: (def common-assign ['x 10 'y 20]) (defmacro

How to access dynamic property by using variable?

半腔热情 提交于 2019-12-11 03:42:59
问题 See: $class_members = get_class_vars(__CLASS__); foreach($class_members as $key => $value) { if (strpos($key, '_output') === 0) { // I want to eval() this $code = '$this->' . $key . ' = 0;'; } } Assume I want to assign the value 0 to all class members that begin with _output . I plan to use eval . Good or bad idea? 回答1: You don't need eval() for this. You can use a variable as in $this->{$key} : foreach($class_members as $key => $value) { if (strpos($key, '_output') === 0) { // Look mom, no

Use of eval to load modules

杀马特。学长 韩版系。学妹 提交于 2019-12-11 03:40:55
问题 I'm facing some trouble with Perl and built-in function eval. I have looked around the web but I can't find any answer or sample code. I'd like to load modules dynamically (I don't know them before the execution time) $module_name="Auth_Auth_Test"; my $ret1; ret = eval{ "use ".$module_name; $ret1 = $module_name."::test(".$log.")"; }; $log->debug ($@) if $@; $log->debug ("Ret".$ret1); The return was : RetAuth_Auth_Test::test(Custom::Log=HASH(0x1194468)) The following method worked for me but I

Pass BASH associative arrays to PHP script

此生再无相见时 提交于 2019-12-11 03:35:16
问题 Is it possible to pass BASH associative arrays as argv to PHP scripts? I have a bash script, that collects some variables to a bash associative array like this. After that, I need to send it to PHP script: typeset -A DATA DATA[foo]=$(some_bash_function "param1" "param2") DATA[bar]=$(some_other_bash_function) php script.php --data ${DATA[@]} From PHP script, i need to access the array in following manner: <?php $vars = getopt("",array( "data:" )); $data = $vars['data']; foreach ($data as $k=>

Accessing numbered variables in loop

青春壹個敷衍的年華 提交于 2019-12-11 03:19:12
问题 I've been unable to find an answer to the following question either in the Matlab documentation, or on message boards. There is much information regarding the use of dynamic variable names and how to avoid use of the eval function when creating variables. My query however concerns accessing pre-existing variables, which are numbered, inside a loop. Consider that someone has sent me a table with various field values. Some of them are numbered such that we have something like: table.abc table

Using ast.literal_eval on a nested dictionary

时光总嘲笑我的痴心妄想 提交于 2019-12-11 03:18:09
问题 I'm using ast.literal_eval to change the data I receive from json.loads() into a Python dictionary; however if I should just be going about this an entirely different way - feel free to point that out as well. # Authentication buf = StringIO.StringIO() c = pycurl.Curl() c.setopt(c.URL, "https://kippt.com/api/account") c.setopt(c.WRITEFUNCTION, buf.write) c.setopt(c.HTTPHEADER, header) c.perform() result = buf.getvalue() buf.close() print result # Printing Output data_string = json.dumps

Evaluating dynamically generated statements in Python

吃可爱长大的小学妹 提交于 2019-12-11 02:57:09
问题 I need to dynamically generate python code and execute it with eval() function. What I would like to do is to generate some "imports" and "assign values". I mean, I need to generate this string to evaluate it eval(x) . x = """ import testContextSummary import util.testGroupUtils testDb = [testContextSummary.TestContextSummary, testGroupUtils.testGroupUtils.TestGroupUtils] """ # x is automatically generated eval(x) ... use testDb ... I tried with this code, but eval() returns an error not

Store array in a string to eval() later

筅森魡賤 提交于 2019-12-11 02:15:36
问题 Having problems with eval() . I am forced to store strings in an array that are exectuted later on. Now, storing strings in the string is no problem. But how do I store an array in there? Since I will NOT have access to the variable, I want the array directly stored to this string. See this code: // ---------------------- // -- class A $strId = 'id_1234'; $strClass = 'classname'; $arParams = array('pluginid' => 'monitor', 'title' => 'Monitor', ...); $strClone = 'openForm(desktop(),"'.$strId.'