Miscellaneous Features of Perl
There are a number of miscellaneous features that Perl has that have not fallen into the previous categories which i will touch on now.
Require Function
This function provides away to break your program into separate files and create libraries of functions. When the Perl interpreter sees the require statement it searchs the directories specified by the built-in array variable @INC for the file specified. The file is executed if found, if not found then the program aborts and displays a error message.
Require Function | require ("myprog.pl"); ## executes the myprog.pl program @proglist = ("test_prog1.pl", "test_prog2.pl", "test_prog3.pl"); require; ## the value of $_ is the file name whose contents are to be executed |
Most of the time however you will use the require function to pull in subroutines from Perl programs that can be used. You can create specific libraries for Databases, Network, Security.
Using subroutines from other Perl programs | ## Main Program (test.pl) require ("function.pl"); ## execute the function.pl program toString(); ## The toString subroutine obtained from function.pl ## Function program (functions.pl) sub toString { |
The last thing require can do is to make sure that you use a specific version of Perl, maybe the program contains syntax that can only be run in version 5.6.
Specify a Perl version | require 5.001; ## your program requires version 5.001 or above to run Note: if you run this on a older version of perl it will terminate with an error |
$#<array> Variable
For each variable defined in your program, a variable named $#<array>, in which array is the name of your array is also defined. This variable contains the subscript of the last element of the array.
$#array example | my @myarray = qw( 1 2 3 4);
$last_subscript = $#myarray; print "Last element in myarray is: " . $last_subscript . "\n"; Note: $[ can affect $#array as $[ determines the first subscript number (by default a 0) - see perl cheat sheet for more information on $[ |
You commonly see this variable being used in the $#ARGV to determine the number of variables passed to a perl program.
Commandline arguments | if ( $#ARGV == -1 ) { ## -1 means no elements die ("No file parameters have been passed"); } |
You can create very large arrays using the $#<array>, or shorten an array
Create very large array | $#veryBigArray = 9999; ## create an array with 10,000 elements Note: if it cannot create the array the program terminates |
Shorten an array | $#varyBigArray = 5000; ## destroy elements from 5001 to 10,000 |
Alternative String Delimiters
You can use single or double quotes for strings, double quotes will search and replace variable names. There are a number of other delimiters choices you can use
Alternative String Delimiters | $test_string = "World";
$string1 = 'Hello $test_string'; print $string1 . "\n"; |
Break string into words | @myarray = qw(hello there it's a nice day); |
Defining Strings Using <<
You can use << to indicate the beginning of a string, this is also known as a here document.
<< example | $longstring = <<END; ## defining a terminatation string (END) - note the semicolon |
You can also use to automatic input information in other programs such as FTP, Telnet
FTP example | usr/bin/ftp<<END; ## END is the terminating string open ftp.datadisk.co.uk ## FTP commands user pvalle password cd public/upload put $file bye END ## here doc finish |
Special Internal Values
You can use three special values in your program
Example | print "Line number is: " . __LINE__ . "\n"; print "Program is: " . __FILE__ . "\n\n"; ## Use data in the file __END__ ## you could have used __DATA__ means the same |
Back Quotes to Invoke System commands
You can run system commands using back quotes, the output of the command will be treated as string.
Example | $myname = `whoami`; ## Unix system command
Note: the return code of the system command is store in system variable $? |