stevieb has asked for the wisdom of the Perl Monks concerning the following question:

Is this a simple case of me not knowing about a write output or seriously overlooking something blatant?

I was reading that use re 'debug'; is lexically scoped, but after redirecting both STDERR and STDOUT to a file, I still get output to on my console. The scope works properly (as I don't see output for the latter regex check on either STD* outputs or files), but why is there output from re 'debug' showing up on my console even with the redirects?

What output is the debug command outputting to that I don't know about?

#!/usr/bin/perl use warnings; use strict; my $string = 'd"alice'; { use re 'debug'; open STDERR, '>', 'err.txt' or die $!; open STDOUT, '>', 'out.txt' or die $!; $string =~ s/\bd\"//g; } $string =~ /alice/;

Output to the console from the code above:

$ ./bound.pl Compiling REx "\bd\%"" Final program: 1: BOUND (2) 2: EXACT <d"> (4) 4: END (0) anchored "d%"" at 0 (checking anchored) stclass BOUND minlen 2

Full output without redirects or scoping the use statement:

Compiling REx "\bd\%"" Final program: 1: BOUND (2) 2: EXACT <d"> (4) 4: END (0) anchored "d%"" at 0 (checking anchored) stclass BOUND minlen 2 Matching REx "\bd\%"" against "d%"alice" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [0..7] gave 0 Found anchored substr "d%"" at offset 0 (rx_origin now 0)... (multiline anchor test skipped) looking for class: start_shift: 0 check_at: 0 rx_origin: 0 endpos: 1 Does not contradict STCLASS... Intuit: Successfully guessed: match at offset 0 0 <> <d"alice> | 1:BOUND(2) 0 <> <d"alice> | 2:EXACT <d">(4) 2 <d"> <alice> | 4:END(0) Match successful! Matching REx "\bd\%"" against "alice" Intuit: trying to determine minimum start position... doing 'check' fbm scan, [2..7] gave -1 Did not find anchored substr "d%""... Match rejected by optimizer Freeing REx: "\bd\%""

Why is the beginning part of the output being written to the console, and the rest stored in my redirected err.txt file?

On v5.22.0 fwiw

-stevieb

Replies are listed 'Best First'.
Re: use re 'debug';, scope, and output
by Anonymous Monk on Jul 14, 2015 at 01:20 UTC

    but why is there output from re 'debug' showing up on my console even with the redirects? Why is the beginning part of the output being written to the console, and the rest stored in my redirected err.txt file?

    Probably compile-time (BEGIN) versus run-time difference

    What output is the debug command outputting to that I don't know about?

    What? What is it that you know? What output is confusing?

    confirmed:

    #!/usr/bin/perl -- use strict; use warnings; use Capture::Tiny qw/ capture /; use Data::Dump qw/ dd /; my $string = 'd"alice'; my( $stdout, $stderr, $exit ) = capture { eval q{ use re 'debug'; $string =~ s/\bd\"//g; };}; $string =~ /alice/; dd( $stdout, $stderr, $exit , $string );

    Without the "eval" Compiling REx and Freeing REx (BEGIN{} and END{} respectively) don't get captured in $stderr

      It didn't occur to me at the time that compile time vs runtime was part of the issue, but now that I'm aware it completely makes sense. When I compile the regex ouside of the use statement, it isn't caught. Thanks for clarifying.

      Shouldn't everything under the feature do the same thing though?

      Your little code snip will come in handy in the future, thanks!

        Shouldn't everything under the feature do the same thing though?

        Eh? It does

        You can pick and choose which debugging output you want, but it might be nice if you could specify a filehandle for the output...

        #!/usr/bin/perl -- use strict; use warnings; use Capture::Tiny qw/ capture /; use Data::Dump qw/ dd /; my $string = 'd"alice'; my( $stdout, $stderr, $exit ) = capture { use re 'Debug', 'ALL'; no re 'Debug', 'COMPILE'; $string =~ s/\bd\"//g; }; $string =~ /alice/; dd( $stdout, $stderr, $exit , $string ); __END__ ( "", "Guessing start of match in sv for REx \"\\bd\\%\"\" against \"d%\"a +lice\"\nFound anchored substr \"d%\"\" at offset 0 ...\nstart_shift: 0 check_at: 0 s: 0 endpos: 1\nDoes not contradict ST +CLASS...\nGuessed: match at offset 0\nMatching REx \"\\bd\\%\"\" against \"d%\"alice\"\n Setting an EVAL scope, savesta +ck=48\n 0 <> <d\"alice> | 1:BOUND(2) \n 0 <> <d\"alice> | 2:EXACT <d\">(4)\n 2 <d\"> <alice +> | 4:END(0)\nMatch successful!\nMatch ing REx \"\\bd\\%\"\" against \"alice\"\nGuessing start of match in sv + for REx \"\\bd\\%\"\" against \"alice\"\nDid not find anchored substr \"d%\"\"...\nMatch rejected by optimizer\nNot pre +sent...\nMatch failed\n", 1, "alice", )
Re: use re 'debug';, scope, and output
by QM (Parson) on Jul 14, 2015 at 09:08 UTC
    Update: Nope, I was wrong, this doesn't work.

    =========

    tl;dr: Change the order:

    { open STDERR, '>', 'err.txt' or die $!; open STDOUT, '>', 'out.txt' or die $!; use re 'debug'; $string =~ s/\bd\"//g; }

    Also note that this method may interfere with other output in a real program.

    -QM
    --
    Quantum Mechanics: The dreams stuff is made of

      the order is irrelevant, use happens at compile time, and "COMPILE" diagnostic gets printed at compile time (same time pattern gets compiiled), way before the open STDERR... take effect