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

Perl debugger tips and tricks


In the debugger:

  • $DB::deep = 500 # prevent the warning "100 levels deep in subroutine calls!"
  • {{v # display the code ahead after every step
  • b Test::Something::App::Module::login # break at a particular subroutine in a specified module
  • c # continue until the next breakpoint
Also remember that the debugger often provides extra stack trace information for 'untraceable' warnings or errors.


Ruby basics


Save URL to a file

require 'open-uri'
open('image.png', 'wb') do |file|
    file << open('http://example.com/image.png').read
end

is this the same as:

require 'open-uri'
file = open('image.png', 'wb')
file << open('http://example.com/image.png').read

Fetch URL through a proxy

print Net::HTTP::Proxy(proxy_addr, proxy_port, proxy_user, proxy_pass).get URI.parse('http://www.compufer.com/')

Hello world

puts "Hello, what's your name?"
STDOUT.flush
name = gets.chomp
puts 'Hello, ' + name + '.'

Blocks


def try  
  if block_given?  
    yield  
  else  
    puts "no block"  
  end  
end  
try # => "no block"  
try { puts "hello" } # => "hello"  
try do puts "hello" end # => "hello" 

Notes

  • Whichever variables(s) you pass into the block, the method will fill them with something, depending on what its function is. e.g. "open" puts a file handle in there.
  • List of file modes
  • HashHashes
  • ---------------
  • Builder tut1tut2tut3

Make apt-get and aptitude work through a proxy

1) sudo vi /etc/apt/apt.conf.d/70debconf
2) It should already have: DPkg::Pre-Install-Pkgs {"/usr/sbin/dpkg-preconfigure --apt || true";};
3) Add this after what's there (no blank lines):
Acquire {
        http {
                Proxy "http://user:pass@host:port";
                No-Cache "false";
                Max-Age "86400";
                No-Store "false";
        };
};
Thanks

Handle the database with Test::Class

package My::Test;

use base 'Test::Class';

sub startup : Tests(startup => 1) {
    $schema = NAP::PRL::Schema->connect( $dsn, $user, $password );
}

sub setup : Test(setup) {
    $schema->txn_begin;
}

sub teardown : Test(teardown) {
    $schema->txn_rollback;
}

Which version of Ubuntu?

Which version of Ubuntu should I install?

  • 11.04 - Yes! This works fine.
  • 11.10 - No. The menus are borked.

These are all the lovely settings you're missing in >11.04:

System preferences:



System administration:



Control centre:

Enable scrolling in GNU screen

.screenrc: 
termcapinfo xterm* ti@:te@ 


Install ruby gems

sudo yum -y install gcc ruby-devel rubygems
sudo gem install nokogiri

DBIx::Class basics

# SELECT COUNT(*) FROM product WHERE id = 104316
my $rs = $schema->resultset( 'Public::Product' )->search( { id => 104316 });
print "test: ".$rs->all;