patricia_e has asked for the wisdom of the Perl Monks concerning the following question:

Hi,

I'm extremely new to perl. so I apologize in advance for the newbie question...

Our sysadmin recently ported our website onto a new unix box. I am updating scripts on this new development site.

The script I'm currently updating is one that asks users for a number which corresponds to a job number. This entered job number has a corresponding file in a certain directory on the new server. Each file's name is like this:

70330.php
70340.php
etc....

The html pages asks the user to input what job number they are searching for and then looks to see if that a file exists that corresponds to that job number. If it does, it displays it. If not, it returns an error message.

The oldest version of this is:

#!/usr/local/bin/perl unshift(INC, "/usr/local/lib"); require("perl-forms-lib"); &form'Parse(); print "Content-type: text/html\n\n"; if ($in{'jobcode'}) { &displayByJobNumber($in{'jobnumber'}); } else { &search($in{'search'}); } sub displayByJobNumber { ($jobNumber) = @_; $file = $jobNumber . ".php"; if (-f $file) { $fileContents = `cat $file`; print $fileContents; } else { print <<EOM; There is no job with that job number. Pick another. EOM } } # end sub displayByJobNumber

**** this doesn't work on my new server. I suspected the cat unix command for some reason. I got on the Chatterbox feature of perl monks where the members steered me to LWP::UserAgent.

After that, I changed my code to read like this:

#!/usr/local/bin/perl unshift(@INC, "/usr/local/lib"); require LWP::UserAgent; use CGI qw(param); require("perl-forms-lib"); &form'Parse(); print "Content-type: text/html\n\n"; if ($in{'jobcode'}) { &displayByJobNumber($in{'jobnumber'}); } else { &search($in{'search'}); } sub displayByJobNumber { ($jobNumber) = @_; $file = "../../06_jcl/jobdesc/" . $jobNumber . ".php"; my $ua = LWP::UserAgent->new; $modfile = $file."?var=".$jobNumber; my $response = $ua->get('<url of directory location on my developm +ent server here>'); if (-f $file) { print $response->content; } else { print <<EOM; There is no job with that job number. Pick another. EOM } } # end sub displayByJobNumber

****** This didn't work.

It DID work when I put the url of the old server into the line:

my $response = $ua->get('<url of directory location on old server h +ere>');

Also, when I put the url of my development box back in there without the last few nested directories (just put in the bare url of the general site), it displayed a web page that might be on the server BUT was definitely not one of my pages. It looked like it was a page from a different website that has some space on the server.

Do you have any idea why this might be?

Could it have to do with the fact that our url is somewhat temporary on this development site? It will eventually be given the official url once we go live.

Thanks in advance for any insights into this problem.

janitored by ybiC: Balanced <readmore> tags around longish codeblock, as per Monastery convention

Replies are listed 'Best First'.
Re: LWP::UserAgent with development website
by astroboy (Chaplain) on Sep 17, 2004 at 23:33 UTC
    replace
    if (-f $file) { $fileContents = `cat $file`; print $fileContents; }
    with
    if (-f $file) { open (FH, "<$file"); print <FH>; }
      if (-f $file) { open (FH, "<$file"); print <FH>; }
      That displays only the first line of the file. I think the OP wants to display the whole file. Did you mean print while <FH>;? Sorry, I didn't realize <> loads the whole file as an array in list context.
Re: LWP::UserAgent with development website
by TheHobbit (Pilgrim) on Sep 18, 2004 at 12:32 UTC
    Hi,

    Just an idea, but it will not surprise me if it was the good one. The problem could be in using a relative path to get the file. The path is relative to the current working directory of the Apache process handling your request, and this will almost certainly be not what you think it is. Try replacying the relative path with the absolute one. Moreover, using relative paths is almost always a BadThing, exactly because you cannot know what would you get at runtime.

    Using cat, while strictly possible, may not be advisable. The slurp solution proposed, eg.

    open FOO,$path; print <FOO>

    are better, but they ask for the interpreter to slurp the whole file before sending it. If the file is relatively small this is not a problem, and the above solution is good, even if this one

    { local $/; open FOO,$path; $text = readline FOO; close FOO; } print $text;

    is better IMHO.

    But if the file is big then:

    print while (<FOO>);

    will likely be a better idea.

    Hoping it wil help.

    Greetings


    Leo TheHobbit