Hello gregory-nisbet,

First, I don’t understand what you are trying to do. How does “pattern matching in languages like Haskell and OCaml” differ from Perl-style regular expressions work? Some explanation would be helpful here. At the very least, you should indicate the output you want to get from the call to match "bob", 45, sub { print $bob; }.

Second, the warning messages are telling you that the package-global variable $bob is being used without having been first initialised or even declared. A search through your code shows that $bob occurs once only, in the print statement within the anonymous subroutine passed to sub match. There is also the string, "bob", passed in to that subroutine, so I’m guessing you expect the variable to be somehow created, from the passed-in string, within that sub? It isn’t, and I don’t know why you think the while loop localisation you identify is relevant here?

Third, when you start sprinkling no strict 'refs' and no strict around your code to keep the compiler quiet, this is almost always a sign that your design is faulty.

Fourth, the use of prototypes in sub match ($$$) is probably a bad idea. Please study

Far More than Everything You've Ever Wanted to Know about Prototypes in Perl -- by Tom Christiansen

and then restrict yourself to using prototypes in only those situations where the “implicit context coercion” (in Tom Christiansen’s phrase) which they provide is actually useful.

Update: Amended sentence re “pattern matching”. Thanks to LanX, below.

Hope that helps,

Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,


In reply to Re: rough approximation to pattern matching using local by Athanasius
in thread rough approximation to pattern matching using local by gregory-nisbet

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.