Skip Menu |

This queue is for tickets about the Perl-Tidy CPAN distribution.

Report information
The Basics
Id: 79947
Status: resolved
Priority: 0/
Queue: Perl-Tidy

People
Owner: Nobody in particular
Requestors: rjray [...] blackperl.com
Cc:
AdminCc:

Bug Information
Severity: Wishlist
Broken in: 20120714
Fixed in: (no value)



Subject: Enhancement: Have "cuddled-else" logic applied to try/catch/finally blocks
Currently, try/catch/finally blocks are not recognized the way if/elsif/else blocks are, and lead to inconsistent line-breaks when --cuddled-else is set. It would be nice if they were cuddled as well. See the attached files: first file (t.pl) is a short example with both an if-block and a try-block. Second file (t.pl.tdy) is what it currently looks like after perltidy. Third file (t.pl.tdy2) is what I think it should look like. This could be tricky, as there are competing exception-handling packages on CPAN, some with slightly different syntax. Try::Tiny, for example, does not take a class name after "catch", and only offers try/catch/finally. Error, in contrast, takes a class name after "catch", and offers try, catch, except, otherwise and finally. Randy -- Randy J. Ray rjray@blackperl.com randy.j.ray@gmail.com
Subject: t.pl.tdy
Download t.pl.tdy
application/octet-stream 294b

Message body not shown because it is not plain text.

Subject: t.pl
#!/usr/bin/env perl use strict; use warnings; use Error qw(:try); sub foo { if (1) { do_something(); } else { do_something_else(); } try { something(); } catch Error with { something_else(); } finally { foo(); } }
Subject: t.pl.tdy2
Download t.pl.tdy2
application/octet-stream 286b

Message body not shown because it is not plain text.

Subject: Re: [rt.cpan.org #79947] Enhancement: Have "cuddled-else" logic applied to try/catch/finally blocks
Date: Tue, 2 Oct 2012 06:27:37 -0700
To: bug-Perl-Tidy [...] rt.cpan.org
From: Steven Hancock <perltidy [...] users.sourceforge.net>
Randy, Thanks, I'll look at it. Steve On Mon, Oct 1, 2012 at 10:50 AM, Randy J Ray via RT < bug-Perl-Tidy@rt.cpan.org> wrote: Show quoted text
> Mon Oct 01 13:50:51 2012: Request 79947 was acted upon. > Transaction: Ticket created by RJRAY > Queue: Perl-Tidy > Subject: Enhancement: Have "cuddled-else" logic applied to > try/catch/finally > blocks > Broken in: 20120714 > Severity: Wishlist > Owner: Nobody > Requestors: rjray@blackperl.com > Status: new > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=79947 > > > > Currently, try/catch/finally blocks are not recognized the way > if/elsif/else blocks are, and lead to inconsistent line-breaks when > --cuddled-else is set. It would be nice if they were cuddled as well. > See the attached files: first file (t.pl) is a short example with both an > if-block and a try-block. Second file (t.pl.tdy) is what it currently > looks like after perltidy. Third file (t.pl.tdy2) is what I think it > should look like. > > This could be tricky, as there are competing exception-handling packages > on CPAN, some with slightly different syntax. Try::Tiny, for example, > does not take a class name after "catch", and only offers > try/catch/finally. Error, in contrast, takes a class name after "catch", > and offers try, catch, except, otherwise and finally. > > Randy > -- > Randy J. Ray > rjray@blackperl.com > randy.j.ray@gmail.com >
From: ulrik.mikaelsson [...] gmail.com
Any progress on this? AFAICT, the original example is a bit flawed. Correct usage of the Error module is try { do_some_stuff(); } catch IO::Error with { # Oh noes! } finally { # Oh well }; <- semi-colon crucial or anything can happen Perltidy seems to handle this correctly. HOWEVER; The Error CPAN-module is no longer recommended, due to bugs in corner-cases. Recommended modules are now instead, for example TryCatch (http://search.cpan.org/perldoc?TryCatch) TryCatch does not expect the semi-colon and breaks with Perltidy. On Tue Oct 02 09:27:56 2012, SHANCOCK wrote: Show quoted text
> Randy, > Thanks, I'll look at it. > Steve > > On Mon, Oct 1, 2012 at 10:50 AM, Randy J Ray via RT < > bug-Perl-Tidy@rt.cpan.org> wrote: >
> > Mon Oct 01 13:50:51 2012: Request 79947 was acted upon. > > Transaction: Ticket created by RJRAY > > Queue: Perl-Tidy > > Subject: Enhancement: Have "cuddled-else" logic applied to > > try/catch/finally > > blocks > > Broken in: 20120714 > > Severity: Wishlist > > Owner: Nobody > > Requestors: rjray@blackperl.com > > Status: new > > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=79947 > > > > > > > Currently, try/catch/finally blocks are not recognized the way > > if/elsif/else blocks are, and lead to inconsistent line-breaks when > > --cuddled-else is set. It would be nice if they were cuddled as well. > > See the attached files: first file (t.pl) is a short example with both an > > if-block and a try-block. Second file (t.pl.tdy) is what it currently > > looks like after perltidy. Third file (t.pl.tdy2) is what I think it > > should look like. > > > > This could be tricky, as there are competing exception-handling packages > > on CPAN, some with slightly different syntax. Try::Tiny, for example, > > does not take a class name after "catch", and only offers > > try/catch/finally. Error, in contrast, takes a class name after "catch", > > and offers try, catch, except, otherwise and finally. > > > > Randy > > -- > > Randy J. Ray > > rjray@blackperl.com > > randy.j.ray@gmail.com > >
Greetings, I just bumped into this when using TryCatch and perltidy. Sample Code Block: try { croak "An Error!"; } catch ( $error ) { print STDERR $error . "\n"; } The error generated by perltidy was: 31: syntax error at ') {', didn't see one of: case elsif for foreach given if switch unless until when while I fixed this by making a minor change to %is_blocktype_with_paren in Perl/Tidy.pm (version 20150815) Line 4110 Before: @_ = qw(if elsif unless while until for foreach switch case given when); After: @_ = qw(if elsif unless while until for foreach switch case given when catch); Hopefully this is something that can be included in the next update, I'd welcome any feedback. Thanks!
Subject: Re: [rt.cpan.org #79947] Enhancement: Have "cuddled-else" logic applied to try/catch/finally blocks
Date: Tue, 23 Feb 2016 10:50:55 -0800
To: "bug-Perl-Tidy [...] rt.cpan.org" <bug-Perl-Tidy [...] rt.cpan.org>
From: Steven Hancock <perltidy [...] users.sourceforge.net>
Thanks, it will be fixed in the next version. Steve On Tue, Feb 23, 2016 at 9:38 AM, Robert Stone via RT < bug-Perl-Tidy@rt.cpan.org> wrote: Show quoted text
> Queue: Perl-Tidy > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=79947 > > > Greetings, > > I just bumped into this when using TryCatch and perltidy. > > Sample Code Block: > > try { > croak "An Error!"; > } > catch ( $error ) { > print STDERR $error . "\n"; > } > > The error generated by perltidy was: > 31: syntax error at ') {', didn't see one of: case elsif for foreach > given if switch unless until when while > > I fixed this by making a minor change to %is_blocktype_with_paren in > Perl/Tidy.pm (version 20150815) > Line 4110 > > Before: > @_ = qw(if elsif unless while until for foreach switch case given when); > > After: > @_ = qw(if elsif unless while until for foreach switch case given when > catch); > > Hopefully this is something that can be included in the next update, I'd > welcome any feedback. > > Thanks! >
This is fixed in version 20160301 On Tue Feb 23 13:51:07 2016, SHANCOCK wrote: Show quoted text
> Thanks, it will be fixed in the next version. > Steve > > On Tue, Feb 23, 2016 at 9:38 AM, Robert Stone via RT < > bug-Perl-Tidy@rt.cpan.org> wrote: >
> > Queue: Perl-Tidy > > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=79947 >
From: qsimpleq
additional fixes: add catch to default keywords for which space is introduced before an opening paren add catch to block types may have text between the keyword and opening curly Суб Фев 27 12:58:51 2016, SHANCOCK писал: Show quoted text
> This is fixed in version 20160301 > > On Tue Feb 23 13:51:07 2016, SHANCOCK wrote:
> > Thanks, it will be fixed in the next version. > > Steve > > > > On Tue, Feb 23, 2016 at 9:38 AM, Robert Stone via RT < > > bug-Perl-Tidy@rt.cpan.org> wrote: > >
> > > Queue: Perl-Tidy > > > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=79947 >
From: qsimpleq
add files Чтв Апр 28 03:33:24 2016, http://mrakobes86reg.id.bk.ru/ писал: Show quoted text
> additional fixes:
From: qsimpleq
Subject: try.diff
--- Tidy.pm_old 2016-03-01 19:46:22.000000000 +0500 +++ Tidy.pm 2016-04-28 12:18:27.857379362 +0500 @@ -7718,7 +7718,7 @@ # default keywords for which space is introduced before an opening paren # (at present, including them messes up vertical alignment) @_ = qw(my local our and or err eq ne if else elsif until - unless while for foreach return switch case given when); + unless while for foreach return switch case given when catch); @space_after_keyword{@_} = (1) x scalar(@_); # first remove any or all of these if desired @@ -11773,7 +11773,7 @@ # curly. Note: 'else' does not, but must be included to allow trailing # if/elsif text to be appended. # patch for SWITCH/CASE: added 'case' and 'when' - @_ = qw(if elsif else unless while until for foreach case when); + @_ = qw(if elsif else unless while until for foreach case when catch); @is_if_elsif_else_unless_while_until_for_foreach{@_} = (1) x scalar(@_); } @@ -30007,7 +30007,7 @@ @_ = qw( BEGIN END CHECK INIT AUTOLOAD DESTROY UNITCHECK continue if elsif else unless do while until eval for foreach map grep sort - switch case given when catch); + switch case given when catch try finally); @is_code_block_token{@_} = (1) x scalar(@_); # I'll build the list of keywords incrementally @@ -30236,6 +30236,8 @@ when err say + + catch ); # patched above for SWITCH/CASE given/when err say
From: qsimpleq
Subject: test.pl
#!/usr/bin/env perl use strict; use warnings; use Error qw(:try); sub foo { if (1) { do_something(); } else { do_something_else(); } try { something(); } catch Error with { something_else(); } finally { foo(); }; } try { croak "An Error ёбаный стыд!"; } catch($error) { print STDERR $error . "\n";};
From: todd_richmond [...] hotmail.com
I see some attempts made to add try/catch work, but the simplest test case is still failing with 20180101. I noticed a few places in the code that have special handling for else cudling that are not replicated for catch, but adding another case did not seem to help Any progress on this?
Subject: Re: [rt.cpan.org #79947] Enhancement: Have "cuddled-else" logic applied to try/catch/finally blocks
Date: Sat, 10 Feb 2018 07:21:27 -0800
To: bug-Perl-Tidy [...] rt.cpan.org
From: Steven Hancock <s7078hancock [...] gmail.com>
Todd, I've included all patches received, but this is has been low priority for a couple of reasons. One is that supporting a variety of syntaxes is an open-ended problem, especially when function calls are made without args in parens. This can lead to an endless cycle of code patches unless great care is taken in any implementation. Another is that modifying the existing cuddled-else coding would be very difficult (really almost impossible to do without creating a bug). However, I have been thinking that it may be possible to generalize the recent 'weld' option (-wn). This option, which causes nearby opening container tokens and associated closing tokens to behave as one unit, might be generalized to also "weld" selected closing and opening braces together to achieve a cuddled-else format. This feature works on top of perltidy, so this avoids modifying the existing code. It works by locating the sections of code which should not be broken and passing this information to the formatter. It could be modified to also search for pairs of closing and opening block braces and 'weld' them together, so that they do not get separated and thus become cuddled. There would also need to be a flag specifying what block types should be welded in this fashion. So the idea would be to have two flags, one turning cuddling on and off, and the other specifying the block types. Specifying block types might be done for example by giving a dash separated list, such as 'if-elsif-else' or 'try-catch-finally' or in general 'someword1-someword2-someword3-someword4'; This seems like a promising approach, but I need to do some research before working on an implementation. Steve On Thu, Feb 8, 2018 at 2:01 PM, todd_richmond@hotmail.com via RT < bug-Perl-Tidy@rt.cpan.org> wrote: Show quoted text
> Queue: Perl-Tidy > Ticket <URL: https://rt.cpan.org/Ticket/Display.html?id=79947 > > > I see some attempts made to add try/catch work, but the simplest test case > is still failing with 20180101. I noticed a few places in the code that > have special handling for else cudling that are not replicated for catch, > but adding another case did not seem to help > Any progress on this? > > >
The -cb option in version 20180219 gives cuddled-else type formatting for try/catch/finally blocks