-
Notifications
You must be signed in to change notification settings - Fork 560
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Feature request: builtin array holding ($1,$2,$3,...) #13412
Comments
From @epaCreated by @epaIt would be useful to have a builtin array variable which holds the Suppose the builtin array is called @CAPTURE. Then $_ = 'abc'; There is a fair bit of boilerplate code with ($1,$2,$3,$4,$5) up to Perl Info
|
From @HugmeirOn Tue, Nov 12, 2013 at 2:26 PM, Ed Avis <perlbug-followup@perl.org> wrote:
Eh, this looks like a feature better suited for CPAN. You can already package Capture { tie @CAPTURE, "Capture"; Although a proper implementation could avoid ties, I imagine. Also, rather |
The RT System itself - Status changed from 'new' to 'open' |
From @demerphqOn 12 November 2013 20:11, Brian Fraser <fraserbn@gmail.com> wrote:
Ugh!
$1 is a tie itself. So @{^CAPTURE} would be a tie as well. The Yves -- |
From @demerphqOn 12 November 2013 18:26, Ed Avis <perlbug-followup@perl.org> wrote:
But there is: @CAPTURE= /(.)(.)(.)/ or die; IOW, in scalar context a regex match returns a list of items captured, Why would having a magic var for this be better? cheers, |
From @epaYves Orton wrote:
I mentioned one reason in the original report: in s/// you don't have access to this. Another reason is that m// in list context makes it hard to distinguish between a $_ = 'abc'; Both print '1'. So, if you have some code like if (/$re/) { do_something($1, $2, $3, $4, $5, ...) } there is not an obvious way to turn it into the form you suggest: @CAPTURE = /$re/; I am thinking of regular expressions which are specified in a configuration table, But if @CAPTURE were available as a builtin, you could simply say if (/$re/) { do_something(@CAPTURE) } -- ______________________________________________________________________ |
From @epaI would add that in general the reason to prefer an array @CAPTURE over individual $1, $2, $3 Similarly, think of @ARGV. We could do without that and instead have $ARG1, $ARG2, $ARG3 etc. In the case of @CAPTURE, the array would have to be read-only (so you can't shift it, etc) but Has nobody else here ever written code like foo($1, $2, $3, $4, $5)? And never wished there were -- ______________________________________________________________________ |
From @emazepOn 2013-11-15 11:55, Ed Avis wrote:
You can currently do: if ( my @CAPTURE =()= /$re/ ) { say @CAPTURE } P.S. Shouldn't be @CAPTURE spelled @CAPTURES? -Emanuele |
From @demerphqOn 15 November 2013 13:02, Emanuele Zeppieri <emazep@gmail.com> wrote:
Style preference. I tend to prefer to use singular for arrays where I if ($CAPTURE[1]) { } reads better to me than if ($CAPTURES[1]) { } Yves -- |
From @demerphqOn 15 November 2013 11:55, Ed Avis <eda@waniasset.com> wrote:
Color me convinced. :-) If FC doesnt beat me to it (as he so often does) I will add support for this. Let the debate begin about the name. I wont participate except to note @+ and then @CAPTURE(S) as well. I leave that for other to agonize over. Yves -- |
From @epaEmanuele Zeppieri wrote:
I don't think that works: $_ = 'abc'; For me this prints no -- ______________________________________________________________________ |
From @epaThank you Yves for offering to implement this. You can call it @BIKESHED if you want, and I'll still be delighted... -- ______________________________________________________________________ |
From @LeontOn Fri, Nov 15, 2013 at 11:19 AM, demerphq <demerphq@gmail.com> wrote:
Now if only we had array vtables, we could have implemented this in C Leon |
From @demerphqThe = () = is superfluous. Yves On 15 November 2013 13:08, Ed Avis <eda@waniasset.com> wrote:
-- |
From @hvdsEd Avis <eda@waniasset.com> wrote: I haven't, but I have frequently written code that makes the relevant array I'd rather see this prototyped on CPAN first, if only because IMO we have But I also feel that while the magic variable approach maybe made sense # assuming a new //mauve option that means "give me an object" (Sorry I haven't really been following this discussion, I'm in the middle Hugo |
From @epaI agree that a 'match object' might be a cleaner way to access properties of a regexp match. -- ______________________________________________________________________ |
From @demerphqOn 15 November 2013 15:19, Ed Avis <eda@waniasset.com> wrote:
I agree. We introduced %- and %+ without difficulty. I agree with hv -- |
From @emazep[Sorry for the private mail Ed, it was intended for the list] On 2013-11-15 13:08, Ed Avis wrote:
Indeed you are right, sorry again :-) However, give me a second attempt: $_ = 'abc'; sub test { sub f { say join '-', @_ } test qr/abc/; # match w/o captures: f is called with () What's the missing case? |
From ema.zep@libero.itOn 2013-11-15 13:02, Emanuele Zeppieri wrote:
No, it doesn't work, sorry! |
From @emazepOn 2013-11-15 13:08, Ed Avis wrote:
Indeed you are right, sorry again :-) However, give me a second attempt: $_ = 'abc'; sub test { sub f { say join '-', @_ } test qr/abc/; # match w/o captures: f is called with () What's the missing case? |
1 similar comment
From @emazepOn 2013-11-15 13:08, Ed Avis wrote:
Indeed you are right, sorry again :-) However, give me a second attempt: $_ = 'abc'; sub test { sub f { say join '-', @_ } test qr/abc/; # match w/o captures: f is called with () What's the missing case? |
From cm.perl@abtela.comLe 15/11/2013 11:55, Ed Avis a écrit :
this is a problem only it you don't know beforehand the number of
if (my @CAPTURE = /$re/) {
Again, can you provide an example ? I generally use either named capture my ($header, $param1, $param2); or named targets, as in if (my ($header, $param1, $param2) = m/..../) { @CAPTURE as proposed here strikes me as too rigid for me in most cases
--Christian |
From cm.perl@abtela.comLe 15/11/2013 11:55, Ed Avis a écrit :
You can always add a capturing group, for instance an empty lookahead : Taisha:~/tttmp $ perl -E 'for (@ARGV) { print; if (my @CAPTURE = --Christian |
From @emazepOn Fri, Nov 15, 2013 at 1:04 PM, demerphq <demerphq@gmail.com> wrote:
On a second thought, I see at least three points where you are right: 1. it's a question of style preference; |
From @emazepOn 2013-11-15 15:27, Ed Avis wrote:
Well, here is an alternative solution, which permits to save the $1 check The trick is to wrap the given regex in extra brackets: if ( my @CAPTURE = /($re)/ ) { do_something(@CAPTURE) } and then shift away the artificial capture in the called subroutine: sub do_something { (or before the sub call). Just to realize how much simpler it would be if your proposal were |
From @emazepOn 2013-11-16 00:21, Emanuele Zeppieri wrote:
And this further refinement avoids the shift too: if ( (undef, my @CAPTURE) = /($_[0])/ ) { do_something(@CAPTURE) }
No less ugly however (and potentially inefficient too), so the above is |
From @emazepOn Sat, Nov 16, 2013 at 1:10 AM, Emanuele Zeppieri <emazep@gmail.com> wrote:
Well, this avoids the potential inefficiency at least :-) if ( (undef, my @CAPTURE) = /()$_[0]/ ) { do_something(@CAPTURE) } |
From @tamiasOn Sat, Nov 16, 2013 at 12:21:18AM +0100, Emanuele Zeppieri wrote:
You should be aware that you cannot depend on any particular capture being $_ = "b"; if (my @CAPTURE = /$re/) { Ronald |
From @emazepOn Sat, Nov 16, 2013 at 6:46 AM, Ronald J Kimball <rjk@tamias.net> wrote:
The error was mine of course. However it's just a matter of finding a reliable indicator that a if ( my @CAPTURE = /$re/ ) { Does $#- > 0 serve the scope or are there exceptions? |
From @druud62On 2013-11-15 11:55, Ed Avis wrote:
@CAPTURE = m/$re/ and s//foo(@CAPTURE)/e But what if the g-modifier is used, AoA? perl -Mstrict -MData::Dumper -wle ' -- |
From @maukeOn 12.11.2013 18:26, Ed Avis (via RT) wrote:
Just FYI, that functionality is available as submatches() in Data::Munge. -- |
From @rjbs* hv@crypt.org [2013-11-15T08:44:17]
I agree. I threw this lousy code together a few months ago and it made me use strict; sub matches { return unless $str =~ $qr; $result->{capture_hash} = { %- }; bless $result, $class; sub capture { sub capture_list { sub captures { sub pre_match { sub match { sub post_match { 1; It would also be less surprising for programmers coming from just about -- |
From perl5-porters@perl.orgRicardo Signes wrote:
Why match? Why not just qr(...)->($str)? (Then we can forget smart-match altogether, since ->(...) is our match |
From @kentfredricOn 17 November 2013 09:53, Father Chrysostomos <sprout@cpan.org> wrote:
Maybe because it lends itself to later enhancements, ie: qr(...)->replace(sub{ }); May be nicer s{...}{ code }e; ( Especially seeing this syntax is more recognised by code -- |
From @tamiasOn Fri, Nov 15, 2013 at 02:19:38PM +0000, Ed Avis wrote:
Ronald |
From @epa
I'd be fine with that. It does remove the off-by-one oddity compared to $1, etc. -- ______________________________________________________________________ |
From @emazepOn Sat, Nov 16, 2013 at 12:01 PM, Emanuele Zeppieri <emazep@gmail.com> wrote:
Or rather: if ( my @CAPTURE = /$re/ ) { otherwise the case of a successful match but no matching subgroups Sorry for the noise! |
From @rjbs* Kent Fredric <kentfredric@gmail.com> [2013-11-16T16:16:46]
qr(..)->(...) does not rule out qr(...)->replace(...) -- |
From @demerphqOn 22 November 2013 04:21, Ricardo Signes <perl.p5p@rjbs.manxome.org> wrote:
The rules for parsing the right side of a regex are special cased. I fear adding support for the same special cases in a method call is s/(f)o(o)/$1$2/ how would you write that with method call notation without requiring qr/(f)o(o)/->replace("$1$2"); isn't going to work out of the box at all. Yves -- |
From @kentfredricOn 22 November 2013 20:55, demerphq <demerphq@gmail.com> wrote:
That would of course not work. It would have to be implemented in terms of qr/(f)o(o)/->replace(sub { Where internally, it did : s{(f)o(o)}{ $code->() }e At least, thats what I'd expect to be required given the current state Now maybe qr/(f)o(o)/->replace('$1$2'); Could be made work, just the surprisingness of that seems dangerous, -- |
From @kentfredricOn 22 November 2013 23:15, Kent Fredric <kentfredric@gmail.com> wrote:
Actually, on second thought, even that would not always be what you qr/(f)o(o)/->replace(sub { And that could lend itself to doing something like: $match->iterpolate("%1helloworld%2"); With the communicative property: if( my $result = qr/(f)o(o)/->match($string) ) { Essentially, making a sane way to pass state around from match results I don't really know, I must be tired or something. |
From @demerphqOn 22 November 2013 11:24, Kent Fredric <kentfredric@gmail.com> wrote:
Exactly. The delayed interpolation of the rhs of a s/// is hard to fit The only way to do so is require people pass in a closure. IMO hardly Yves -- |
From @epaI believe this has now been implemented as @{^CAPTURE} in perl 5.26 (thanks demerphq!) so the bug can be closed. |
From @jkeenanOn Fri, 09 Jun 2017 15:46:33 GMT, ed wrote:
Confirmed. ##### $ cat 120521-capture.pl $_ = 'abc'; $_ = 'def'; $ perl 120521-capture.pl Marking ticket Resolved. -- |
@jkeenan - Status changed from 'open' to 'resolved' |
Migrated from rt.perl.org#120521 (status was 'resolved')
Searchable as RT120521$
The text was updated successfully, but these errors were encountered: