Skip to content

Conversation

@scottchiefbaker
Copy link
Contributor

I was bit by a bug wherein I was accessing @{^CAPTURE} on Perl v5.16 which does not have this variable. This PR adds some documentation to allow a user a work-around if they are in the same situation.

@scottchiefbaker
Copy link
Contributor Author

I'm not sure if this is the type of thing we want/need in the documentation, but it would have saved me about an hour of hair pulling if it were documented.

@jkeenan jkeenan changed the title DOCUMENTATION: Add a work-around for older Perl's that need @{^CAPTURE} Add a work-around for older Perl's that need @{^CAPTURE} Nov 30, 2023
@scottchiefbaker
Copy link
Contributor Author

@mauke pointed out on IRC that this functionality is also in Data::Munge. Their implementation is a little hacky, shorter, and harder to read. For completeness I'm including it here:

sub get_captures {
    no strict 'refs';
    map $$_, 1 .. $#+
}

We could include this in the documentation if my function is too long/wordy.

pod/perlvar.pod Outdated
function immediately after your regexp.

sub get_captures {
my $str = $&;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

At least in older Perls, the use of $& slows down all regex matches everywhere in the program.

pod/perlvar.pod Outdated
my @captures = ();

# Build array using the offsets from @- and @+
for (my $i = 1; $i < @-; $i++) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Why use a C-style for loop here? This is a simple counting loop, so

for my $i (1 .. $#-) {

expresses the idea more directly (and slightly more efficient).

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Agree to disagree here... $#- is too much punctuation and not as easily understood.

I've been using Perl for 20+ years and I just learned about # syntax in arrays today.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If you don't like $#-, @- - 1 is the same thing. (Doesn't really look better with @-, though, IMHO).

pod/perlvar.pod Outdated
my $len = $end - $start;

my $str = substr($str, $start, $len);
push(@captures, $str);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

my @captures;
for (RANGE) {
    push(@captures, ...)
}

could also be written as

my @captures = map {
    ...
} RANGE;

@scottchiefbaker
Copy link
Contributor Author

Upon further reflection I think it makes sense to use the format that @mauke proposed in Data::Munge. I would however like to expand it slightly so it's more clear and readable for novices. Since the intention is to put it in the documentation I think readability is key here. Here is my expanded version:

sub get_captures {
    no strict 'refs';

    my $last_idx = scalar(@-) - 1;
    my @arr      = 1 .. $last_idx;
    my @ret      = map { $$_; } @arr;

    return @ret;
}

@khwilliamson
Copy link
Contributor

I have quibbles about the commit messages, in particular the titles. A title should have enough information for someone scanning a log of commits to be able to quickly determine if this is potentially a commit that has bearing on what they're scanning for. For that, the title should include what the commit pertains to. For small specific changes like this, noting in the title that it is perlvar that is affected will tell me immediately if I should look further into it or not. I have adopted the convention for myself to place the file name at the beginning of the message, like

perlvar: Use a faster/shorter version of get_captures()

Something that affects more things wouldn't have the file name, but should indicate what area, as explicitly as possible, is affected.

A commit message should answer the classic questions "Who, what, when, where, and why". The who and when are automatically filled in for you by the system, and appear adjacent to the title, but they are less important than the other three. A title should give at least a hint towards those. So "perlvar: Fix a typo" is better than "perlvar: Fix a word" because it more explicitly says what is wrong that needs to be fixed.

@scottchiefbaker
Copy link
Contributor Author

@khwilliamson all that makes sense to me. Usually that all gets corrected on the squash and merge phase. Unless you'd like me to rebase and squash all these down to a single commit before then.

@khwilliamson
Copy link
Contributor

khwilliamson commented Dec 6, 2023 via email

@scottchiefbaker
Copy link
Contributor Author

Ping... what's next on this? Is this something we need in the docs?

@khwilliamson
Copy link
Contributor

I think now is a good time to squash and make sure the commit msg is good. Then I would look at the full result.

@scottchiefbaker
Copy link
Contributor Author

@khwilliamson I have squashed

@khwilliamson khwilliamson merged commit 0ee1ec4 into Perl:blead Dec 22, 2023
@scottchiefbaker
Copy link
Contributor Author

@khwilliamson Thank you sir!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants