use strict;
my $quote_ref = {
phrase => "Foo",
author => "Bar",
approved => 1,
};
set_phrase($quote_ref, "Bee");
print STDOUT get_phrase($quote_ref) . "\n";
print STDOUT get_author($quote_ref) . "\n";
print STDOUT (is_approved($quote_ref) ? "Is approved\n" : "Is not approved\n");
exit 0;
sub get_phrase {
my $hash_ref = shift;
return $hash_ref->{phrase};
}
sub set_phrase {
my $hash_ref = shift;
$hash_ref->{phrase} = shift;
}
sub get_author {
my $hash_ref = shift;
return $hash_ref->{author};
}
sub set_author {
my $hash_ref = shift;
$hash_ref->{author} = shift;
}
sub is_approved {
my $hash_ref = shift;
return $hash_ref->{approved};
}
####
package Quote;
use strict;
my $quote_ref = {
phrase => "Foo",
author => "Bar",
approved => 1,
};
sub get_phrase {
return $quote_ref->{phrase};
}
sub set_phrase {
$quote_ref->{phrase} = shift;
}
sub get_author {
return $quote_ref->{author};
}
sub set_author {
$quote_ref->{author} = shift;
}
sub is_approved {
return $quote_ref->{approved};
}
1;
##
##
use strict;
use Quote;
print STDOUT Quote::get_phrase(), "\n";
print STDOUT Quote::get_author(), "\n";
print STDOUT (Quote::is_approved() ? "Is approved" : "Is not approved"), "\n";
exit 0;
##
##
Foo
Bar
Is approved
##
##
package Quote;
use strict;
sub new {
my $class = shift;
my $self = {};
return bless $self, $class;
}
sub set_phrase {
my $self = shift;
my $phrase = shift;
$self->{phrase} = $phrase;
}
sub get_phrase {
my $self = shift;
return $self->{phrase};
}
sub set_author {
my $self = shift;
my $author = shift;
$self->{author} = $author;
}
sub get_author {
my $self = shift;
return $self->{author};
}
sub is_approved {
my $self = shift;
@_ ? $self->{_approved} = shift : $self->{_approved};
}
1;
##
##
use strict;
use Quote;
my $phrase = "Baz";
my $author = "Foo";
my $quote = Quote->new();
$quote->set_phrase($phrase);
$quote->set_author($author);
print STDOUT $quote->get_phrase(), "\n";
print STDOUT $quote->get_author(), "\n";
print STDOUT ($quote->is_approved() ? "Is approved" : "Is not approved"), "\n";
exit 0;
##
##
Baz
Foo
Is not approved
##
##
sub new {
my $class = shift;
my $self = {};
print STDOUT ref($self), "\n";
bless $self, $class;
print STDOUT "Object ", $self, " is of class ", ref($self), "\n";
return $self;
}
##
##
HASH
Object Quote=HASH(0x804b514) is of class Quote
Baz
Foo
Is not approved
##
##
sub set_phrase {
my $self = shift;
print STDOUT "Object ", $self, " is of class ", ref($self), "\n";
my $phrase = shift;
$self->{phrase} = $phrase;
}
##
##
Object Quote=HASH(0x804b514) is of class Quote
##
##
use strict;
use Quote;
my $phrase = "Baz";
my $author = "Foo";
my $quote1 = Quote->new();
my $quote2 = Quote->new();
$quote1->set_phrase($phrase);
$quote1->set_author($author);
$quote2->set_phrase($phrase);
$quote2->set_author($author);
$quote2->set_phrase("Some other phrase");
$quote1->set_author("Some other author");
print STDOUT $quote1->get_author(), " wrote ", $quote1->get_phrase(), "\n";
print STDOUT $quote2->get_author(), " wrote ", $quote2->get_phrase(), "\n";
exit 0;
##
##
Some other author wrote Baz
Foo wrote Some other phrase
##
##
sub set_phrase {
my $self = shift;
my $phrase = shift;
if (length($phrase) > 155) {
$self->{invalid} = 1;
$self->{invalid_message} = "Quote is more than 155 characters";
}
$self->{phrase} = $phrase;
}
sub set_author {
my $self = shift;
my $author = shift;
unless ($author) {
$self->{invalid} = 1;
$self->{invalid_message} = "You must specify an author";
}
$self->{author} = $author;
}
sub is_valid {
my $self = shift;
return ! $self->{invalid};
}
sub get_invalid_message {
my $self = shift;
return $self->{invalid_message};
}
##
##
sub set_phrase {
my $self = shift;
my $phrase = shift;
if (length($phrase) > 255) {
$self->{invalid} = 1;
$self->{invalid_message} = "Quote is more than 255 characters";
}
$self->{phrase} = $phrase;
}
sub set_author {
my $self = shift;
my $author = shift;
unless ($author) {
$author = "Anonymous";
}
$self->{author} = $author;
}
##
##
package Saver;
use strict;
sub save {
my $self = shift;
return;
}
package Saver::File;
use strict;
use IO::Handle;
@Saver::File::ISA = qw(Saver);
sub new {
my $class = shift;
my $file = shift;
my $self = {
file => $file,
};
return bless $self, $class;
}
sub _open {
my $self = shift;
open (OUT, ">" . $self->{file}) or die ("Unable to open file: " . $self->{file});
$self->{fh} = \*OUT;
}
sub save {
my $self = shift;
my $line = shift;
unless ($self->{fh}) {
&_open($self);
}
print {$self->{fh}} $line . "\n";
}
sub DESTROY {
my $self = shift;
print STDOUT "Closing file\n";
&_close($self);
return;
}
1;
##
##
use strict;
use Quote;
use Saver;
my $file = "temp_file.txt";
my $phrase = "Baz";
my $author = "Foo";
my $saver = Saver::File->new($file);
my $quote1 = Quote->new();
my $quote2 = Quote->new();
$quote1->set_phrase($phrase);
$quote1->set_author($author);
$quote2->set_phrase($phrase);
$quote2->set_author($author);
$quote2->set_phrase("Some other phrase");
$quote1->set_author("Some other author");
$saver->save($quote2->get_author() . " wrote " . $quote2->get_phrase());
$saver->save($quote1->get_author() . " wrote " . $quote1->get_phrase());
print STDOUT "Exiting\n";
exit 0;
##
##
Exiting
Closing file
##
##
sub serialize {
my $self = shift;
my $quote = shift;
my $text = '';
$text .= $quote->get_author();
$text .= " wrote ";
$text .= $quote->get_phrase();
return $text;
}
##
##
sub save {
my $self = shift;
my $quote = shift;
unless ($self->{fh}) {
$self->_open();
}
print {$self->{fh}} $self->serialize($quote) . "\n";
}
##
##
$saver->save($quote2);
$saver->save($quote1);
##
##
package Saver::Database;
use strict;
use DBI;
@Saver::Database::ISA = qw(Saver);
our $sql = "INSERT INTO quotes VALUES (0, ?)";
sub new {
my $class = shift;
my $db = shift;
my $user = shift;
my $pass = shift;
my $self = {
db => $db,
user => $user,
pass => $pass,
};
return bless $self, $class;
}
sub _open {
my $self = shift;
my $dbh = DBI->connect("DBI:mysql:" . $self->{db} . ";host=localhost", $self->{user}, $self->{pass}) or die ("Couldn't connect to database: " . $DBI::errstr);
$self->{dbh} = $dbh;
}
sub _close {
my $self = shift;
$self->{dbh}->disconnect() or die ("Couldn't disconnect from database: " . $self->{dbh}->errstr);
}
sub save {
my $self = shift;
my $quote = shift;
unless ($self->{dbh}) {
&_open();
}
my $sth = $self->{dbh}->prepare($Saver::Database::sql);
$sth->execute($self->serialize($quote));
$sth->finish();
}
sub DESTROY {
my $self = shift;
&_close();
return;
}
##
##
my $saver = Saver::File->new($file);
##
##
my $saver = Saver::Database->new($db, $user, $pass);
##
##
sub phrase {
my $self = shift;
@_ ? $self->{phrase} = shift : $self->{phrase};
}
##
##
$quote->Set(phrase=>$phrase,author=>$author);
##
##
eval {
$object->method($foo)
}
if (@$) {
... handle error ...
}