slang-users mailing list

[2019 Date Index] [2019 Thread Index] [Other years]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]

Re: [slang-users] Limitations of eval()?


Bernd Eggink <bernd.eggink@xxxxxxxxxx> wrote:
> I noticed that, while this works:
>
>slsh> s = "print(\"foo\")"; eval(s);
>
> "foo"
>
> this doesn't:
>
>slsh> s = "print(\"foo\nbar\")"; eval(s);
>
> ***string***:1: Expecting a quote-character: found '??'
>
> It seems that eval() doesn't like statements with a line break in it, 
> but I found no such limitation mentioned in the docs.

Here is what the eval function saw:

   print("foo
   bar")

This is not a valid statement since the parsing of the string "foo..."
stopped at the newline.  You need to pass to the eval function one of
the string forms such as:

  print("foo\nbar");

  print(`foo
  bar`);

  print("foo\n\
  bar");

The above 3 forms would be coded as:

  s = "print(\"foo\\nbar\")"; eval(s);
  s = "print(`foo\nbar`)"; eval(s);
  s = "print(\"foo\\n\\\nbar\")"; eval(s);

Note that the print function will not output a physical newline
character to the terminal.  If you want that, then either use the
message function, e.g.,

 s = "message(\"foo\\nbar\")"; eval(s);

or use the "pager" qualifier:

 s = "print(\"foo\\nbar\";pager)"; eval(s);

I hope this helps.
Thanks,
--John
_______________________________________________
For list information, visit <http://jedsoft.org/slang/mailinglists.html>.


[2019 date index] [2019 thread index]
[Thread Prev] [Thread Next]      [Date Prev] [Date Next]