Home Accessibility Courses Twitter The Mouth Facebook Resources Site Map About Us Contact
 
For 2023 (and 2024 ...) - we are now fully retired from IT training.
We have made many, many friends over 25 years of teaching about Python, Tcl, Perl, PHP, Lua, Java, C and C++ - and MySQL, Linux and Solaris/SunOS too. Our training notes are now very much out of date, but due to upward compatability most of our examples remain operational and even relevant ad you are welcome to make us if them "as seen" and at your own risk.

Lisa and I (Graham) now live in what was our training centre in Melksham - happy to meet with former delegates here - but do check ahead before coming round. We are far from inactive - rather, enjoying the times that we are retired but still healthy enough in mind and body to be active!

I am also active in many other area and still look after a lot of web sites - you can find an index ((here))
What does blessing a variable in Perl mean?

When you "bless" a variable in Perl, what are you doing?

You are taking the address of the variable, and returning a "reference to an object" - that's a result which contains three pieces of information:
1. The address in memory at which the object is held
2. The type of Perl structure that's held there (SCALAR, HASH or ARRAY)
3. The package (class) in which the data that's there resides

Those three pieces of information are the vital elements which let you pass around the single reference to a whole series of other pieces of code, and let those other pieces of code (using just the single reference) make use of and alter that data. The presence of the package information allows for "polymorphism" - that's where the call to a function is diverted to different bits of code depending on the sort of data that's help at the given location.

Here's a piece of code that defines a bus journey - blessing a scalar variable into the package (it happens to contain the number of passengers who want to travel on the journey) and a methos that's unique to buses telling us that we require one driver per 41 passengers (full bus load) or part thereof:

{ package bus;
  sub new {
    print "Hiring a bus\n";
    my ($class,$passengers) = @_;
    bless \$passengers,$class;
  }
  sub getstaff {
    my $which = $_[0];
    my $ts = int(($$which+40)/41);
    return $ts;
  }
}



We have a very similar piece of code for a train, except that we'll always need a driver and a guard (2 staff) irrespective of the number of passengers - we just need to add a few carriages if the train's getting a bit full [historic example - railways are currently short of carriages!].

Then we can set up some passenger flows and decide whether to use a bus or train for them:

$swindon = new train(230);
$trowbridge = new bus(200);
print "$swindon ... $trowbridge\n";


I've printed out the blessed variables there - usually something you would NOT do as it would really worry the user to see the strange output, but in this training note it shows you what bless has placed into the variables:
train=SCALAR(0x10082b190)
bus=SCALAR(0x10082aea8)

And we can now work out the number of staff needed for each of them in our program:

foreach $trans($swindon,$trowbridge) {
  $sn = $trans->getstaff();
  print "Staff needed - $sn\n";
  }


Results:
Staff needed - 2
Staff needed - 5


Complete source discussed / used above - [here].

(written 2010-09-24)

 
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P213 - Perl - Creating your own Classes
  [227] Bellringing and Programming and Objects and Perl - (2005-02-25)
  [246] When to bless a Perl variable - (2005-03-15)
  [975] Answering ALL the delegate's Perl questions - (2006-12-09)
  [983] Blessing in Perl / Member variable in Ruby - (2006-12-14)
  [1320] Perl for Larger Projects - Object Oriented Perl - (2007-08-25)
  [1435] Object Oriented Programming in Perl - Course - (2007-11-18)
  [1664] Example of OO in Perl - (2008-06-03)
  [1864] Object Oriented Perl - First Steps - (2008-11-01)
  [1925] Introduction to Object Oriented Programming - (2008-12-06)
  [2169] When should I use OO techniques? - (2009-05-11)
  [2834] Teaching examples in Perl - third and final part - (2010-06-27)
  [2877] Further more advanced Perl examples - (2010-07-19)
  [3059] Object Orientation in an hour and other Perl Lectures - (2010-11-18)
  [3098] Learning Object Orientation in Perl through bananas and perhaps Moose - (2010-12-21)
  [3833] Learning to use existing classes in Perl - (2012-08-10)
  [4607] Classes and object - first steps in Perl 6 - (2016-01-02)


Back to
Well House Consultants - a potted history
Previous and next
or
Horse's mouth home
Forward to
Perl - doing several things at the same time
Some other Articles
Learning to program - where to start if you have never programmed before
Some more advanced Perl examples from a recent course
Should the public sector compete with businesses? and other deep questions
Perl - doing several things at the same time
What does blessing a variable in Perl mean?
Well House Consultants - a potted history
Multiway branches in Perl - the given and when syntax
Cheap Country Hotel in Melksham, Wiltshire?
Testimonials - Well House Consultants Open Source courses
An introduction to file handling in programs - buffering, standard in and out, and file handles
4759 posts, page by page
Link to page ... 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33, 34, 35, 36, 37, 38, 39, 40, 41, 42, 43, 44, 45, 46, 47, 48, 49, 50, 51, 52, 53, 54, 55, 56, 57, 58, 59, 60, 61, 62, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93, 94, 95, 96 at 50 posts per page


This is a page archived from The Horse's Mouth at http://www.wellho.net/horse/ - the diary and writings of Graham Ellis. Every attempt was made to provide current information at the time the page was written, but things do move forward in our business - new software releases, price changes, new techniques. Please check back via our main site for current courses, prices, versions, etc - any mention of a price in "The Horse's Mouth" cannot be taken as an offer to supply at that price.

Link to Ezine home page (for reading).
Link to Blogging home page (to add comments).

You can Add a comment or ranking to this page

© WELL HOUSE CONSULTANTS LTD., 2024: 48 Spa Road • Melksham, Wiltshire • United Kingdom • SN12 7NY
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho

PAGE: http://www.wellho.net/mouth/2969_.html • PAGE BUILT: Sun Oct 11 16:07:41 2020 • BUILD SYSTEM: JelliaJamb