cosmicperl has asked for the wisdom of the Perl Monks concerning the following question:
Hi Guys,
I have a regular expression problem. Currently I'm doing it a long winded way:-
$systempath = "$ENV{'PATH_TRANSLATED'}"; $systempath =~ s/(\\[a-z0-9]*\.cgi)$//g; if ($&) {$extention = $&;} ## End if $systempath =~ s/(\\[a-z0-9]*\.pl)$//g; if ($&) {$extention = $&;} ## End if $systempath =~ s/(\\[a-z0-9]*\.asp)$//g; if ($&) {$extention = $&;} ## End if $extention =~ /\./; $extention = $';
I'm trying to create 2 variables from the $ENV{'PATH_TRANSLATED'}. One for the system path to the scripts folder. Another for the extention of the script, be it .cgi, .pl or .asp (I'm dabling with PerlScript for ASP as well).
As this is at the top of my scripts I'd like to minimise it. The full chunk is:-
BEGIN { if (($^O eq 'MSWin32') || defined($ENV{'OS'})) { ##### Get ENV unless ($ENV{'PATH_TRANSLATED'} || $ENV{'SCRIPT_FILENAME'}) { $aspmode = 1; $ENV{'PATH_TRANSLATED'} = $Request->ServerVariables('PATH_TRANSL +ATED')->item; $ENV{'SCRIPT_FILENAME'} = $Request->ServerVariables('SCRIPT_FILE +NAME')->item; } ## End unless $operatingsystem = 0; $osstring = "Win32 - NT, 2000, 2003"; $operatingsystemoldnt = 0; $systempath = "$ENV{'PATH_TRANSLATED'}"; unless ($systempath) { $systempath = "$ENV{'SCRIPT_FILENAME'}"; } ## End unless $systempath =~ s/(\\[a-z0-9]*\.cgi)$//g; if ($&) {$extention = $&;} ## End if $systempath =~ s/(\\[a-z0-9]*\.pl)$//g; if ($&) {$extention = $&;} ## End if $systempath =~ s/(\\[a-z0-9]*\.asp)$//g; if ($&) {$extention = $&;} ## End if $extention =~ /\./; $extention = $'; # $operatingsystemoldnt = 1; # $slash = '\\'; $slash = '/'; } ## End if else { $operatingsystem = 1; $osstring = "Unix - Linux"; $systempath = "$ENV{'SCRIPT_FILENAME'}"; $systempath =~ s/(\/[a-z0-9]*\.cgi)$//g; if ($systempath =~ /cgiwrap/) { $systempath = "$ENV{'PATH_TRANSLATED'}"; $systempath =~ s/(\/[a-z0-9]*\.cgi)$//g; } ## End if $slash = '/'; } ## End else ## $systempath = "systempath to your folder"; ## Enter the correct val +ue and un-comment this if you are having system path detection proble +ms push (@INC, "$systempath"); } ## End BEGIN
In case you can give me any other tips of minimising or improving.
Thanks!
janitored by ybiC: Balanced <code> tags around regex example
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Quick REGEXP question
by jimbojones (Friar) on Oct 31, 2004 at 01:51 UTC | |
|
Re: Quick REGEXP question
by ysth (Canon) on Oct 31, 2004 at 03:00 UTC | |
|
Re: Quick REGEXP question
by graff (Chancellor) on Oct 31, 2004 at 03:56 UTC | |
|
Re: Quick REGEXP question
by TedPride (Priest) on Oct 31, 2004 at 14:57 UTC | |
|
Re: Quick REGEXP question
by cosmicperl (Chaplain) on Nov 01, 2004 at 21:06 UTC | |
by jimbojones (Friar) on Nov 01, 2004 at 23:58 UTC |