Jun 22, 2008

Different ways of calling a method

a) We are calling the method after the definition

#!F:\Perl\bin\perl -w
use strict;

sub method {
print " \n Inside the method";
}

method; #Works as you expect
&method #Works as you expect, '&' is the general way of calling a method
method(); #Works as you expect since 'method' is not taking any parameters


b) We are calling the method before the definition itself

#!F:\Perl\bin\perl -w
use strict;

method; #Error
&method #Works as you expect, '&' is the general way of calling a method
method(); #Works as you expect since 'method' is not taking any parameters

sub method {
print " \n Inside the method";
}


So before the definition of a function if you call the function,you hav to compulsarily use either '()' [Eg: method()] or '&' [Eg: &method]

No comments:

Post a Comment