How to detect *every* kind of error when printing to a pipe, including missing write permissions?

On this site and elsewhere, there are quite a few questions about how to handle errors when printing to a pipe in Perl. But none of the answers, solutions and explanations I have seen so far are applicable to my specific case. Please consider the following situation:
Under Linux (Debian 13), I have a folder /path where only the root user has write permissions. Further, I have a Perl script that includes the following snippet:
if (!(open($fh_Pipe, '|-:utf8', '/usr/bin/weasyprint',
'-',
'/path/test.pdf')))
{
# Perform error action here.
}
if (!(print($fh_Pipe 'Foo')))
{
# Perform error action here.
}
If I execute that script as root, everything works: A PDF file /path/test.pdf is created that contains the text "Foo".
But if I execute that script as another user, it does not behave as expected: Indeed, the PDF file /path/test.pdf is not created, and Perl outputs a warning regarding the missing write permission. But to my surprise, none of the error action blocks executes.
That is a problem because I'd like to detect all errors with writing to the pipe from within the script, of course with reasonable effort, and the behavior I have observed seems to contradict the documentation. From the documentation for print:
Prints a string or a list of strings. Returns true if successful. [...]
Although it is formally not correct, I interpret this as it would say "... and false if not successful ..." in addition. But then it contradicts what I observe.
So my question is:
Is there a general method to detect all errors that may occur when printing to a pipe, including situations where the user that executes the respective script simply does not have write permission in the respective directory?
And why does Perl not behave as documented?
[ Side note #1: To be clear, I'm not interested in which error exactly has occurred. I just would like to know whether or not the print has succeeded, completely regardless of the kind of the possible error. ]
[ Side note #2: The page linked above later on explains that a SIGPIPE signal will be raised if we try to print to a closed pipe or socket. However, I didn't try to implement an error handling for print based on signals, because that would be a high effort for a very simple thing, and because I am not sure if a missing write permission would effect the same as a closed pipe. ]
Take Your Experience to the Next Level
NewDownload our mobile app for a faster and better experience.
Comments
0Join the discussion
Sign in to leave a comment