I'm trying to assign a variable a regex, then pass the varibale to a subroutine, then use the passed regex in the subroutine to find a value in a line of text and then return that value. I've tried quotes, single quotes, using quotes around the passed variable in the sub, not using quotes. I've found that since there are spaces in the string I'm searching I have to use to 2 escapes because 1 of the escapes gets stripped. Here's what it looks like:
$val1 = 'Torque\\sDriver\\sCS\#:\\s*(\\D{2}\\d{4})'; $tq = &val_search($val1);
and then in the sub:
sub val_search { my $ret_val = 0; { ($ret_val) = ($line =~ /$_[0]/); print "ret_val = ", $ret_val, "\n"; return $ret_val; } }
I've tried every combination I can think of, does anyone have an idea of how to properly do this?

In reply to passing a regex to a sub by mark4444az

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.