Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

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

by hippo (Bishop)
on Sep 19, 2022 at 17:00 UTC ( [id://11146974]=note: print w/replies, xml ) Need Help??


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

I get the following error

It isn't an error, it's a warning. You can disable it if you like with no warnings 'recursion'; but be sure to comment why you are doing that and do it in the smallest lexical scope you can manage.


🦛

  • Comment on Re: Unnesting deeply nested HTML elements (Deep recursion on subroutine "HTML::Element::delete")
  • Download Code

Replies are listed 'Best First'.
Re^2: Unnesting deeply nested HTML elements (Deep recursion on subroutine "HTML::Element::delete")
by mldvx4 (Friar) on Sep 20, 2022 at 02:40 UTC

    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>

      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.

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11146974]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others examining the Monastery: (5)
As of 2024-04-25 10:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found