Perl

Using Open3: Create a file with the size of 15MB and check it's created successfully

Difficulty: unrated

Source: bregman-arie/devops-exercises by Arie Bregman

Answer

  • Code:
use IPC::Open3;
use Data::Dumper;

sub execute_command {
    my @command_to_execute = @_;
    my ($stdin, $stdout, $stderr);
    eval {
        open3($stdin, $stdout, $stderr, @command_to_execute);
    };
    if ($@) {
        print "Error. Details: $@";
    }
    close($stdin);
    return $stdout;
}

my $file_name = 'perl_open3_test';
&execute_command('truncate', '-s', '15M', $file_name);
my $result = &execute_command('stat', '-c', '%s', $file_name);
print Dumper();
  • Result:
$ -> perl command.pl
$VAR1 = '15728640
';