0

我有一个源文件 openpage.pl,我在其中调用 use_module/1 来“导入”SWI-Prolog 的 http_open/3:

use_module(library(http/http_open)).

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

它毫无怨言地加载。但是,尽我所能,我无法按照其中的规则运行它。

?- [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

所以在我的下一个会话中,我在加载源文件之前调用 use_module/1 ,一切都很好:

?- 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).

如何设置和执行我的文件,以便在加载我自己的代码之前不需要这个手动加载模块的步骤?

4

1 回答 1

5

尝试:

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

在您的源文件中。

于 2012-06-15T17:41:27.067 回答