homepage Welcome to WebmasterWorld Guest from 54.226.136.179
register, free tools, login, search, pro membership, help, library, announcements, recent posts, open posts,
Become a Pro Member

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

    
PERL Help on changing folder for file
How can I change/add the folder to file
fredfletcher



 
Msg#: 4360449 posted 11:04 am on Sep 9, 2011 (gmt 0)

Hello, I have a question.

I found a script on the Internet that lets people change from one language to another, in this case from English to French, but files must all reside in the same folder.

The script works, however, my files are in seperate folders in the root, being /e/ and /f/ respectively.

How can I implement a folder change with the corresponding filename of _e.shtml to _f.shtml and go to my root folder of a different language ie from my ROOT /e/file_e.shtml to /f/file_f.shtml ?

Here is the script in question which I tried to modify, but did not work:

# START Perl script

# get the URL for the Web page that called this script
$calling_page = $ENV{'HTTP_REFERER'};
# when the browser is Netscape, the referrer can contain the # and anchor name: REMOVE IT! (eg. [clf-nsi...]

if($calling_page =~ /(.*)\#.*/) {
# only take the first part up to the #
$calling_page = $1;}

# ignore any file that is not _e.shtml or _f.shtml and do nothing!

# is this an -eng.htm file?

if($calling_page =~ /_e\.shtml/) {
# replace the suffix
$calling_page =~ s/_e\.shtml/_f\.shtml/;
print "Location: $calling_page\n\n";

# then is this an _f.shtml file?

} elsif($calling_page =~ /_f\.shtml/) {
# replace the suffix
$calling_page =~ s/_f\.shtml/_e\.shtml/;
print "Location: $calling_page\n\n";}

# END Perl script


Thank you.

 

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4360449 posted 2:48 am on Sep 12, 2011 (gmt 0)

you need to capture the "file" part of the file path in your regular expression match and use that in the substitution.
if you were to define a pattern for /e/file_e.shtml what are the legal characters for "file"?

fredfletcher



 
Msg#: 4360449 posted 3:18 am on Sep 12, 2011 (gmt 0)

The complete script that I have right now works, but only if both _e.shtml and _f.shtml files reside in the same folder, which is not what I would like the script to do.

Since I have an "called-file_e.shtml" in the "English folder", would like the script to call the beginning of the same file name, but with _f.shtml in the root/f/called-file_f.shtml

Here is a working copy of my Perl script:

#!/usr/bin/perl
#
#
# get the URL for the Web page that called this script
$calling_page = $ENV{'HTTP_REFERER'};


if($calling_page =~ /(.*)\#.*/) {

# # only take the first part up to the #
$calling_page = $1;}

# ignore any file that is not _e.shtml or _f.shtml and do nothing!

# is this an _e.shtml file?

if($calling_page =~ /_e\.shtml/) {
# replace the suffix

$calling_page =~ s/_e\.shtml/_f\.shtml/;

print "Location: $calling_page\n\n";

# then is this an _f.shtml file?

}

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4360449 posted 4:35 am on Sep 12, 2011 (gmt 0)

what are the legal characters for "called-file" in "root/f/called-file_f.shtml"?

fredfletcher



 
Msg#: 4360449 posted 4:58 am on Sep 12, 2011 (gmt 0)

sorry, I don't quite know what you mean by "legal" characters.

For example of what I would like to have the script do:

Say I'm in www.mysite.com/E/myfile_e.shtml and I have a hyperlink with the perl script "language_change.pl" on the page where users could see that page in another language, say French for example.

Once they click on "language_change.pl" it would bring them to the same name of the page but with the _e.shtml extension. Going to the French page www.mysite.com/F/myfile_f.shtml / which is what the script presently does, but it won't work if the changed language file is in another folder, which is what I would like to know if we can add/prepend to $calling_page =~ s/_e\.shtml/_f\.shtml/; in between the _e\.shtml/ and _f\.shtml/ somewhere.

I tried: $calling_page =~ s/_e\.shtml/'s/^/$.\f'\/_f\.shtml/;

but it didn't work. It brings me to the same _e.shtml file.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4360449 posted 6:41 am on Sep 12, 2011 (gmt 0)

you need to define a capture group that contains the "file name" (by enclosing it in parentheses) in your regular expression so you can backreference it in the substitution string.
until you precisely define what characters can be in that "file name" you can't write a regular expression that works properly for all cases.

so far your examples include:
file
called-file
myfile
and perhaps "language_change.pl" as an example for "myfile", but i'm not clear if that's what you meant there.

so the "file" part of the referrer might contain (i'm guessing) alphanumerics, dashes and perhaps underscores and periods.
anything else?

fredfletcher



 
Msg#: 4360449 posted 12:17 pm on Sep 12, 2011 (gmt 0)

I appreciate your trying to help me, but I think I may have not explained myself properly. By "called-file_e.shtml", is just an example. The entire script I left above "Works". I will show you a structure of what I need to be "added" to the script, in order for it to work in a slightly different way.

language_change.pl is in cgi-bin
called-file_e.shtml is in public_html/e folder with link to language_change.pl file in the page to change called-file.f.shtml

called-file_f.shtml is in public_html/f folder

the working script shown above in previous comment will only work if called-file_f.shtml in in public_html/E folder

I copied called-file_f.shtml in public_html/E folder and it came up when I clicked on the .pl link from the called-file_e.shtml

That short script alone, WORKS, but only if "both" _e.shtml and _f.shtml files are in the "same" folder. The script needs to be modified, so that when the .pl link is clicked, it will go in public_html/f to find the _f.shtml file.

That's the best I can do to explain.

phranque

WebmasterWorld Administrator phranque us a WebmasterWorld Top Contributor of All Time 10+ Year Member Top Contributors Of The Month



 
Msg#: 4360449 posted 8:52 pm on Sep 12, 2011 (gmt 0)

$calling_page =~ s/\/e\/(the regular expression that precisely defines "called_file" that you haven't described yet in your problem statement)_e\.shtml/\/f\/$1_f\.shtml/;

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