ESPUG::Meeting::Summary...

Ryan Erwin ryan at dbedge.com
Thu Mar 30 04:07:04 CST 2000


Just wanted to let everyone know that we just had
another great ESPUG meeting and give a report to
those who were unable to attend.

Robert Abarbanel with Boeing's Phantom Works and
the University of Washington continued his
excellent presentation of Object Oriented Perl.
We started with a brief review about basic object
oriented concepts.  The 3 little rules:
1) To create a class, build an object
2) To create a method, write a subroutine
3) To create an object, bless a referent

We had a brief discussion about closures vs..
"module variables".  The majority conclusion was
that "by default, make your code as restrictive as
possible.  loosen it up later if you need to"

Hashes make good objects, but:
-expensive
-slower
-autovivify (automatically created when used)

Arrays make good objects, but:
-still autovivify
-no 'named' attributes

Constant.pm will take care of the named attributes
in an array.  Constant creates a simple function
that the compiler will optimize and replace with a
value.  To use constant, try:
#!/usr/bin/perl -w
use strict;
use constant NAME => 0;
use constant STAR => 1;
use constant ASIN => 2;
use constant RELEASE_DATE => 3;
use constant RATING => 4;
use constant TIMES_I_SAW => 5;

my @DVDs;
@DVDs[ NAME,
  STAR,
  ASIN,
  RELEASE_DATE,
  RATING,
  TIMES_I_SAW ] =

  'Fight Club',
  'Pitt, Norton',
  'B00003W8NM',
  'April 25th 2000',
  '4 Stars',
  '7 in Theatre');

print $DVDs[NAME], "\t", $DVDs[TIMES_I_SAW], "\n";

Then, there are Pseudo Hashes.
Pseudo Hashes were introduced in Perl 5.005.  A
Pseudo Hash is an *array* with a first element
that is a reference to a *hash*.  A Pseudo Hash is
really just an array reference pretending to be a
hash reference.

my $pseudo_hash = [ { a=>1, b=>2, c=>3},
    "val1", "val2", "val3" ];
print $pseudo_hash->[1]; # returns 'val1'
print $pseudo_hash->{"a"}; # returns 'val1'
Whenever perl encounters an array reference being
used as a hash reference in this way, it
translates the expression to something equivalent
to the following:
$pseudo_hash->[$pseudo_hash->[0]->{"a"}];

fortunately, fields.pm comes to the rescue to give
us the best of both worlds while avoiding
autovivification.

 package DVD
 use strict;
 use fields qw{ name star rating };

this:
-informs the perl compiler that name, star, and
rating are the only valid hash keys for DVD
objects and throws an exception at compile time if
you access any other key
-creates %DVD::Fields and initializes it to
(name=>1, star=>2, rating=>3) or some other
combination.
-causes the perl compiler to replace any access of
the form $DVD_ref->{name} with the direct access
$DVD_ref->[1].  Likewise $DVD_ref->{star} to
$DVD_ref->[2] and $DVD_ref->{rating} to
$DVD_ref->[3].

Typed Lexicals:
Also starting in Perl 5.005, an extension to the
lexical variable declaration was added.  You can
use:
 my ClassName $variable_name;
 my ClassName ($var_name1, $var_name2,
$var_name3);
Just add the name of the class you are using after
the 'my'.  The ClassName must be one that perl
knows about at the point you use it in the
program.  The package must first appear *before*
you use the construct.  The typed lexical
construct tells perl that the variable is
*supposed* to be used to refer to
pseudo-hash-based objects of the specified class.
The compiler looks for access like $d->{name},
$d->{star}, or $d->{rating}.  When the compiler
finds such an access, it verifies that the key
exists in %FIELDS package hash for the same class.
If the field doesn't exist, you get a 'No Such
Field Exception'.
Compile time optimization only occurs if you use
the "typed" my syntax.  If you leave out the class
name, the code still works but isn't optimized and
field name checking will only happen at run-time.

Other Blessings...
You can bless Scalars, Subroutines, Regular
Expressions and even Typeglobs.  Scalars can
actually make quite useful objects, especially
when used with the 'flyweight pattern' where the
scalar is passed around by value.

A few final notes:
SUPER:: is the parent of your object.
Ever tried to use 'vec'?

If you are interested in presenting something at
our next ESPUG meeting, or would be interested in
hearing about a particular topic, please contact
me ryan at dbedge.com

---
Ryan Erwin
ESPUG Emperor

BTW: don't talk about fight club 8-}


 - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
     POST TO: spug-list at pm.org       PROBLEMS: owner-spug-list at pm.org
 Seattle Perl Users Group (SPUG) Home Page: http://www.halcyon.com/spug/
 SUBSCRIBE/UNSUBSCRIBE: Replace "action" below by subscribe or unsubscribe
           Email to majordomo at pm.org: "action" spug-list your_address





More information about the spug-list mailing list