Bug with XML-PARSE?

Q&A's, tips, howto's
Locked
pjot
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Bug with XML-PARSE?

Post by pjot »

I found this peculiar code:

Code: Select all

#!/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
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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
Posts: 733
Joined: Thu Feb 26, 2004 10:19 pm
Location: The Hague, The Netherlands
Contact:

Post by pjot »

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

Code: Select all

(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:

Code: Select all

(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:

Code: Select all

(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
Posts: 5289
Joined: Thu Sep 26, 2002 4:45 pm
Location: Pasadena, California
Contact:

Post by Lutz »

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.

Code: Select all

(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.

Code: Select all

(context 'DEMO)

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

(coompare)
Lutz

Locked