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
In reply to Quick REGEXP question by cosmicperl
| For: | Use: | ||
| & | & | ||
| < | < | ||
| > | > | ||
| [ | [ | ||
| ] | ] |