Stories
Slash Boxes
Comments
NOTE: use Perl; is on undef hiatus. You can read content, but you can't post it. More info will be forthcoming forthcomingly.

All the Perl that's Practical to Extract and Report

The Fine Print: The following comments are owned by whoever posted them. We are not responsible for them in any way.
 Full
 Abbreviated
 Hidden
More | Login | Reply
Loading... please wait.
  • > Note that $_ within the closure refers to
    > this state object, not the original search
    > string. If you search on the state object,
    > however, it pretends that you wanted to
    > continue the search on the original string.

    What if I wanted to do something else to $_ ?

    Hmm... More precisely, I guess I'm wondering about two
    separate things here.

    1) What if I wanted to do something to what
       had been $_ before we were within the closure?

    2) What if I wanted to do something to the
       current search string?

    Regarding #1, I can see that it just might not be
    possible. If you intend to be accessing the old $_
    within the regex, then you need to be sure to assign
    that $_ to something else before entering the regex.

    Alternatively, if it's not too ugly, perhaps the $_
    state object could have an _ method which returns
    a reference to what $_ had been before the regex?

        m:i/^\#{._.comment()}$/ for @stringifiable_objects;

    Regarding #2, the above example just happens to have
    $_ as the current search string. However, in some
    cases, $_ will be busy doing other things, and the
    current search string will be in some other variable.
    In the case of a for my $x (@a) {...} loop, we know
    that the string is $x, so our regex could be told to
    tweak $x directly.

    However, we won't always be building our regexen
    right near where they get used. In fact, they could
    be quite far away. The regex could be used in several
    places and need to tweak the current search string in
    different bits of code where that search string has
    different names in different instances.

    Can the regex state object have a method that returns
    a reference to the current search string?

        .string
        .current
        .target
        .cur
        .str

    or something like that? In many cases, this would be
    synonymous with ._ (or whatever it would be called),
    but in cases where the current search string is not
    $_ (from before entering the regex), then the two
    would be different.

    -matt
    • By the time you get into a regex, you're always
      dealing with $_.  Even =~ behaves as a topicalizer
      for its right side.  So, while the $_ inside a
      closure is the search state object, it's always
      related intimately to the outer $_, which is always
      an alias for whatever you're currently searching.
      Any string operations on the inner $_ should be
      delegated to the original string.  Providing an
      explicit method to get at that string should be a
      no-brainer, though if you mess with the string,
      there's no guaran
      • > though if you mess with the string,
        > there's no guarantee that the regex
        > will continue to make sense.

        I kinda figured that would be the case. I was thinking
        of methods on strings or stringifiable objects which
        don't change the string. A trivial example might be
        getting the length of the target string, when the regex
        may not know the name of the variable containing the
        string to which is being applied.

        It sounds like it won't need to know, which sounds good!

        -matt