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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.