Way too much file system work.
use warnings;
use strict;
use RPi::Pin;
use RPi::Const qw(:all);
my $curtain_relay_pin = 5;
my $pin = RPi::Pin->new($curtain_relay_pin);
$pin->mode(OUTPUT);
if (...) {
$pin->write(LOW);
}
elsif ($pin->read == LOW) {
$pin->write(HIGH);
}
Done. That assumes an active low relay. No need for super user access whatsoever. No touching of the file system whatsoever. Executed in fast C code that has access directly to the GPIO registers. No need for the entire RPi::WiringPi either. However, you don't get the pin security and cleanup that's provided by the encompassing distribution, but I digress.
The RPi::Pin module provides access to all related GPIO pin functionality... pull up/down resistors, PWM, interrupts, setting of alternate pin modes etc.