Programming in Perl predefined variables

23
Programming in Perl predefined variables Peter Verhás January 2002.

description

Programming in Perl predefined variables. Peter Verhás January 2002. Predefined Variables. Global, module independent variables use English; defines English names Only the most important variables are detailed here, consult the manual. $_ $ARG. Default input and pattern matching variable - PowerPoint PPT Presentation

Transcript of Programming in Perl predefined variables

Page 1: Programming in Perl predefined variables

Programming in Perlpredefined variables

Peter VerhásJanuary 2002.

Page 2: Programming in Perl predefined variables

Predefined Variables

• Global, module independent variables

• use English; defines English names

• Only the most important variables are detailed here, consult the manual

Page 3: Programming in Perl predefined variables

$_ $ARG

• Default input and pattern matching variable

• while(<>) reads into $_• s/// m// tr// uses $_

Page 4: Programming in Perl predefined variables

$n

• Sub patterns of the previous m// or s/// operation

"apple" =~ m/(.)(.)\2le/;

print $1," ",$2;

OUTPUT:a p

Page 5: Programming in Perl predefined variables

$` $PREMATCH$& $MATCH

$’ $POSTMATCH

$_ = "apple";/ppl/;print "pre $`\n";print "mat $&\n";print "pos $'\n";OUTPUT:pre amat pplpos e

Due to Perl implementation bugs there is performance penalty using any of these

variables.

Page 6: Programming in Perl predefined variables

$+ $LAST_PAREN_MATCH

• The last bracket matched by the last search pattern.

/Version: (.*)|Revision: (.*)/ && ($rev = $+);

Page 7: Programming in Perl predefined variables

@+ @LAST_MATCH_END@- @LAST_MATCH_START

$_ = "appleeeee";# 012345678/(.)\1(.)e/;print $-[0]," ",$+[0],"\n";print $-[1]," ",$+[1],"\n";print $-[2]," ",$+[2],"\n";

OUTPUT:1 51 23 4

$+[0] is the position after, $-[0] is the position start the last match, $+[n] is the position after, $-[n] is the position start the nth sub match.

Page 8: Programming in Perl predefined variables

$. $NR $INPUT_LINE_NUMBER

open(F,"test.pl");

$l = <F>;

print $.;

$l = <F>;

print $.;

close F;

OUTPUT:12

Actual value depends on what the $/ record separator is.

(See next slide.)

Page 9: Programming in Perl predefined variables

$/ $RS $INPUT_RECORD_SEPARATOR

• A string (not a regexp and not only a single character!) that separates records in the input

• undef $/; makes slurp mode (read the whole file in a single read as a big string

Page 10: Programming in Perl predefined variables

$/ referencing an integer

$/ = \3;open(F,"test.pl");while( <F> ){ print "$_|"; }close F;OUTPUT:$/ |= \|3;|ope|n(F|,"t|est|.pl|");|wh|ile|( <|F> |){| p|rin|t $|_,"||";| |}c|los|e F|;|

Reads at most the referenced number of bytes from the file.

On VMS or other systems where records are supported reads a record but at most

that number of bytes.

Page 11: Programming in Perl predefined variables

$| $OUTPUT_AUTOFLUSH

• $| = 1; to get automatic flush of output after each print statement on the selected channel

• Useful when used on sockets or• STDERR and STDOUT in debug

environment

Page 12: Programming in Perl predefined variables

$\ $ORS $OUTPUT_RECORD_SEPARATOR$, $OFS $OUTPUT_FIELD_SEPARATOR

• $, is printed between two items on he print list

• $\ is printed after each print statement

• Both are null string by default

Page 13: Programming in Perl predefined variables

$? $CHILD_ERROR $! $ERRNO $OS_ERROR

• $? is the error code of the last system() call, ` ` operator or popen/pclose

• $! is the code of errno after a system call

Page 14: Programming in Perl predefined variables

$@ $EVAL_ERROR

• The Perl syntax error message from the last eval() operator.$a = "print \"1\\n\";\nwhat is this?";eval $a;print $a,"\n",$@;print "but we run fine\n";$a = "print \"1\\n\";";eval $a;print $a,"\n",$@;OUTPUT:print "1\n";what is this?syntax error at (eval 1) line 3, at EOFbut we run fine1print "1\n";

Page 15: Programming in Perl predefined variables

$$ $< $> $( $)

• $$ $PID $PROCESS_ID– Process ID (read only)

• $< $UID $REAL_USER_ID– Real user ID of the process

• $> $EUID $EFFECTIVE_USER_ID– Effective user id

• $( $GID $REAL_GROUP_ID– The real group id of the process

• $) $EGID $EFFECTIVE_GROUP_ID– The effective group id of the process

Page 16: Programming in Perl predefined variables

$0 $PROGRAM_NAME

• The name of the program• On some system if you assign

value to this variable that name may be seen on the output of the program ps

Page 17: Programming in Perl predefined variables

$[

• This is 0 and indicates the first index of an array

• Do not ever change it!!!

Page 18: Programming in Perl predefined variables

$]

• The version + patch level /1000 of the actual interpreter

Page 19: Programming in Perl predefined variables

$^O $OSNAME

• The name of the operating system the program runs on

• On my test NT it prints:

– MSWin32 using ActivePerl– cygwin using Cygwin Perl

Page 20: Programming in Perl predefined variables

@_

• Contains the arguments passed to the subroutine

sub a {

print $_[0],$_[1],$_[2];

}

a 1,2,3;

OUTPUT:123

Page 21: Programming in Perl predefined variables

%ENV

• The environment variables• Changing this hash changes the

environment for any sub process

• This variable is heavily used in CGI programs

Page 22: Programming in Perl predefined variables

%SIG

sub handler { # 1st argument is signal name my($sig) = @_; print "Caught a SIG$sig--shutting down\n"; close(LOG); exit(0); } $SIG{'INT'} = \&handler; $SIG{'QUIT'} = \&handler; ... $SIG{'INT'} = 'DEFAULT'; # restore default action $SIG{'QUIT'} = 'IGNORE'; # ignore SIGQUIT

INT is the signal for CONTROL-C

Page 23: Programming in Perl predefined variables

Thank you for your kind attention.