in reply to run perl -e programmatically as another user

If the main script is being run as root, you can easily switch user by assigning to $< and $>

use strict; use warnings; use IPC::Run 'run'; my $code = q{ print "Real user id: $<\n"; warn "Effective user id: $>\n"; }; my ($stdout, $stderr); { local $< = local $> = 500; run [ qw/perl -e/, $code ], '>>', \$stdout, '2>>', \$stderr; } print "STDOUT was: $stdout"; print "STDERR was: $stderr";
package Cow { use Moo; has name => (is => 'lazy', default => sub { 'Mooington' }) } say Cow->new->name