Page 1 of 1

Perl Special Variables There's a lot of them! Rate Topic: -----

#1 jumptrooper   User is offline

  • D.I.C Head
  • member icon

Reputation: 68
  • View blog
  • Posts: 234
  • Joined: 19-August 10

Posted 31 August 2010 - 12:42 PM

There are over 50 "special variables" in Perl, which are used by default to store various kinds of information. They cannot be overriden and they can be used either implicitly or explicitly. For example: $_ is used as the default variable for input and pattern-matching. If you read input from a file, or the user, and do not specify a variable to store that input, it gets stored in $_.

Here is a list of Perl's special variables and a brief description of what they're for. (This is taken from "Perl 5 How-To Second Edition")

$_ ----- Default input and pattern-searching space.
$[digit]- Contains the subpattern from a successful parentheses pattern match (Ex: $2, to refer to the second set of parentheses in a regex)
$& ----- The string from the last successful pattern match
$` ----- (That's a back-tick - the other symbol on the "~" key) The preceding string to the last successful pattern match
$' ----- The string following the last successful pattern match
$+ ----- The last bracket matched from the last search pattern
$x ----- Controls internal string multiline pattern matching (default value = 0)
$. ----- The current input line number of last filehandle read
$/ ----- The input record separator (Default value = "\n")
$| ----- If set to nonzero, forces a flush of th currently selected stream after every write (Default value = 0)
$, ----- The output field separator for the print command
$" ----- The separator that joins elements of arrays interpolated in strings (Default value = space)
$\ ----- The output record separator for the print command
$; ----- The subscript separator for multidimensional array emulation.
$# ----- The output format for printed numbers
$% ----- The page number of the currently selected output stream
$= ----- The page length of hte currently selected output stream
$ ----- The numbe of lines left on the current page
$: ----- The characters used to fill a continuation field. ("\n-" by default)
$? ----- The status value returned from the last system, pipe close, or back-tick command
$! ----- Contains the current value of errno.
$@ ----- The Perl syntax error from th last eval statement
$$ ----- The process ID (PID) of the current running Perl script
$< ----- The real user ID (UID) of the current running process
$> ----- The effective UID of the current running process
$( ----- The real group ID (GID) of the current running process
$) ----- The effective GID of hte current running process
$0 ----- The name of the file of the Perl script
$[ ----- The index of the first element of an array. (Default = 0)
$] ----- The string printed out when Perl is run with the -v command line option
$ A ----- The accumulator for form line and write operations
$ D ----- The current value of the debugging flags
$ F ----- The maximum number of system file descriptors (Default = 2)
$ I ----- Contains the current value of the in-place editing flag (-i)
$ P ----- Internal debugging flag
$ T ----- The time in which the script began running. The time is in seconds since January 1, 1970.
$ W ----- The current value of hte warning switch
$ X ----- The name of the Perl binary that was executed
$ARGV --- The name of the current file when reading from <>
@ARGV --- The command line arguments issued when the script was started
@EXPORT - The list of methods the package will export be default
@EXPORT_OK The list of methods the package will export by request
@INC ---- The include path of directories to search looking for libraries or Perl scripts to be evalutated by the do command
@ISA ---- The list of base classes of the package
@_ ----- The parameter array for subroutines
%ENV ---- This associative array contains your current environment
%INC ---- This associative array contains a record for each entry required using do or require
%OVERLOAD Used to overload operators in a package
%SIG ---- This associative array contains signal handlers for various signals. This is set by the programmer so initially there are no signals trapped unless the programmer has explicitly stated them in the script.

Of these special variables, $_, @_, $[digit], $!, and $ARGV are the one's I've used most. So here are some examples of how they can be used.

foreach( @myarray ){
	print $_;		#using $_ explicitly

# or...
foreach( @myarray ){
	print;		#using $_ implicitly

&mySubroutine(5, 6, 7, 8);
&myOtherSub(1, 2, 3, 4);

sub mySubroutine {
	@valuesPassed2Sub = @_;	#using @_ explicitly to store all values in local array
	print @valuesPassed2Sub;

sub myOtherSub {		
	#NOTE: "shift()" pops the first element off an array, making the next element the first
	$a = shift;	#using shift on @_ implicitly to store first parameter in $a
	$b = shift;	#using shift on @_ implicitly to store second parameter in $b
	$c = shift(@_);	#using shift on @_ explicitly to store third parament in $c
	$d = shift(@_):	#...and the last parameter

#reading through an html file line by line and printing out only the links
foreach(<FILEHANDLE>){	#go through each line of the file
	$_ =~ m/(<a href*\/a>)/	#find anything that matches an html link and store it in $1
	print qq($1\n);	#print each stored link on a new line

#try to open a directory for reading, if perl can't say "Can't...." and print the system error that would normally display to the console
opendir(DIRECTORYHANDLE, $directoryName) or die "Can't open directory because: $!\n";
@files = readdir DIRECTORYHANDLE;


[when calling your script]
perl myPerlScript.pl blah blah 3 **press enter**
@valuesPassedInFromOutside = @ARGV;

$a = shift(@ARGV);	#"blah"
$b = shift(@ARGV);	#"blah"
$c = shift(@ARGV);	#"3"

Hope this helps!

Is This A Good Question/Topic? 2
  • +

Page 1 of 1