A subroutine is similar to the function() command in PHP except... in PERL.
eg
CODE
#!/usr/bin/perl
&sub1; #Calls the subroutine
sub sub1 { #Creates a subroutine called sub1
print "Hello World\n"; #When the subroutine is called this is what will be printed
}
&sub1; #Calls the subroutine
sub sub1 { #Creates a subroutine called sub1
print "Hello World\n"; #When the subroutine is called this is what will be printed
}
That is just a basic example of a subroutine.
You are also able to add diffrent values to the subroutines
eg:
CODE
#!/usr/bin/perl
&sub1("Hello World"); #Calls the subroutine and specifies the message
sub sub1 { #Creates a subroutine called sub1
my $message = $_[0]; #Retrieves The message from the ()s and adds it to a variable
print "$message \n"; #Prints the message
}
&sub1("Hello World"); #Calls the subroutine and specifies the message
sub sub1 { #Creates a subroutine called sub1
my $message = $_[0]; #Retrieves The message from the ()s and adds it to a variable
print "$message \n"; #Prints the message
}
If you notice in the sub routine I added a my before the $message this is called a private variable and no other routine can call it
You can also call public variables using sub routines eg:
CODE
#!/usr/bin/perl
$hello = "Hello World"; #public or "global" variable
sub sub1 { #Creates a subroutine called sub1
my $message = $hello; #Retrieves The global variable
print "$message \n"; #Prints the message
}
$hello = "Hello World"; #public or "global" variable
sub sub1 { #Creates a subroutine called sub1
my $message = $hello; #Retrieves The global variable
print "$message \n"; #Prints the message
}
or
CODE
#!/usr/bin/perl
$hello = "Hello World";
&sub($hell0); #Calls the subroutine and specifies the message
sub sub1 { #Creates a subroutine called sub1
my $message = $_[0]; #Retrieves The message from the ()s
print "$message \n"; #Prints the message
}
$hello = "Hello World";
&sub($hell0); #Calls the subroutine and specifies the message
sub sub1 { #Creates a subroutine called sub1
my $message = $_[0]; #Retrieves The message from the ()s
print "$message \n"; #Prints the message
}
Thats all I can really think of putting in here at the moment I've covered most the topics Sorry if its a little weird its my first tutorial so forgive me