LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Linux - Newbie (https://www.linuxquestions.org/questions/linux-newbie-8/)
-   -   Help needed on language change-adding folder in expression (PERL) (https://www.linuxquestions.org/questions/linux-newbie-8/help-needed-on-language-change-adding-folder-in-expression-perl-902497/)

fredfletcher 09-11-2011 09:09 PM

Help needed on language change-adding folder in expression (PERL)
 
Hello,

I'd really like some help on this, a PERL-CGI script.

How can I add the server's root folder of "my_folder" to this:

~ s/_e\.shtml/_f\.shtml/

This searches all occurences of _e.shtml and replaces them with _f.shtml (it being in the same folder).

Yet, in between that, I would like to add the server's root of "french" folder in there.

For example; and yes I know it's not right:

~ s/_e\.shtml/(MY_FOLDER)_f\.shtml/

I would really appreciate anyone's help on this one, as I can't seem to get it right, with what I know so far.

The complete script is below, which works but only if both language files (file_e.shtml and file_f.shtml) reside in the same folder. I need to be able to have the script work, so that it will get the 'french' file (file_f.shtml) in the /f/ folder (/ being the root).

#!/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?

}

chrism01 09-12-2011 06:14 PM

As no-one has answered, I'll give it a crack.
TBH, I'm not entirely clear on what you're doing without a fuller example of the data (ie src & tgt filepaths).
However, have a look at this snippet (tested), which I believe may be the answer, or at least close.

Code:

$var1='/dir/e/x_e.shtml';
print "$var1\n";

$var1 =~ s/_e\.shtml/_f\.shtml/ ;
print "$var1\n";

$var1 =~ s:/e/:/f/: ;
print "$var1\n";

# output is
/dir/e/x_e.shtml
/dir/e/x_f.shtml
/dir/f/x_f.shtml

Note that the s/// notation in Perl is borrowed from sed, and like sed, you can use a different pattern separator if the string(s) use the '/' char eg use ':' instead, to avoid LTS https://secure.wikimedia.org/wikiped...hpick_syndrome

HTH

fredfletcher 09-12-2011 06:22 PM

Hello and thanks for replying. The entire script is above; the script is to show the user, a link to the same page but in a different language when they click on the .pl link; for example "This page in English" from a French page (welcome_f.shtml) which would lead to the English equivalent of (Welcome_e.shtml) but in a different folder, being either /e/ or /f/ (e-English and f-French). TBH, I don't know why the "print" statement is doing in my original script lol. That's probably why it might be confusing.

chrism01 09-12-2011 06:31 PM

Well my example changes the filename and then changes the dir path for you.
Does that not work, if not why not?
I always advise testing as a non-CGI script first to get make it easier to get the processing code correct, then use the same method inside the CGI version.

fredfletcher 09-13-2011 08:29 AM

I'm a "newbie" in a way, but can modify scripts to a certain point, just can't write them from scratch. Am trying to figure out how to implement your suggestion into my existing script. Any chance on a total re-write with my example above?

chrism01 09-14-2011 01:15 AM

Not really ... I don't do CGI ;)

Anyway, just remember that what you are fundamentally asking about (I believe) is really just string shuffling; it doesn't matter that they contain dir/file paths. So, just write a basic script like mine and experiment until you 'get it'
Here are some good Perl links
http://perldoc.perl.org/
http://www.perlmonks.org/?node=Tutorials


and ask again if you get stuck


All times are GMT -5. The time now is 01:15 AM.