I'd like to write data to a file, but the file handle should be opened with sudo or else I get permission denied error. But looks like something like following is not possible in perl?
sudo open (FH, "> $filename") or die "$!\n";
I'd like to write data to a file, but the file handle should be opened with sudo or else I get permission denied error. But looks like something like following is not possible in perl?
sudo open (FH, "> $filename") or die "$!\n";
sudo
is a linux command, it is not a Perl function. You can run the whole Perl script with sudo (sudo perl script.pl
), or you can change your user id in Perl by assigning to $<
and $>
special variables (see perlvar - Perl predefined variables) which will only be possible with extra privileges, anyway.
BTW, open
sets $!
on failure, not $@
.
open(my $pipe_fh, '-|', 'sudo', 'cat', $filename) or die "Unable to open pipe: $!\n";
It creates another process to solve your problem that may be better solved by running the script with the correct rights.