http://qs321.pair.com?node_id=11147005


in reply to Re: Unnesting deeply nested HTML elements (Deep recursion on subroutine "HTML::Element::delete")
in thread Unnesting deeply nested HTML elements (Deep recursion on subroutine "HTML::Element::delete")

Thanks. I've tried adding the no warnings 'recursion'; to both the example script above and to the real script (there in the smallest lexical scope available). It does not suppress the warnings in either case.

I wonder if there would be a way to simply collect and not print the warnings, perhaps with an eval. However, the attempt below still prints the same warnings as the original example script above.

#!/usr/bin/perl use HTML::TreeBuilder::XPath -weak; use strict; use warnings; my $ent = HTML::TreeBuilder::XPath->new; $ent->parse_file(\*DATA); eval { no warnings 'recursion'; $ent->delete; }; if ($@) { print "FOO\n"; } exit(0); __DATA__ <html> <head> <title>foo bar</title> </head> <body> foo <br /> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <center> <strong>bar</strong> <br /> <center>(baz)</center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </center> </body> </html>
  • Comment on Re^2: Unnesting deeply nested HTML elements (Deep recursion on subroutine "HTML::Element::delete")
  • Select or Download Code

Replies are listed 'Best First'.
Re^3: Unnesting deeply nested HTML elements (Deep recursion on subroutine "HTML::Element::delete")
by hv (Prior) on Sep 20, 2022 at 03:23 UTC

    Yeah, warnings are lexically scoped, so turning them off in one place only suppresses them if that's where they are generated.

    In this case you need to get a bit more invasive: catch all warnings for the duration of the call, and rethrow all but the one you want to avoid:

    { local $SIG{__WARN__} = sub { warn @_ unless $_[0] =~ /^Deep recursion/; }; $ent->delete; }

    Note that it is safe to warn inside the warnings handler - the handler is suppressed while it is being called.