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

Log and email your Apache errors

In the .htaccess at the root of the site:
ErrorDocument 404 http://www.example.com/error_page.php?err=404
ErrorDocument 503 http://www.example.com/error_page.php?err=503
etc.

In error_page.php:


$myemail = "someone@example.com";
$subject = "example.com: $errorNum error";
$message = "$errorNum Error Report:\n";
$message .= "\nHTTP_REFERRER: ".$_SERVER['HTTP_REFERER'];
$message .= "\nREQUEST_URI: ".$_SERVER['REQUEST_URI'];
$message .= "\nHTTP_USER_AGENT: ".$_SERVER['HTTP_USER_AGENT'];
$message .= "\nQUERY_STRING: ".$_SERVER['QUERY_STRING'];
$message .= "\nREMOTE_ADDRESS: ".$_SERVER['REMOTE_ADDR'];


ob_start();
print "\n\nSERVER = "; print_r( $_SERVER );
print "\nGET = "; print_r( $_GET );
print "\nPOST = "; print_r( $_POST );
print "\nFILES = "; print_r( $_FILES );
print "\nREQUEST = "; print_r( $_REQUEST );
print "\nSESSION = "; print_r( $_SESSION );
print "\nENV = "; print_r( $_ENV );
print "\nCOOKIE = "; print_r( $_COOKIE );
print "\nprevious php_errormsg = "; print_r( $php_errormsg );
print "\nargv = "; print_r( $argv );
$output = ob_get_clean();
$message .= "\n\n$output\n";


# email the error
mail($myemail,$subject,$message,"From: support@example.com");


# log the error
$myFile = "logs/error.".date('Y_m_d-H.i.s').".log";
$fh = fopen($myFile, 'w');
$stringData = $message;
fwrite($fh, $stringData);
fclose($fh);
?>

Vim features


  • Auto-complete (for Perl files): start typing, then press Ctrl-Shift-P

Automate an FTP script

in the ftp script put.pl:
#!/bin/bash
HOST="example.com"
HOMEDIR="/"
ftp -i $HOST <<-EOF
passive
cd $HOMEDIR
put $1
quit
EOF
echo "I put the file "$1

in ~/.netrc:

machine example.com
    login [username]
    password [password]

to run:
put.pl [filename]

Vim navigation

1. Vim Line Navigation
Following are the four navigation that can be done line by line.
  • k – navigate upwards
  • j – navigate downwards
  • l – navigate right side
  • h – navigate left side
By using the repeat factor in VIM we can do this operation for N times. For example, when you want to
go down by 10 lines, then type “10j”.

Within a line if you want to navigate to different position, you have 4 other options.
  • 0 – go to the starting of the current line.
  • ^ – go to the first non blank character of the line.
  • $ – go to the end of the current line.
  • g_ – go to the last non blank character of the line.

2. Vim Screen Navigation

Following are the three navigation which can be done in relation to text shown in the screen.
  • H – Go to the first line of current screen.
  • M – Go to the middle line of current screen.
  • L – Go to the last line of current screen.
  • ctrl+f – Jump forward one full screen.
  • ctrl+b – Jump backwards one full screen
  • ctrl+d – Jump forward (down) a half screen
  • ctrl+u – Jump back (up) one half screen

3. Vim Special Navigation

You may want to do some special navigation inside a file, which are:
  • N% – Go to the Nth percentage line of the file.
  • NG – Go to the Nth line of the file.
  • G – Go to the end of the file.
  • `” – Go to the position where you were in NORMAL MODE while last closing the file.
  • `^ – Go to the position where you were in INSERT MODE while last closing the file.
  • gg – Go to the beginning of the file.

4. Vim Word Navigation

You may want to do several navigation in relation to the words, such as:
  • e – go to the end of the current word.
  • E – go to the end of the current WORD.
  • b – go to the previous (before) word.
  • B – go to the previous (before) WORD.
  • w – go to the next word.
  • W – go to the next WORD.

5. Vim Paragraph Navigation

  • { – Go to the beginning of the current paragraph. By pressing { again and again move to the previous paragraph beginnings.
  • } – Go to the end of the current paragraph. By pressing } again and again move to the next paragraph end, and again.

6. Vim Search Navigation

  • /i – Search for a pattern which will you take you to the next occurrence of it.
  • ?i – Search for a pattern which will you take you to the previous occurrence of it.
  • - Go to the next occurrence of the current word under the cursor.
  • - Go to the previous occurrence of the current word under the cursor.

7. Vim Code Navigation

% – Go to the matching braces, or parenthesis inside code.

8. Vim Navigation from Command Line

Vim +N filename: Go to the Nth line of the file after opening it.
vim +10 /etc/passwd

An easy way to browse the file system is the command:
:Sex
Really!

Perl sub name in vi status line

" Thanks Ovid!
" http://blogs.perl.org/users/ovid/2011/01/show-perl-subname-in-vim-statusline.html


:set laststatus=2


if ! exists("g:did_perl_statusline")
    setlocal statusline+=%(\ %{StatusLineIndexLine()}%)
    setlocal statusline+=%=
    setlocal statusline+=%f\ 
    setlocal statusline+=%P
    let g:did_perl_statusline = 1
endif


if has( 'perl' )
perl << EOP
    use strict;
    sub current_sub {

        my $curwin = $main::curwin;
        my $curbuf = $main::curbuf;


        my @document = map { $curbuf->Get($_) } 0 .. $curbuf->Count;
        my ( $line_number, $column  ) = $curwin->Cursor;


        my $sub_name;
        # for modules, display the current sub name
        for my $i ( reverse ( 1 .. $line_number  -1 ) ) {
            my $line = $document[$i];
            if ( $line =~ /^\s*sub\s+(\w+)\b/ ) {
                $sub_name = $1;
                last;
            }
        }
        # for templates, display the current block starting line
        if (not $sub_name) {
            for my $i ( reverse ( 1 .. $line_number  -1 ) ) {
                my $line = $document[$i];
                # if ($input_state eq 'next_disc_at_station') {
                if ( $line =~ /^}/ ) {
                    # we're below a block
                    last;
                }
                elsif ( $line =~ /^(\S.+{)\s*$/ ) {
                    $sub_name = $1;
                    $sub_name =~ s/'/''/g; #' # escape single quotes for vim function
                    last;
                }
            }
        }
        # TODO:
        # * Reset sub name if we're out of the sub (check for closing bracket: })
        # * Search upwards vertically in the same column for nested blocks
        $sub_name ||= '..';
        VIM::DoCommand "let subName='$line_number: $sub_name'";
    }
EOP


function! StatusLineIndexLine()
  perl current_sub()
  return subName
endfunction
endif

Set an option in vim

:set cmdheight=3
or
:set ch=3