1. Perl
  2. Subroutine
  3. here

Compare the flexibility of Perl subroutine with C

Here is a summary of the features of Perl subroutine. I compared it with C language. You can see the flexibility of Perl as a language. * 1

Perl Subroutine Features

Twice Perl C language
1 Subroutine definition can be done anywhere In general, a function forward declaration (prototype declaration) is required
2 There is no need to declare the argument type The argument type must be declared.
3 There is no need to declare the return type The return type must be declared.
Four Receives variable arguments by default It is necessary to decide the number of arguments at the time of declaration
Five List can be used as return value Array cannot be a return value
6 A reference to a lexical variable can be used as a return value Cannot return the address of a local variable
7 There is a concept of context There is no concept of context
8 There is a namespace to which the subroutine belongs There is no namespace to which the function belongs

1. Subroutines can be defined anywhere

# perl (Subroutine definition can be done anywhere)

# func () Can also be used here

sub func {

}

# func () Can also be used here
/ * C language (generally requires a forward declaration) */
int func (int x);/* Function prototype declaration */
func (3)/* Here, a forward declaration (prototype declaration) is required to use it */
int func (int x) {

}

Perl looks for the name of the subroutine in the symbol table at run time. So you can write anywhere.

In C, function names are resolved at compile time. If the function definition is after the place where the function is used, the forward declaration (prototype declaration) must be made before the place where the function is used.

2. 3. 4. It is not necessary to determine the argument type, the number of arguments, and the return type.

# Perl (The first thing to decide is the name of the subroutine.)
sub func {

}
/ * C language (It is necessary to decide the argument type, the number of arguments, and the return type.) */
int func (int x, int y) {

}

In Perl, the argument type, the number of arguments, and the return type are determined at run time. In C, the argument type, the number of arguments, and the return type are determined at compile time.

5. List can be used as return value

# Perl (list can be used as return value)

sub func {
    return(1, 2, 3);
}
/ * C language (To return an array, you need to manipulate the pointer received as an argument) */
int ary [100];
fucn (ary);/* Pass a pointer */
void func (int * ary) {
/* Now you need to assign a value to ary through a pointer. */}

In Perl, you can use a list as a return value. In C language, an array cannot be returned as a return value. If you want to return the array to the caller, you need to pass it as a pointer as an argument and operate it.

6. A reference to a lexical variable can be used as a return value

# Perl
sub func {
  my $val = 1;
  return \$val;
}
/ * C language */int func () {
    int x;
    return & x;
}

In Perl, a lexical variable are not destroyed from memory as long as they are referenced from somewhere. In C, a local variable are discarded when the function ends.

7. There is a concept of context

Perl can use a scalar or list as the return value of a function, depending on the context. There is no such concept in C language.

8. There is a namespace to which the subroutine belongs

# Perl
# The real name is main::func (func subroutine belonging to the main package)
sub func {

}

package SomePackage;
# The real name is SomePackage::func
# (Func subroutine belonging to SomePackage package)
# It's completely different from the above func
sub func {

}
/ * C language *// * func is func, which is func from any point of view in the program. */int func () {

}

In Perl, all subroutine belong to namespaces. In Perl, namespaces are called packages. C language does not have a namespace.

Note

* 1 I'm not criticizing C, so I'm not sure. C has the characteristics of "fast execution speed", "programming from hardware", and "good at low-level data manipulation". Perl is not good at this.

Related Informatrion