Allow capture internally of the error from a failed $p4->connect() rather than going to scripts STDERR on terminal
(cross referenced to https://swarm.perforce.com/jobs/job078669
If a call to $p4->connect fails:
$p4->Connect() or die( "Failed to connect to Perforce Server" );
..Connect() spits out an error message with a helpful hint to check P4PORT e.g.
Connect to server failed; check $P4PORT.
TCP connect to p4test11:1666 failed.
How can this message be intercept and have it in say $p4->Errors (or anywhere else in the script such a variable) and not to the terminal stderr output)?
Redefining the STDERR handle to point to a variable for example in the script doesn't capture it, it still goes to the terminal.
For example something like this doesn't populate $stderr
do {
local *STDERR;
# redirect STDERR to variable
open(STDERR, ">", \$stderr) or die "Can't open STDERR: $!";
eval {
$p4->Connect() or die "Error connecting $!" ;
}
Wrapping the $p4->Connect in an eval block doesn't capture the useful message about checking the port to $! - that's still printed to STDERR on the terminal.
eval {
$p4->Connect() or die "Error connecting $!" ;
};
if($@){
# Do something before bailing out
print $@ ;
};
A real kludge would be to redirect the script stderr to a temp file and read that in (YUK!).