Always check whether your regular expression matched before accessing the capture variables $1, $2 ...:

#!/usr/contrib/bin/perl # Parses a string in the format: # REFDATA_ERROR[type=<1>,system=<2>,category=<3>,code=<4>] # Prints out the four parameters, <1>, <2>, <3>, <4> sub parse_error { $params =~ m/REFDATA_ERROR\[type=(.*),system=(.*),category=(.*),code +=(.*)\]/ or die "The input string was invalid: '$params'"; print "$params\n"; print "$1\n"; print "$2\n"; print "$3\n"; print "$4\n"; }; # Main parse_error("REFDATA_ERROR[type=val1,system=val2,category=val3,code=va +l4]");

If you had used strict, Perl would have told you that your variable $params is undefined, as you seem to have a misconception on how parameters are passed around in Perl. The input parameters to your function live in the @_ array. Also, you are using a prototype on your sub declaration, which is in most cases a bad thing to do unless you know what you're doing (you don't).


In reply to Re: Parsing with Regular Expressions by Corion
in thread Parsing with Regular Expressions by arunhorne

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.