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

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

  • Comment on Re^2: print statements in perl pakages seem to be masked from STDOUT
  • Download Code

Replies are listed 'Best First'.
Re^3: print statements in perl pakages seem to be masked from STDOUT
by Eily (Monsignor) on Mar 07, 2018 at 17:09 UTC

    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.

Re^3: print statements in perl pakages seem to be masked from STDOUT
by pryrt (Abbot) on Mar 07, 2018 at 17:16 UTC

    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)