tayade.blogg.se

Perl tutorials
Perl tutorials







perl tutorials

Here's how I would now call this new version of the hello subroutine: Now that our hello subroutine returns a string, our calling program needs to be modified to receive that string. Getting return values from Perl subroutines

#Perl tutorials code

I think the code is more clear with the Perl return operator, but again, it is optional. You can write that same subroutine without the return operator, as shown below, and in fact, most Perl developers seem to do this: To return one parameter to the calling program, your subroutine can look like this:Īn interesting thing about Perl subroutines is that the Perl return operator is optional. Perl subroutines can also return values when they are called. If my subroutine was written to take both the first name and last name of the user (it currently is not), the subroutine call would now look like this: To pass an argument to a Perl subroutine, just add the argument to the subroutine call like you normally would when calling any Perl function: Passing arguments to a Perl sub (subroutine) To be more clear, you will not be able to access the $name variable outside of the hello function because I have declared $name with the Perl my operator, as shown. Makes the variable $name local in scope to the hello function. So, in the example shown above, this code: Oops, I almost forgot: The Perl "my" operator makes the variable after the my keyword private to the Perl subroutine. The shift approach is generally easier to use and remember (so I probably should show it here first), but I wanted to show the numbered array example so you can see more of what's going on here. You can also access Perl subroutine arguments using the shift operator, like this:Įither approach to accessing Perl subroutine arguments can be used. The second argument to your Perl sub is accessed with the $_ element, and so on. Therefore, when you need to access the first element passed in to your Perl subroutines, you use the $_ syntax, as shown in that example. # expect a person's name to be passed in, then print hello to them.Īrguments to Perl subroutines are made available via the special array. Here's what this new subroutine looks like: Once you've created a simple Perl subroutine that takes no arguments, you'll want to be able to create one that does take arguments.įor our purposes, we'll extend our current Perl function to take one argument, and we'll print that argument. Perl subroutines - Accessing subroutine arguments When you run it, the 'Hello, world.' string shown will be printed. If this script is named, we can run it from the command line like this: Putting these two pieces of code together, we currently have a Perl script that looks like this: To call this simple Perl subroutine, just use this syntax: To define a simple Perl subroutine, just use the following Perl "sub" syntax:Īs you can see, this simple Perl subroutine (function) should print "Hello, world." when it is called. In this article I'll try to briefly cover each of these Perl subroutine questions. How do I return a value from a Perl subroutine?.How do I access arguments in a Perl subroutine?.How do I pass arguments to a Perl subroutine?.Perl subroutines FAQ - As a developer, when you start working with subroutines in Perl, you'll probably have the same questions I did:









Perl tutorials