ORM::Tutorial

ORM::Tutorial is a Perl module that offers a guided tour to ORM module.
Download

ORM::Tutorial Ranking & Summary

Advertisement

  • Rating:
  • License:
  • Perl Artistic License
  • Price:
  • FREE
  • Publisher Name:
  • Alexey V. Akimov
  • Publisher web site:
  • http://search.cpan.org/~akimov/ORM-0.85/lib/ORM.pod

ORM::Tutorial Tags


ORM::Tutorial Description

ORM::Tutorial is a Perl module that offers a guided tour to ORM module. ORM::Tutorial is a Perl module that offers a guided tour to ORM module.Purpose of this document is to brief introduce usage of PerlORM library on simple example. Example is 'Tasks Planner' (or 'Todo List') application.CREATING OBJECTSCreation of objects in ORM is performed by calling 'new' method. For example let's create 'Worker' object: use Todo::Worker; $error = ORM::Error->new; $worker = Todo::Worker->new ( prop => { name => 'E. Cartman' }, error => $error, ); print $error->text;If 'new' operation fails then $error object contains information about occured errors. Use of $error object is not necessary but strongly recomended.Now to more easily manage objects of our model we will create perl script for object creation new.plFile new.pl #!/usr/bin/perl # # Use: perl new.pl ... # # Class - Name of the class without 'Todo::' prefix. # use lib "lib"; use lib "../ORM/lib"; $nick = shift; $class = "Todo::$nick"; eval "require $class" or die $@; $error = ORM::Error->new; %prop = @ARGV; $obj = $class->new( prop=>%prop, error=>$error ); if( $obj ) { print "New $nick was created with id:".$obj->id."n" if( $obj ); $obj->print; } print $error->text;Above script uses print method we doesn't declare yet. This method is aimed to print plain text information about specified object. This method should be defined in initial class so every object of our model can acces it. sub print { my $self = shift; my $ident = shift||0; my @ref; # Do not dive deeper that third level of recursion # when printing information about related objects. return if( $ident > 3 ); # Print information about specified object print ' 'x($ident*2),('-'x20),"n"; for my $prop ( (ref $self)->_all_props ) { printf "%".(20+$ident*2)."s %sn", "$prop:", (defined $self->_property_id($prop) ? $self->_property_id($prop) : '*UNDEF*'); if( (ref $self)->_prop_is_ref( $prop ) && $self->_property( $prop ) ) { push @ref, $prop; } } print ' 'x($ident*2),('-'x20),"nn"; # Print information about related objects for my $prop ( @ref ) { print ' 'x(($ident+1)*2),"Related object '$prop':n"; $self->_property( $prop )->print( $ident+1 ); } }Note the way properties were accessed. For this purpose $obj->_property( $property_name ) were used in above code. To access object properties with more grace there is AUTOLOAD method. So you can simply call $obj->$property_name() (or just $obj->deadline for example). The ORM::_property method is for cases when $property_name method should be redefined in child class for some reason.Also you can use $obj->_property_id( $property_name ) to get raw database value of the property. Its result is: * the same as of ORM::_property for plain (non-object) properties * database property value for non-ORM third party classes * object's ID for ORM classesNow we can fill our model with some more objects. # perl new.pl Worker name "Kenny McCormic" New Worker was created with id:2 -------------------- id: 2 class: Todo::Worker name: Kenny McCormic -------------------- # perl new.pl Task title "Kill Kenny" desc "Just kill Kenny!" worker 1 created "2005-12-18" start_date "2006-01-01" deadline "2006-01-02" New Task was created with id:1 -------------------- id: 1 class: Todo::Task created: 2005-12-18 desc: Just kill Kenny! worker: 1 deadline: 2006-01-02 title: Kill Kenny start_date: 2006-01-01 -------------------- Related object 'worker': -------------------- id: 1 class: Todo::Worker name: Eric Cartman -------------------- # perl new.pl Task title "Eat Chocolate pie" desc "Ask your mummy." worker 1 created "2005-12-18" start_date "2006-01-01" deadline "2006-01-02" New Task was created with id:2 -------------------- id: 2 class: Todo::Task created: 2005-12-18 desc: Ask your mummy. worker: 1 deadline: 2006-01-02 title: Eat Chocolate pie start_date: 2006-01-01 -------------------- Related object 'worker': -------------------- id: 1 class: Todo::Worker name: Eric Cartman --------------------For more comfort let's modify Todo::Task class so it can automatically assign current time to created property when explicit value is not specified: sub _validate_prop { my $self = shift; my %arg = @_; if( $arg{method} eq 'new' && ! $self->created ) { $self->_fix_prop ( prop => { created=>ORM::Date->current }, error => $arg{error}, ); } $self->SUPER::_validate_prop( %arg ); } * Method _validate_prop is implicitly called when new object is being created (new method) and when object is being updated (update method). * Condition ( $arg{method} eq 'new' ) is true only when called from within new method. In another words this means that object is not yet stored in database table. * Method ORM::_fix_prop is intended to use only within _validate_prop. * Do not forget to call SUPER::_validate_prop.Let's add one more task: # perl new.pl Task title "Keep alive" desc "Just keep alive!" worker 2 start_date "2005-12-31" deadline "2006-01-02" New Task was created with id:3 -------------------- id: 3 class: Todo::Task created: 2005-12-18 desc: Just keep alive! worker: 2 deadline: 2006-01-02 title: Keep alive start_date: 2005-12-31 -------------------- Related object 'worker': -------------------- id: 2 class: Todo::Worker name: Kenny McCormic --------------------As you can see created property is implicitly initialized with default value of current time. (It seems like Kenny will die anyway after deadline.)Requirements:· Perl Requirements: · Perl


ORM::Tutorial Related Software