0

I have a source file, openpage.pl, where I call use_module/1 to "import" SWI-Prolog's http_open/3:

use_module(library(http/http_open)).

request(URL, In) :- http_open(URL, In, []),
    copy_stream_data(In, user_output),
    close(In).

It loads without complaint. However, try as I might, I can't run it the rules in it.

?- [openpage].
% openpage compiled 0.00 sec, 1,828 bytes
true.

?- request('http://www.google.com', In).
ERROR: request/2: Undefined procedure: http_open/3
?- use_module(library(http/http_open)).
true.

?- request('http://www.google.com', In).
ERROR: request/2: Undefined procedure: http_open/3
?- make.
% Scanning references for 1 possibly undefined predicates
Warning: The predicates below are not defined. If these are defined
Warning: at runtime using assert/1, use :- dynamic Name/Arity.
Warning: 
Warning: http_open/3, which is referenced by
Warning:    status/2 at /home/dale/sesame_test/prolog/openpage.pl:16
Warning:    request/2 at /home/dale/sesame_test/prolog/openpage.pl:3
Warning:    modified/2 at /home/dale/sesame_test/prolog/openpage.pl:7
true.

?- [openpage].
% openpage compiled 0.00 sec, 616 bytes
true.

?- request('http://www.google.com', In).
ERROR: request/2: Undefined procedure: http_open/3
?- 
[forced] Action (h for help) ? exit

So in my next session, I invoke use_module/1 before loading my source file, and all is fine:

?- use_module(library(http/http_open)).
%  library(uri) compiled into uri 0.00 sec, 199,772 bytes
%  library(readutil) compiled into read_util 0.00 sec, 10,312 bytes
%  library(socket) compiled into socket 0.00 sec, 6,376 bytes
%  library(option) compiled into swi_option 0.00 sec, 7,748 bytes
%  library(base64) compiled into base64 0.00 sec, 9,776 bytes
%  library(debug) compiled into prolog_debug 0.01 sec, 12,056 bytes
% library(http/http_open) compiled into http_open 0.01 sec, 282,844 bytes
true.

?- [openpage].
% openpage compiled 0.00 sec, 1,380 bytes
true.

?- request('http://www.google.com/', In).
<!doctype html><html itemscope itemtype="http://schema.org/WebPage">
...
In = <stream>(0x9366508).

How can I set up and execute my files so that I don't need this manual step of loading modules before loading my own code?

1 Answers1

5

Try:

:- use_module(library(http/http_open)).

in your source file.

Kaarel
  • 10,554
  • 4
  • 56
  • 78
  • 1
    At first I thought you hadn't read my question closely, but then I saw that you meant, put ':-' in front the use_module predicate at the beginning of the source file. It worked, and I think I understand why. Thank you! – sdesciencelover Jun 15 '12 at 23:30
  • Kaarel answer preceded mine by 20 sec, the time I took to check how SWI-Prolog IDE reacts to that miss operator. Probably you are not using the IDE, that highlights the miss directive very well. – CapelliC Jun 16 '12 at 06:10