Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

Re: print statements in perl pakages seem to be masked from STDOUT

by Corion (Pope)
on Mar 07, 2018 at 16:13 UTC ( #1210457=note: print w/replies, xml ) Need Help??


in reply to print statements in perl pakages seem to be masked from STDOUT

It's hard to tell without seeing relevant code, but if the code was short enough likely you would have found the culprit already.

The intervening code might close STDOUT or use select to select a different filehandle:

sub nasty1 { close STDOUT; } nasty1(); print "everybody keep silent"; # maybe you get a warning that STDOUT w +as closed
sub nasty2 { open my $fh, '>', 'output.txt' or die $!; select $fh; # everybody keep silent print "everybody keep silent"; # this shows up in } nasty2(); print "everybody keep silent"; # this shows up in the output file

Maybe you did not properly create and open your debug filehandle.

Maybe the code that should print stuff never even gets called at all.

Replies are listed 'Best First'.
Re^2: print statements in perl pakages seem to be masked from STDOUT
by holandes777 (Beadle) on Mar 07, 2018 at 16:46 UTC

    Looks like you're pointing me in the right direction. so I will pose a question: I am just dropping print statements in the package (the module).

    # In the main .pl use ThisPackage; ... my $the_package = ThisPackage->new(); ... (this other guys code) print "What is the value of var_here? $var_here\n"; # my debug statem +ent gets printed as What is the value of var_here? CornFlakes (more of this other guys code) # In the package: package ThisPackage; ... sub new{ my ($class, $self)=@_; ... } (this other guys code) print "What is the value of var_in_package? $var_in_package\n"; # my +debug statement does NOT get printed out as the statement in the main + program did. (more of this other guys code)

    should I expect it to print is STDOUT? The print statements I put in the .pl file are printed to STDOUT but anything inside the package does not get printed

      Maybe. Where is that print exactly? Inside a function? If so, are you sure it is called? Maybe you can try warn instead of print, or even die, they print to STDERR rather than STDOUT so you should be able to see them in the console (and since die also stops the execution, if you use it and nothing changes, you're probably modifying code that actually never gets called). If you do see something with warn, you can search for select in the code, which will change the default output for print. But since you didn't manage to write to a file with an explicit filehandle for print, it's also probable that you are modifying code that isn't run.

      In a module, supporting code (not in subroutines) should get run when you use the module, so I would have expected the "What is the value of var_in_package" to print when you ran use ThisPackage;. For example:

      #./ThisPackage.pm package ThisPackage; sub new { local $, = ", "; local $\ = "\n"; print __PACKAGE__."::new(@_)"; } print "Dummy code gets run at compile/use time\n"; 1;
      #./pkg.pl BEGIN { print "this should print before\n" } use lib '.'; use ThisPackage; ThisPackage::new(qw/hello world/); BEGIN { print "this should print after the use, but before the new\n" +}
      # output this should print before Dummy code gets run at compile/use time this should print after the use, but before the new ThisPackage::new(hello world)

      Does my code do the same for you? If so, maybe it will help you figure out what's wrong with your code.

      specifically, if you want to print values inside the package's functions, you have to include the prints inside. If you put prints outside the functions in the package, they will only execute the once, and none of the package's functions will have been run yet, so probably won't have changed package variables (assume that's what you're expecting)

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1210457]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2018-07-21 11:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    It has been suggested to rename Perl 6 in order to boost its marketing potential. Which name would you prefer?















    Results (448 votes). Check out past polls.

    Notices?