Tuesday, April 28, 2015

byobu: Show pwd in prompt

Enable prompt
byobu-enable-prompt
Disable prompt
byobu-disable-prompt

Friday, April 24, 2015

mod_perl: How to send a custom content type along with custom_response

By default, mod_perl Apache2::Response::custom_response will use the following content-type: text/html; charset=iso-8859-1.
But, what if we want to use e.g. application/x-www-form-urlencoded; charset=utf-8 instead?

Main handler:
sub handler {
    my ($i_request_rec) = @_;

    @{$i_request_rec->pnotes}{'err_txt', 'err_content_type'} = ('appid=45465&status=-42', 'application/x-www-form-urlencoded; charset=utf-8');
    $i_request_rec->custom_response(Apache2::Const::SERVER_ERROR, '/error_vas/');
    return Apache2::Const::SERVER_ERROR;
}
Apache conf.:
<Location /error_vas/>
        SetHandler perl-script
        PerlResponseHandler Toto::ErrorVas
</Location>
ErrorVas.pm:
package Toto::ErrorVas;

use strict;
use warnings;
use Apache2::Const -compile => qw(:common :log);

sub handler {
    my ($i_request_rec) = @_;

    return Apache2::Const::NOT_FOUND unless $i_request_rec->prev;

    $i_request_rec->content_type($i_request_rec->prev->pnotes->{err_content_type});
    $i_request_rec->print($i_request_rec->prev->pnotes->{err_txt});

    return Apache2::Const::OK;
}
source: http://foertsch.name/ModPerl-Tricks/custom-content_type-with-custom_response.shtml

perl: setting multiple hash values at once

perl -MData::Dumper -e '@{$h}{qw/toto tata/} = (34, 56); print Dumper $h;'              
$VAR1 = {
          'tata' => 56,
          'toto' => 34
        };

Thursday, April 23, 2015

perl: posting to multihomed hostname

What if you want to post to a hostname resolving to multiple IP addresses?
    local @LWP::Protocol::http::EXTRA_SOCK_OPTS = (
                                                   PeerAddr   => $my_hostname,
                                                   MultiHomed => 1,
                                                  );
    my $ua = LWP::UserAgent->new;
    my $response = $ua->post("https://$my_hostname/action",
                             Content_Type => 'application/x-www-form-urlencoded; charset=utf-8',
                             Content      => {
                                              param1 => 'value1',
                                              param2 => 'value2',
                                             });

php/oci-extension: missing leading zero

Reading numbers from Oracle in PHP results in missing leading 0s if -1 < value < 1.
The reason for this behavior is because Oracle OCI omits 0s in results and PHP converts all results from OCI to strings without performing any casting depending on column datatype.

A simple workaroud consists in converting to float:
$a = '.63';
echo (float) $a;
source: http://stackoverflow.com/questions/4284571/php-oci-oracle-and-default-number-format

Oracle alternative:
REGEXP_REPLACE(TO_CHAR(x), '^\.', '0.')
or if we handle negative numbers
REGEXP_REPLACE(TO_CHAR(x), '(-?)^\.', '\10.')

Wednesday, April 22, 2015

perl: print module version

perl -MLWP::UserAgent -e 'print $LWP::UserAgent::VERSION'

Thursday, April 9, 2015

HTML: POST array not showing unchecked checkbox

<td>
  <input type="checkbox" name="cb[]"/>
  <input type="checkbox" name="cb[]"/>
</td>
  • Solution 1) Explicit index + hidden trick
  • <td>
      <input type="hidden" name="cb[0]" value=""/>
      <input type="checkbox" name="cb[0]"/>
      <input type="hidden" name="cb[1]" value=""/>
      <input type="checkbox" name="cb[1]"/>
    </td>
  • Solution 2) Explicit index + fill in the blanks (e.g. with PHP)
  • <td>
      <input type="checkbox" name="cb[0]"/>
      <input type="checkbox" name="cb[1]"/>
    </td>
    $post_array = $_POST['cb'];
    $defaults = array_fill(0, 2, '0');
    $post_array = $post_array + $defaults;