Discussion:
When a File Exists and is Empty
Martin G. McCormick
2014-09-11 17:02:30 UTC
Permalink
I was checking to see how in perl can one quickly test
for a file which exists but is empty and found an example so I
wrote the following code which seems to work beautifully but it
looks a little different compared to some things I have seen so
I am asking whether it could have unintended consequences. Code
follows:

if ( !-z '/usr/home/automation/power' ) {

#mail /usr/home/automation/power
system("mail -s \"Power Issues\" toptendhcp </usr/home/automation/power");
}
else {
#no POE problems
system("mail -s \"No Power Issues\" toptendhcp </tmp/nothing2report.txt");
}

The -z test reminds me a bit of shell scripting when one
needs to test whether a string is empty or not. If this is safe,
it is a really nice way to test files for emptiness.
Thank you.

Martin McCormick
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Shawn H Corey
2014-09-11 17:07:24 UTC
Permalink
On Thu, 11 Sep 2014 12:02:30 -0500
Post by Martin G. McCormick
I was checking to see how in perl can one quickly test
for a file which exists but is empty and found an example so I
wrote the following code which seems to work beautifully but it
looks a little different compared to some things I have seen so
I am asking whether it could have unintended consequences. Code
if ( !-z '/usr/home/automation/power' ) {
#mail /usr/home/automation/power
system("mail -s \"Power Issues\" toptendhcp
</usr/home/automation/power"); }
else {
#no POE problems
system("mail -s \"No Power Issues\" toptendhcp
</tmp/nothing2report.txt"); }
The -z test reminds me a bit of shell scripting when one
needs to test whether a string is empty or not. If this is safe,
it is a really nice way to test files for emptiness.
Thank you.
Martin McCormick
Yes, Perl does follow the shell when it comes to file tests. See
`perldoc -f -X

Also, you can use `-s` instead of `! -z`.
--
Don't stop where the ink does.
Shawn
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Martin G. McCormick
2014-09-11 18:04:02 UTC
Permalink
Post by Shawn H Corey
Yes, Perl does follow the shell when it comes to file tests. See
`perldoc -f -X
Also, you can use `-s` instead of `! -z`.
Thank you. That's very close to the test -s clause in
shell scripting.
--
To unsubscribe, e-mail: beginners-***@perl.org
For additional commands, e-mail: beginners-***@perl.org
http://learn.perl.org/
Loading...