Jump to content


Photo
- - - - -

Sub Routines in Perl


  • Please log in to reply
No replies to this topic

#1 Hit3k

Hit3k

    Young Padawan

  • Members
  • Pip
  • 120 posts
  • Gender:Male
  • Location:Australia

Posted 05 August 2006 - 09:28 PM

To use this tutorial you should have a basic understanding of PERL


A subroutine is similar to the function() command in PHP except... in PERL.

eg

#!/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
}


That is just a basic example of a subroutine.

You are also able to add diffrent values to the subroutines

eg:

#!/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
}

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:
#!/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
}

or

#!/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
}
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 :D and keep coding! :P

Edited by Hit3k, 05 August 2006 - 09:32 PM.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users