Bug with XML-PARSE?

Started by pjot, July 16, 2005, 01:56:25 PM

Previous topic - Next topic

pjot

I found this peculiar code:



#!/usr/bin/newlisp

(context 'DEMO)

(define (DEMO:DEMO)
(xml-type-tags nil 'cdata '!-- nil)
(set 'url (xml-parse (get-url "http://newlisp.org/rss.cgi?News") (+ 1 2 8 16) ))
)

#-----------------------------------

(context 'MAIN)

(DEMO)
(xml-type-tags nil 'cdata '!-- nil)
(set 'url (xml-parse (get-url "http://newlisp.org/rss.cgi?News") (+ 1 2 8 16) ))

(if (= url DEMO:url) (println "They are equal")(println "They are not"))

(exit)


The result is "They are not". How come the 'url' variables do not have the same content?



If I try to read symbols of the 'url' variable in the MAIN context, everything is OK. But for some reason, the DEMO context does not see the elements in the 'url' variable as symbols.



What am I doing wrong here?



Peter

pjot

#1
Aha I see the problem: in a context the contextname is put before symbols in lists. This does not happen with string variables though.



Peter

Lutz

#2
url and DEMO:url are the same exept for the variable 'cdata ans '!-- which one version is created in DEMO and the others in MAIN as part of the program code.



If you do:



 (pop url)



and:



(pop DEMO:url)



you will get:



(= DEMO:url url) => true



Because you are calling (DEMO) while in MAIN all symbols: channel, link, item etc. are created in MAIN and the different '!-- is popped off.



Lutz

pjot

#3
Well I had a problem checking on the appearance of a symbol in the 'url' variable. E.g.:



(if (= (nth 0 url) 'something) (do blabla))


does not work in a context. The reason is that the "something"-symbol automatically will be preceeded with the contextname. So the equation in the context 'DEMO really is:



(if (= (nth 0 url) 'DEMO:something) (do blabla))


and this never becomes true, since the list in the 'url' variable never possesses the symbol 'DEMO:something.



I have solved this by evaluating dynamically to a symbol:



(if (= (nth 0 url) (symbol "something")) (do blabla))


This way, the equation really will compare with the symbol 'something instead of 'DEMO:something.



Actually I find this behaviour a little bit peculiar, since variables can be used in a context without the contextname as well. I'ld expect the usage of symbols to be similar. Obviously, newLisp changes a symbolname to a name with a context-prefix behind my back :-(



Anyway my new version of the RSS reader is working now.



Thanks





Peter

Lutz

#4
Just to clarify for other readers:



The context name in front of a symbol is only shown when the the symbol is displayed outside the scope of its current context. Your symbol 'something belongs to a context which is defined when it is read/translated the first time. That context it belongs to is never ever changed afterwards.



(context 'DEMO)

(define (compare)
   (if (= (nth 0 url) 'something) (do blahblah))
(context MAIN)


'symbol will belong to DEMO because that is the context under which it was first seen/translated.



In the following code sym will create 'something as part of MAIN because the 'sym funtion is executed while in MAIN. When the file is read "something" is just the a string.



(context 'DEMO)

(define (compare)
   (if (= (nth 0 url) (sym "something")) (do blahblah))
(context MAIN)

(coompare)


Lutz