1

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.

Sinan Ünür
  • 116,958
  • 15
  • 196
  • 339
Will Reese
  • 2,801
  • 2
  • 15
  • 27
  • Perhaps when added to your other script $jquery_host is no longer what you expect at the point the head() is requested. Add a print() to see. – JRFerguson Oct 31 '11 at 18:18
  • That URL to google's jquery copy is correct. The head is coming back with information everytime I run the function from the command line. – Will Reese Oct 31 '11 at 21:11

2 Answers2

1

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:

$ perl -MLWP::Simple -e '
if(head("http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js")) {
    print qq{<script type="text/javascript"
      src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
    </script>};
} else {
    print qq{<script type="text/javascript" src="$localPath"></script>};
}'

Output:

<script type="text/javascript"
  src="http://ajax.googleapis.com/ajax/libs/jquery/1.6.2/jquery.min.js">
</script>

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.

dlamblin
  • 43,965
  • 20
  • 101
  • 140
  • I agree that its probably a specific local issue, so I guess what I'm asking is what are probably issues that might be disrupting my ability to use the header command from a perl script in my website running on my localhost, but would still allow me to make the connection when calling it from the command line. – Will Reese Nov 01 '11 at 16:09
  • You should probably update your question with the specifics of how this code is integrated into your webserver. Is this Apache's mod_cgi running a perl cgi script that includes this code, is it Apache's mod_perl doing something similar. Is it a perl web framework like catalyst, or are you using a totally different web server? I'll update my answer with one or two suggestions. – dlamblin Nov 01 '11 at 18:59
  • 1
    io sockets in mod_perl was the problem. I just switched over to LWP::UserAgent and used its head command and problem solved. Thank you very much for your help! – Will Reese Nov 02 '11 at 14:15
0

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:

return <<"END";
<script type="text/javascript" src="$jquery_host"></script>
<script type="text/javascript">
if (typeof jQuery == 'undefined') document.write(
    '<'+'script type="text/javascript" src="$localPath"><'+'/script>'
);
</script>
END
Community
  • 1
  • 1
Ilmari Karonen
  • 49,047
  • 9
  • 93
  • 153
  • 1
    This is a good answer and was my original solution to our problem, but our clients are using a lot of really slow computers and our onReady functions are executing before jquery loads when I try to include it like this. I am not saying it is impossible to rework our pages to accommodate this, but it is a very low priority. For a site that largely runs by itself with little maintenance, I just want a check that 5 years down the road, can see if google is still hosting jquery at this URL. – Will Reese Nov 02 '11 at 13:15
  • That's surprising. As far as I know, ` – Ilmari Karonen Nov 02 '11 at 18:22