- Home
- Interview Questions
- Perl
The reset function affects all variables, including system variables. For this reason, you should be careful when you use reset
Yes. For example, $#ARGVcontains the largest subscript of the @ARGVarray; you can test this to determine whether your program was passed enough arguments
The bless() function takes one or two arguments. The first argument is a reference to an object. The second argument is optional and specifies the name of a class; if the name is not specified, the default is the current class. After the call, the reference uses the name as its class name. As a result, the reference becomes an object of the class whose name was specified.
We need to Make sure we are using the require Exporter;statement and that the name of the new function is in the @EXPORTER array.
They're not; they both refer to the same conceptual things. In this lesson I simply
decided to make the distinction so there would not be any confusion about calling
built-in functions versus functions you define yourself.
Note that subroutines you define are, in fact, different from the built-in functions;
subroutines don't control the number or type of arguments they can receive (at least,
not without prototypes), and the rules of whether you can leave off the parentheses
for subroutines are different. Future versions of Perl are working toward making
programmer-defined subroutines and built-in subroutines closer in behavior.
The best way to do this is to use references, Another way is to modify global variables inside the subroutine, rather than passing the array data in via an argument list. Yet another way is to compress the arrays into single scalar values (using delimiters), and then expanding them back into arrays inside your subroutine.
Subroutines are useful for a number of reasons:
They help break up a large script into smaller bits to help manage complexity
Procedures in subroutines can be referred to by name to make scripts more
readable
Subroutines can define local variables that are more efficient, easier to manage
and control than globals
If you use the same code repeatedly throughout your programs, you can put
that code into a subroutine, and then just reuse it
Smaller chunks of scripts can be more easily developed and debugged
Call a subroutine using one of these forms:
&thisubroutine(); # both % and parens
&thissubroutine(1,2); # with arguments
thissubroutine(1,2); # & is optional
Define a subroutine like this:
sub name {
# body of subroutine
}