为什么 Perl 的 LWP::Simple 附带的 head 命令仅在我在命令行上调用它时才起作用?
我有一个简单的 Perl 子例程,用于检查 google 是否仍在托管 jquery 1.6 的副本,然后决定是否打印链接到它或本地托管副本的脚本。
这是我正在使用的代码的副本。
my $jquery_host = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js";
my $header = LWP::Simple::head($jquery_host);
if(defined $header) {
return qq{<script type="text/javascript" src="$jquery_host"></script>};
}
else {
return qq{<script type="text/javascript" src="$localPath"></script>};
}
当我在此命令行上运行此代码时,检索响应没有问题,并且它正确打印出链接到 google 的 jquery 副本的脚本标记。然而,当我实际从构建 html 页面的 perl 脚本调用它时,它每次都找不到任何内容,并打印出链接到我们自己的副本的脚本标记。
哪些权限或其他类型的障碍可能会停止此连接?
感谢您的帮助。
注意:到目前为止,这仅在我们的本地测试服务器上运行。命令行也在测试服务器上运行。
I've got a simple perl subroutine that checks to see if google is still hosting a copy of jquery 1.6 before deciding whether to print a script linking to it or to our locally hosted copy.
This is a copy of the code I'm using.
my $jquery_host = "http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js";
my $header = LWP::Simple::head($jquery_host);
if(defined $header) {
return qq{<script type="text/javascript" src="$jquery_host"></script>};
}
else {
return qq{<script type="text/javascript" src="$localPath"></script>};
}
When I run this code on this command line, I have no problem retrieving a response, and it properly prints out a script tag linking to google's copy of jquery. However, when I actually call it from a perl script that is building an html page, it finds nothing every time and prints out a script tag linking to our own copy.
What permissions or other type of barrier could be halting this connection?
Thanks for your help.
NOTE: This has only run on our local test server so far. The command line is also running on the test server.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我可以确认您的代码可以单独运行。尝试将对 header 的赋值包装到 if 语句中;事实上也内联了 url 字符串。唯一可能困扰您的事情是您的服务器脚本正在使用某些代理或 tcp 包装器的计算机上运行,而这些代理或 TCP 包装器不允许您从 google.com 获取 jQuery。我们无法真正帮助您解决这些非常具体的本地问题。
如果我只是猜测,我会说你有多个 Perl 环境正在运行,并且你的命令行正在使用一个很好的版本,可以正常工作,但你的 Web 服务器正在调用另一个版本,一个可能有已以破坏您正在做的事情的方式进行了强化,例如在 AppEngine 中使用所提议的内容。但我上面的建议是基于以下想法:此代码段周围的代码可能会干扰您正在使用的变量。似乎这种这种问题确实出现在使用mod_perl 中的 io 套接字。
程序片段:
输出:
我也会这样写,以便将一些
$scriptPath
分配给 jQuery 路径或$localPath
,然后通过 if 语句 I' d 使用现在确定的$scriptPath
仅打印一次脚本标记。但这也许只是我。I can confirm that your code works in isolation. Try wrapping the assignment to header into the if statement; in fact inline the url string too. The only other thing that could be messing with you is that your server script is running on a machine that uses some proxy or tcp wrapper which isn't letting you fetch jQuery from google.com. We can't really help you with these very specific very local issues.
If I were just guessing I'd say that the you have more than one Perl environment going on, and that your command line is using a nice version where things work, but your web server is calling out to another version, one that might have been hardened in ways that break what you're doing, like what was proposed for use in AppEngine. But my suggestions above are based on the idea that the code around this snippet might be messing with the variables you're using. It seems this kind of issue does come up though around the use of io sockets in mod_perl.
Program snippet:
Output:
I'd also write this so that some
$scriptPath
gets assigned to either the jQuery path or the$localPath
, and then past the if statement I'd print the script tag only once using the now determined$scriptPath
. But that's just me perhaps.我不知道为什么您的代码不起作用,但您无论如何都不应该使用它:仅仅因为您的服务器可以访问
ajax.googleapis.com
并不意味着用户的浏览器可以。只需在客户端上使用 JavaScript 进行回退即可。以下是一些基于 这个答案:
I don't know why your code isn't working, but you shouldn't be using it anyway: just because your server can reach
ajax.googleapis.com
doesn't mean the user's browser can.Just do the fallback in JavaScript on the client instead. Here's some sample code based on this answer: