Thursday, October 30, 2008

OO Perl

Just wanted to jot down a good OO Perl site I came across:

Wikipedia had a decent example that got to the point:

So, here's some poop I through together just to use as my own reference. It's a Poop class!

(NOTE: Some of the lines may have been screwed up during the copying and pasting... but I don't care right now)


package Poop;
use strict;

our $VERSION = "1.0";
my $DEBUG = 0;

=head1 NAME

Poop - A pooping package.

=head1 SYNOPSIS

use Poop;
my $boy = Poop->new();
$boy->eat("Candy Bar", "Ice Cream");
$boy->poop() if $boy->turd();

=head1 DESCRIPTION

Just an OO Perl template

=head1 METHODS

=head3 new

my $cat = Poop->new();
my $dog = Poop->new(eat => @food, debug => 1, size => $size);

Instantiates a new Pooping object.
If a list of C<@food> is given, the object eats right away.
If $size is given, the object's stomach is set to that size (default 10).
If "debug" is a true value, debugging will be turned on.

=cut

sub new {
my $proto = shift;
my $class = ref($proto) || $proto;
my %params = @_;
my $self = {};

bless ($self, $class);

$self->debug($params{debug}) if exists $params{debug};

$self->{TURD} = 0;
$self->{FOOD} = [];
$self->{SIZE} = exists $params{size} ? $params{size} : 10;

$self->eat(@{$params{eat}}) if exists $params{eat};

return $self;

}

=head3 eat

my $dog = Poop->new();
$dog->eat("Dog food", "Dog bone");

Causes the object to eat the items in the list. The object will quit eating when it has to poop.
Returns a list of food it couldn't eat if it can't eat it all.
Returns an empty list if it ate it all.

=cut

sub eat($) {
my $self = shift;

$self->_DEBUG_PRINT("Eating...");

while (@_ && ! $self->turd()) {
my $f = shift;
push @{$self->{FOOD}}, $f;
print "-- Mmmmm... $f\n";
}

return @_;

}

=head3 turd

my $dog = Poop->new();
$dog->poop() if $dog->turd();

Reports whether the object has a turd. And object with a turd cannot eat anything else.

=cut

sub turd() {
my $self = shift;

$self->_DEBUG_PRINT("Checking for turd...");

return 1 if @{$self->{FOOD}} >= $self->{SIZE};
return 0;
}

=head3 poop

my $dog = Poop->new();
$dog->poop() if $dog->turd();

Makes the object poop. If there is no turd, it will return 0 and not poop.

=cut

sub poop() {
my $self = shift;
my @sounds = ("Grunt","Plop","Spphhlll","Splash","Pfffft");

$self->_DEBUG_PRINT("Entering pooping phase...");

if (! $self->turd()) {
$self->_DEBUG_PRINT("No turd to poop with...");
return 0;
}

while (my $food = shift @{$self->{FOOD}}) {
my $sound= $sounds[int(rand() * @sounds)];

print "-- $sound... $food\n";
sleep 1;
}

$self->{TURD} = 0;
}

=head3 puke

my $dog->puke();

Expells the most recent item ate. Returns the item if it existed, otherwise returns undef.

=cut

sub puke() {
my $self = shift;

my $last = pop @{$self->{FOOD}};

if ($last) {
print "-- Blahhhhh..... $last\n";
return $last;
}

return undef;
}


=head3 debug

$dog->debug();
$dog->debug(0);
$dog->debug(1);

Returns the current debug setting, sets it to false, or sets it to true (respectively).

=cut

sub debug($) {

my $self = shift;

return $DEBUG unless @_;

if ($_[0]) { $DEBUG = 1 }
else { $DEBUG = 0 }

return $DEBUG;
}

sub _DEBUG_PRINT ($) {
my $self = shift;
return unless $DEBUG;

print "-- DEBUG: " . shift;
print "\n";
}

1; # Really only need this unless you make this a .pm and "use" it.


#
# Just putting it all in one file...
#

package main;
#use Poop;

sub feed(@) {
my $obj = shift;

print "Feeding it: " . join(", ", @_) . "\n";
my @full = $obj->eat(@_);

if (@full) {
print "Couldn't eat: " . join(", ", @_) . "\n";
} else {
print "Ate it all. Good boy!\n";
}
print "\n";
}

my $dog = Poop->new(size => 5);

feed($dog, qw/Bones Chocolate Cat Soap/);
feed($dog, "Saw Dust", "Dog Food");
print "Puked up " . $dog->puke() . "\n";
print "Puked up " . $dog->puke() . "\n";
feed($dog, "Pie", "Hot Dog");
print "Go potty, boy:\n";
$dog->poop();
print "Good boy, here's a treat!\n";
$dog->eat("Treat");
print "Nooo!!! Don't puke in here!!!\n";
print "Puked: " . $dog->puke() . "\n";



Wednesday, October 29, 2008

Firefox Memory

Just a useful setting I found for Firefox to reduce the memory usage:
http://www.litfuel.net/plush/?postid=118

Basically, do the "about:config" and change the browser.sessionhistory.max_total_viewers setting. By default, this caches 10 pages per tab.

Saturday, October 18, 2008

Copying a hard disk with Knoppix

I slapped together a computer for my parents a couple of years ago. They really only surf the net, so it did not need to be to impressive. I had a 20 GB hard drive I was going to stick in it, but I dropped and broke it.... I ended up putting an extra 8 GB drive in it instead. With XP installed, it didn't take long before that was filled up and there was hardly any swap space left (something that Window's OSes seem to need a lot of for some reason...).

Anyways, I finally got around to getting a 30 GB hard drive for them. I didn't feel like reinstalling everything, so I figured I'd try copying their old hd with "dd". I used a Knoppix 5.1 disk and this page as a reference:

http://www.nilbus.com/linux/disk-copy.php


What's needed
  • Some flavor of Linux on a bootable CD (Knoppix 5.1 in this case)
  • dd
  • gparted and it's dependencies
  • a new hard disk
Steps
  1. Shutdown and unplug everything and put in the new hard drive (I switched the original to slave and made the new one master at this point)

  2. Boot up the PC and get into the Setup mode (usually by pressing F2, Delete, or some other key).

  3. Make sure everything looks savvy with the new hd. While in there, be sure it's set to boot from the CD first. Also, if you don't have a floppy drive, be sure it is disabled in the bios, otherwise gparted will hang while trying to access it.

  4. Save and boot to Knoppix

  5. Once Knoppix is up, fire up gparted and format the new drive to NTFS (I don't think this is necessary because of the way we will use dd, but I did it anyways. Besides, gparted gave me the peace of mind to know I was working with the correct partitions). You really should reboot after this to be sure the kernal picks up the new partition correctly, but I didn't (as noted early, I probably didn't need to format it anyways).

  6. Once that is done, start an xterm and do:

    dd if=/dev/hdb of=/dev/hda

  7. One thing I didn't do was enable dma on the hard drives. I had read that would make it faster, but oh well. dd will run for quite some time. NOTE: If you use the wrong device for "if" or "of", you could really screw yourself!!!

    While dd is running, it won't give any status. But, from another xterm, you can send the USR1 signal to the dd process and it displays a status:

    kill -USR1 3274 (or whatever pid)

  8. After about 2000 seconds, all 8+ GB of the old drive was copied over. Since I used "/dev/hdb" as the infile, the MBR and all that good stuff was also copied to /dev/hda. So, I fired up gparted again and was happy to see that /dev/hda now had a primary partition the exact same size of the original. I rebooted at this point.

  9. Once rebooted back into Knoppix, fired up gparted one more time and expanded the /dev/hda1 partition to fill the entire 30 GB disk.

  10. Finally, I rebooted again and was very happy to see that Windows was starting up. XP wanted to do a chkdisk since the partition size increaesed, so I let that take place. Once it was back into Windows, I defragged

So, I was very happy to see that dd worked so well. Once again, a free Linux-based solution worked as well or better than some commercial software!

Tuesday, October 14, 2008

First Blog

My first blog... How exciting. Anyways, I'll probably try to use this as a space to post anything I think will either A) help me out in the future, or B) might be useful to somebody else at some point in the future.... Stay tuned.