homepage Welcome to WebmasterWorld Guest from 54.227.34.0
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Pubcon Platinum Sponsor 2014
Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
Forum Library, Charter, Moderators: coopster & jatar k & phranque

Perl Server Side CGI Scripting Forum

    
'Current' directory question
JohnF

5+ Year Member



 
Msg#: 3805094 posted 1:12 pm on Dec 11, 2008 (gmt 0)

Hi, I am writing a perl script to build Web pages for a photo gallery, and so it needs to link in with some existing stuff. The site already has some Javascript to do a fixed gallery and I want to make my perl use this same Javascript with the additional galleries.
I've written the perl, which mainkly just writes out the same HTML as the fixed gallery page, but substituting variables for the image file names etc. and that all works ok.
My problem is that the Javascript part is fairly impenetrable code and seems to be designed to work based on being in the sites root directory, or the directory it is excuted from. Does that make sense?
If I make a copy in the cgi-bin directory it no longer works (and I don't want multiple copies of the same thing).

When I run the perl script it executes based on the directory /root/cgi-bin being 'current'. This is ok. I had to adjust some paths in the menu script to ne absolute and not relative but it all works. The rather rmore complex image display Javascript doesn't though.

So, the question.... how can I make the perl script work as if it was running from the site root, rather than from the cgi-bin directory (without physically moving it)?

Thanks for any advice. It seems like a simple thing but I'm a bit baffled at the moment.

John

(I want all the perl stuff to be in cgi-bin, as per standard.)
(I could work on understanding the Javascript a bit more but it works as-is and I'd rather not spend the time as it was a downloaded thing by someone else.)

Directory structure is the usual (I believe):
root - all the html is in here
/cgi-bin
/vwd-scripts - part of the Javascript stuff mentioned
/something
/something else
etc.

 

janharders

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3805094 posted 7:38 pm on Dec 11, 2008 (gmt 0)

you could use mod_rewrite, if you're using apache and it is enabled (which it usually is)

let's say you script is in /cgi-bin/mygallery.pl and you want it to be run as /mygallery.htm, you'd have to put something like this in a file you call ".htaccess" (without the quotes of course):

RewriteEngine On
RewriteBase /
RewriteRule ^mygallery.htm$ /cgi-bin/mygallery.pl

that way, you can call /mygallery.htm and the script in the cgi-bin will get executed. would that solve it?

JohnF

5+ Year Member



 
Msg#: 3805094 posted 10:56 pm on Dec 11, 2008 (gmt 0)

Well, yes and no. ;-}

It did answer my question, for which - thanks. I have learnt something and it does simplify things, in terms of hooking into the exisitng setup.

However, I now have another problem (which may have been there all the time) which is that the Javascript to display the image is not being executed. The same static page works ok, but the same html built by my perl prog does not. I substituted the fairly complex Javascript to show the image for a very simple one that simply displayed an alert, and... nothing!
I'm pretty sure that the perl-genrated page is the same as the staic, I just cut and paste the code, and just changed a couple of bits for variables. The whole idea was that it should be the same as the static pages already present. I guess something must be different though.

I don't quite follow this, as the Javascript is called and execs (or should do) on the client, yes? And the server stuff should make no difference at that stage, and as long as the page generated is correct it should make no difference where it came from? True?

I'm a bit tired now so I'm giving up - and I'll have a fresh look tomorrow.

jwzumwalt

5+ Year Member



 
Msg#: 3805094 posted 11:45 pm on Dec 11, 2008 (gmt 0)

Another thing you may be fighting is most scripts are counting on the use of environment variables that are set from the original directory. This information contains referring path info, etc. When you run it, you will see that there are about 30-50 pieces of information being sent "invisibly" allowing with the http request. Here is a short program that gives some of this information...

#!/usr/bin/perl
##
## printenv -- Perl program shows environment variables
##

print "Content-type: text/plain\n\n";
foreach $var (sort(keys(%ENV))) {
$val = $ENV{$var};
$val =~ s¦\n¦\\n¦g;
$val =~ s¦"¦\\"¦g;
print "${var}=\"${val}\"\n";
}

JohnF

5+ Year Member



 
Msg#: 3805094 posted 12:12 am on Dec 12, 2008 (gmt 0)

I just tried making the perl generated code a static page, by doing 'view source' and cutting/pasting it into a new page.
This now looks almost identical to the original static pags that work, except for actual file names. However, the Javascript is still not executed.

Aaaagh! I'm definitely getting some sleep now!

janharders

WebmasterWorld Senior Member 5+ Year Member



 
Msg#: 3805094 posted 8:15 am on Dec 12, 2008 (gmt 0)

I don't quite follow this, as the Javascript is called and execs (or should do) on the client, yes? And the server stuff should make no difference at that stage, and as long as the page generated is correct it should make no difference where it came from? True?

absolutely. There must be a small bug that keeps the javascript from working. Did you try putting the alert()-test directly into the file? e.g.
<script language="javascript">
alert('hooray, javascript works!');
</script>

If that'll do it's most likely a problem in the way you include the javascript. Also, if you're using Mozilla, try bringing up the Error Console (Ctrl + Shift + J in the old german version I'm using, should be in the Tools-menu, at least I think it's called Tools in the english versions, the one where settings etc sit), it may contain some hints.

JohnF

5+ Year Member



 
Msg#: 3805094 posted 12:57 pm on Dec 12, 2008 (gmt 0)

Thanks to those offering advice, esp janharders. I learnt a couple of things through this.
What I did in the end was to dump the image display Javascript and substitute my own, which works fine. The original Javascript came from a wysiwyg package which generates, like many of these things, complicated and impenetrable code and life's too short to dig into that unless it's unavoidable.

This is a great board. I'll be back.

Thanks, John

rocknbil

WebmasterWorld Senior Member rocknbil us a WebmasterWorld Top Contributor of All Time 10+ Year Member



 
Msg#: 3805094 posted 2:52 pm on Dec 12, 2008 (gmt 0)

<snicker> Yesterday I was going to propose the exact same solution janharder did - but wasn't sure if that addressed the problem.

If your JS is not executing, try using absolute paths everywhere. Example:

<script type="text/javascript" src="/js-gallery.js"></script>

Within JS, anywhere you see references to the directory containing the images, do the same thing. You may have to "chase down" the image references. For example,

document.write('<img src="'+myimgVariable+'">');

change to

document.write('<img src="/images/'+myimgVariable+'">');

The forward slash means always start at domain root. Combined with mod_rewrite, this always gives you a common place to start and will always work.

fairly impenetrable code

You do have a copy, right, not an external script reference? If you can edit it, you can fix it.

JohnF

5+ Year Member



 
Msg#: 3805094 posted 3:09 pm on Dec 12, 2008 (gmt 0)

Well, it's all fine now - thanks for the advice.

By impenetrable code I meant just difficult to understand. It is presented as a solid block of text, without formatting and white space. I'm sure I could understand it , and debug it, if I put the time in, but the original plan was to just hook into it (as it was already in use). In the event it turned out easier to just use some other image display code I already had (written by me originally) and adapt it slightly.

Global Options:
 top home search open messages active posts  
 

Home / Forums Index / Code, Content, and Presentation / Perl Server Side CGI Scripting
rss feed

All trademarks and copyrights held by respective owners. Member comments are owned by the poster.
Home ¦ Free Tools ¦ Terms of Service ¦ Privacy Policy ¦ Report Problem ¦ About ¦ Library ¦ Newsletter
WebmasterWorld is a Developer Shed Community owned by Jim Boykin.
© Webmaster World 1996-2014 all rights reserved