A piggy bank of commands, fixes, succinct reviews, some mini articles and technical opinions from a (mostly) Perl developer.

VirtualBox commands


VBoxManage showvminfo [appliance name] |grep NIC

NIC 1 Rule(0):   name = AMQ admin page, protocol = tcp, host ip = 127.0.0.1, host port = 6666, guest ip = , guest port = 8161
NIC 1 Rule(1):   name = PRL, protocol = tcp, host ip = 127.0.0.1, host port = 5555, guest ip = , guest port = 5000
NIC 1 Rule(2):   name = SSH, protocol = tcp, host ip = 127.0.0.1, host port = 2222, guest ip = , guest port = 22
NIC 1 Rule(3):   name = XT, protocol = tcp, host ip = 127.0.0.1, host port = 8888, guest ip = , guest port = 8529
NIC 1 Rule(4):   name = web, protocol = tcp, host ip = 127.0.0.1, host port = 8080, guest ip = , guest port = 80

Run an external command from Perl, capturing both the output and the exit value


# This is how to run an external command from Perl,
# capturing both the output and the exit value

sub run_external_command {
    my ($self, $command) = @_;
    $self->logger->debug("Running command: $command");

    my $output = '';
    $command .= ' 2>&1 | '; # capture all output
    open(my $capture, $command);
    {
        local $/ = undef;
        $output = <$capture>;
    }
    close($capture);
    chomp($output);

    my $exit_value = ${^CHILD_ERROR_NATIVE} >> 8;
    $self->logger->debug("Exit value of command: '$exit_value'");
    $self->logger->debug("Output of command: '".($output || '[no output]')."'");

    return ($exit_value, $output);
}

This is another way:

use IPC::Open3;

sub run_external_command_2 {
  local (*my_in, *my_out, *my_err);

  my $command = "some command here that might error";
  my $pid = open3(\*my_in, \*my_out, \*my_err, $command) or die($!);

  close(my_in);
  my $std_out = do { local $/; };close(my_out);
  my $std_err = do { local $/; };close(my_err);

  waitpid($pid, 0);
  my $exit_code = $? >> 8;

  return { exit_code => $exit_code, std_out => $std_out, std_err => $std_err };
}

Find all tables with a particular column

Postgres:

SELECT table_name, column_name FROM information_schema.columns WHERE column_name like '%foo%';

information_schema is documented here:

http://www.postgresql.org/docs/devel/static/information-schema.html