Main Content

Perl functions such as chop change their input parameters

Archive - Originally posted on "The Horse's Mouth" - 2012-01-10 23:57:25 - Graham Ellis

Typically, functions / named blocks of code take a series of operands / parameters as inputs, and return a result which is saved into another variable - here's an example in Perl:

  $size = length($persname);

Incoming value - read from $persname
Action defined in - length
Outgoing value - saved into $size

However - I have said typically, as in some languages it's possible for a function to amend the input values as well. Take a look at this Perl line:

  $boots = chop($feet);

$boots gets set to the final character of the incoming string in $feet which is modified by the removal of that character. So:

Incoming in $feet: Hello[cr]
Outgoing in $boots: [cr]
Outgoing in $feet: Hello

Once you're aware of this action from the chop function, it works very well for you ... but it comes as a bit of a surprise the first time you see it!

Complete source example (and sample output) [here]. We cover this subject (and of course a lot more!) on our Perl Programming course.