In this post i will explain some subroutine in Perl.
1)AUTOLOAD subroutine:
In perl , if this subroutine is defined in the package, then if any undefined subroutine in that package is called then then the statements of this AUTOLOAD subroutine are executed.
For example in below example i have called a subroutine
&subroutine_not_present(12,23,"hi");
which is not present.When this program is executed and perl will not found the called subroutine it will call the AUTOLOAD subroutine which is present in the program.
2)BEGIN Subroutine:
If the program contains any subroutine named as BEGIN then that subroutine will be executed first before executing any other statement of that program.A program can have more than one BEGIN subroutine.If this is the case then these are executed on the basis of order in which they have defined.
1)AUTOLOAD subroutine:
In perl , if this subroutine is defined in the package, then if any undefined subroutine in that package is called then then the statements of this AUTOLOAD subroutine are executed.
For example in below example i have called a subroutine
&subroutine_not_present(12,23,"hi");
which is not present.When this program is executed and perl will not found the called subroutine it will call the AUTOLOAD subroutine which is present in the program.
Program Code:
#! /Perl/bin/perl
&subroutine_not_present(12,23,"hi");
AUTOLOAD
{
print("Can't find $AUTOLOAD subroutine\n");
print("Argumens to that subroutine are @_");
}
Output:#! /Perl/bin/perl
&subroutine_not_present(12,23,"hi");
AUTOLOAD
{
print("Can't find $AUTOLOAD subroutine\n");
print("Argumens to that subroutine are @_");
}
2)BEGIN Subroutine:
If the program contains any subroutine named as BEGIN then that subroutine will be executed first before executing any other statement of that program.A program can have more than one BEGIN subroutine.If this is the case then these are executed on the basis of order in which they have defined.
Program Code:
#! /Perl/bin/perl
BEGIN{
print("I am printing from BEGIN subroutine\n");
print("And interesting thing is that \nwe don't need to call this routine\n");
print("It is automatically called\n");
}
BEGIN{
print("And another interesting fact about this is that there can be\n");
print("more than one BEGIN routines in a single program.\n");
print("They are called in the order in which they are specified in the program.");
}
Output:
#! /Perl/bin/perl
BEGIN{
print("I am printing from BEGIN subroutine\n");
print("And interesting thing is that \nwe don't need to call this routine\n");
print("It is automatically called\n");
}
BEGIN{
print("And another interesting fact about this is that there can be\n");
print("more than one BEGIN routines in a single program.\n");
print("They are called in the order in which they are specified in the program.");
}
3)END Subroutine:
This subroutine is called after the last statement of the program has been executed.A program can contain more than one END subroutine.If this is the case then these are executed in the reverse order.
Program Code:
#! /Perl/bin/perl
END{
print("And another interesting fact about this is that there can be\n");
print("more than one END routines in a single program.\n");
print("They are called in the opposite order in which they are specified in the program.");
}
END{
print("I am printing from END subroutine\n");
print("And interesting thing is that \nwe don't need to call this routine\n");
print("It is automatically called\n");
}
#! /Perl/bin/perl
END{
print("And another interesting fact about this is that there can be\n");
print("more than one END routines in a single program.\n");
print("They are called in the opposite order in which they are specified in the program.");
}
END{
print("I am printing from END subroutine\n");
print("And interesting thing is that \nwe don't need to call this routine\n");
print("It is automatically called\n");
}
Output:
0 comments:
Post a Comment