OeufMayo is exactly correct, but just to add a little detail regarding your logic: here's your code rearranged into a more traditional format:
if ($in{username} =~ /^(\[a-zA-Z]{2})/ || ' ') { $dirtype = $1 || 'other'; }
First of all, the "or" in the "if" statement will never trigger because "if" always tests for "truth." In other words, the "if" is saying "if the regular expression is true OR the string " " is true." Unfortunately, " "is NEVER true in Perl, so if your regex doesn't match the if is skipped entirely.

Second of all, IF the regular expression DOES match (triggering the code between the brackets and populating $1 at the same time) THEN in your case $1 is always guaranteed to be true (because it will contain two A-Za-z letters. Letters always evaluate to "true." Because the first expression in your short-circuit operation ($1) is always true, your program will never get to the 'other' value.

This is why OeufMayo resorted to a ternary operator, which is just a short-hand way to write another if/then statement. Here it is rewritten for clarity:

my $dirtype; if ( $in{username} =~ /^([a-zA-Z]{2})$/ ) { $dirtype = $1; } else { $dirtype = 'other'; }
This way the logic is all straigthened out.

And then there's the backslash in your regex. :-D

Gary Blackburn
Trained Killer


In reply to Re: Expression, but is it regular? by Trimbach
in thread Expression, but is it regular? by web-yogini

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.