| |||||||||||
| |||||||||||
Use of caller to examine the sub stack
this example from a Well House Consultants training course
Source code: B4.pl Module: P310
{ use strict; package Banana1; use Time::Local; use Carp; sub new { my ($class,$d,$m,$y,$paid,$q) = @_; my %info; $info{"date"} = timegm(0,0,12,$d,$m,$y); $info{"paid"} = $paid; $info{"quantity"} = $q; bless \%info,$class; } sub getpriceeach { my ($which) = @_; # Validate the call! # Report where it is CALLED from! # Is it called on the right type of object? # If not, abort program with full stack trace if (ref($which) ne "Banana1") { my @about = caller; print "Wrong - @about\n"; my $i = 0; while (my @rslt = caller($i)) { print "Frame $i: @rslt\n"; $i++; } # Will fail (undef fn.) but we are # in an eval so it's trapped camel(); } # Is it called without params? # If extra params given, warn but carry on # (use 'croak' if you want to die) if ($#_ != 0) { carp "Parameter(s) ignored"; } $which->{"paid"} / $which->{"quantity"}; } } # Test program - lots of levels to ensure we have plenty of frames # on the stack for error reporting tests! sub action { $sept = new Banana1 (7,9,2010,1.60,8); $oct = Banana1::new("Banana1",14,10,2010,1.75,9); $nov = Banana1::new("Banana1",5,11,2010,1.80,8); print ("$sept ... $oct ... $nov \n"); $p1 = $sept -> getpriceeach(); $p2 = $oct -> getpriceeach("yellow"); # will generate a warning $p3 = Banana1::getpriceeach($nov); print "Prices are $p1 $p2 and $p3\n"; # Test the error handler - should crash $p2 = Banana1::getpriceeach("hhh"); # will generate an error print "Prices are $p1 $p2 and $p3\n"; } sub runme{ # Just to build another frame to make caller action; # demo more interesting! } $dobbin = eval ('runme'); # Don't abort on failure - return error if ($@) { print ("We know what the issue was ...\n"); print "Failure: $@\n"; } else { print "AOK\n"; } print ("We are still alive because eval trapped it\n"); __END__ Here's how that runs: munchkin:pl grahamellis$ perl Banana1.pl Banana1=HASH(0x100853c20) ... Banana1=HASH(0x100858798) ... Banana1=HASH(0x100858810) Parameter(s) ignored at Banana1.pl line 62 Prices are 0.2 0.194444444444444 and 0.225 Wrong - main Banana1.pl 69 Frame 0: main Banana1.pl 69 Banana1::getpriceeach 1 0 256 Frame 1: main Banana1.pl 75 main::action 1 0 0 Frame 2: main (eval 1) 1 main::runme 1 0 0 Frame 3: main Banana1.pl 78 (eval) 0 0 runme ; 256 We know what the issue was ... Failure: Undefined subroutine &Banana1::camel called at Banana1.pl line 36. We are still alive because eval trapped it munchkin:pl grahamellis$ Learn about this subject
This module and example are covered as required on private courses.
Should you wish to cover this example and associated subjects, and you're attending a public course
to cover other topics with us, please see our extra topic program.
Books covering this topic
Yes. We have over 700 books in our library. Books
covering Perl are listed here and when you've selected a
relevant book we'll link you on to Amazon to order.
Other Examples
This example comes from our "this" training module. You'll find a description of the topic and some
other closely related examples on the "this" module index page.
Full description of the source code
You can learn more about this example on the training courses listed on this page,
on which you'll be given a full set of training notes.
Many other training modules are available for download (for limited use) from our download centre under an Open Training Notes License. Other resources
• Our Solutions centre provides a number of longer technical articles.
• Our Opentalk forum archive provides a question and answer centre. • The Horse's mouth provides a daily tip or thought. • Further resources are available via the resources centre. • All of these resources can be searched through through our search engine • And there's a global index here. Web site author
Purpose of this website
This is a sample program, class demonstration or answer from a
training course. It's main purpose
is to provide an after-course service to customers who have attended our
public private or
on site courses, but the examples are made
generally available under conditions described below.
Conditions of use
Past attendees on our training courses are welcome to use individual
examples in the course of their programming, but must check
the examples they use to ensure that they are suitable for their
job. Remember that some of our examples show you how not to do
things - check in your notes. Well House Consultants take no responsibility
for the suitability of these example programs to customer's needs.
This program is copyright Well House Consultants Ltd. You are forbidden from using it for running your own training courses without our prior written permission. See our page on courseware provision for more details. Any of our images within this code may NOT be reused on a public URL without our prior permission. For Bona Fide personal use, we will often grant you permission provided that you provide a link back. Commercial use on a website will incur a license fee for each image used - details on request. |
| ||||||||||
PH: 01144 1225 708225 • EMAIL: info@wellho.net • WEB: http://www.wellho.net • SKYPE: wellho PAGE: http://www.wellho.net/resources/ex.php4 • PAGE BUILT: Sun Oct 11 14:50:09 2020 • BUILD SYSTEM: JelliaJamb |