From the course: Perl 5 Essential Training

Unlock the full course today

Join today to access over 22,600 courses taught by industry experts or purchase this course individually.

Calling a function with arguments

Calling a function with arguments - Perl Tutorial

From the course: Perl 5 Essential Training

Start my 1-month free trial

Calling a function with arguments

- [Voiceover] In Perl, function arguments are passed using a special default array variable. Here is a working copy of func.pl from Chapter 10 of the Exercise Files. And if I run this, you see it displays the message there. It's being called without any arguments, and so, if I want to pass an argument, I can simply put it inside of the parentheses in the function call operator. And then down here I can say, "Say hello to..." I need to grab the argument first, so I say, dollar name equals shift. This is using the shift function to grab the argument from the default array. And of course down here I say "name", my error should go away. And so, the shift function will use the default array variable if no array variable is specified, and it shifts the value off of that, and returns that value, which is then assigned to the name variable there on line 10, and then displayed in line 11. So when I run this, it says, "Say hello to Fred". So that's pretty simple. You can just use shift to grab…

Contents