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.
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
$scriptPathgets 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.