
Romeo wants to take his girlfriend Julie out to
The Jolly Judge tomorrow night, but Julie's Dad (who really doesn't approve of young Romeo) has shut her in her room on the first floor of their home, which as you can see is on the top of a substantial mound.
Being a resourceful lad, Romeo is going to nip down to his local
B & Q Store and nick a couple of ladders (it's this habit of his of nicking things that has put Julie's Dad off him, by the way) to get her out.
Your task ... if you're on
our Perl Course ... is to write a program to help Romeo work out how long the two ladders he'll need will be:
• The mount is 12 metres tall, and there's a good grounding about 5 metres away from the base
• the window of Julie's room is 4 metres up from the ground, but Dad has placed a line of holly bushes just under Julie's window, so Romeo is going to have to foot the ladder 3 metres out.
The algorithm you need is:
$ladlen = sqrt($w * $w + $h * $h);
and you're asked to write that in a separate sub / in a separate file, so that you can easily re-use the same code later if you need too (since Romeo is a bit of a jack-the-lad by all accounts, it would be a shame to write code just for him rather than have it available for re-use, wouldn't it?)
There's a sample answer - source code of the main program
here, and you can find the file that contains the sub
here.
If you ARE here because you're a Perl programmer, do have a look at the source which is rather thoroughly commented - and contains examples of things like my v our, package definition, and the use of the wantarray function. (written 2008-12-04, updated 2008-12-03)
Associated topics are indexed as below, or enter http://melksh.am/nnnn for individual articles
P209 - Subroutines in Perl [96] Variable Scope - (2004-10-22)
[308] Call by name v call by value - (2005-05-11)
[357] Where do Perl modules load from - (2005-06-24)
[531] Packages in packages in Perl - (2005-12-16)
[588] Changing @INC - where Perl loads its modules - (2006-02-02)
[775] Do not duplicate your code - (2006-06-23)
[969] Perl - $_ and @_ - (2006-12-07)
[1163] A better alternative to cutting and pasting code - (2007-04-26)
[1202] Returning multiple values from a function (Perl, PHP, Python) - (2007-05-24)
[1782] Calling procs in Tcl and how it compares to Perl - (2008-09-02)
[1784] Global - Tcl, PHP, Python - (2008-09-03)
[1826] Perl - Subs, Chop v Chomp, => v , - (2008-10-08)
[1850] Daisy the Cow and a Pint of Ginger Beer - (2008-10-21)
[1860] Seven new intermediate Perl examples - (2008-10-30)
[2069] Efficient calls to subs in Perl - avoid duplication, gain speed - (2009-03-07)
[2550] Do not copy and paste code - there are much better ways - (2009-12-26)
[2833] Fresh Perl Teaching Examples - part 2 of 3 - (2010-06-27)
[2929] Passing a variable number of parameters in to a function / method - (2010-08-20)
[3066] Separating groups of variables into namespaces - (2010-11-24)
[3574] Perl functions such as chop change their input parameters - (2012-01-10)
[3833] Learning to use existing classes in Perl - (2012-08-10)
Some other Articles
Introduction to Object Oriented ProgrammingPreventing ^C stopping / killing a program - PerlMaking it all worthwhileFlurinci knows Raby Lae PHP and JeveRomeo and JulieProgress Bar Techniques - PerlTransitionPerl Socket Programming ExamplesOut of memory during array extend - PerlWhy I remember East Grafton