Presentation is loading. Please wait.

Presentation is loading. Please wait.

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

Similar presentations


Presentation on theme: "Programming in Perl predefined variables Peter Verhás January 2002."— Presentation transcript:

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

2 Predefined Variables Global, module independent variables use English; defines English names Only the most important variables are detailed here, consult the manual

3 $_ $ARG Default input and pattern matching variable while(<>) reads into $_ s/// m// tr// uses $_

4 $n Sub patterns of the previous m// or s/// operation "apple" =~ m/(.)(.)\2le/; print $1," ",$2; OUTPUT: a p

5 $` $PREMATCH $& $MATCH $’ $POSTMATCH $_ = "apple"; /ppl/; print "pre $`\n"; print "mat $&\n"; print "pos $'\n"; OUTPUT: pre a mat ppl pos e Due to Perl implementation bugs there is performance penalty using any of these variables.

6 $+ $LAST_PAREN_MATCH The last bracket matched by the last search pattern. /Version: (.*)|Revision: (.*)/ && ($rev = $+);

7 @+ @LAST_MATCH_END @- @LAST_MATCH_START $_ = "appleeeee"; # 012345678 /(.)\1(.)e/; print $-[0]," ",$+[0],"\n"; print $-[1]," ",$+[1],"\n"; print $-[2]," ",$+[2],"\n"; OUTPUT: 1 5 1 2 3 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 n th sub match.

8 $. $NR $INPUT_LINE_NUMBER open(F,"test.pl"); $l = ; print $.; $l = ; print $.; close F; OUTPUT: 12 Actual value depends on what the $/ record separator is. (See next slide.)

9 $/ $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

10 $/ referencing an integer $/ = \3; open(F,"test.pl"); while( ){ print "$_|"; } close F; OUTPUT: $/ |= \|3; |ope|n(F|,"t|est|.pl|");| wh|ile|( |){ | 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.

11 $| $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

12 $\$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

13 $? $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

14 $@ $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 EOF but we run fine 1 print "1\n";

15 $$ $ $( $) $$ $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

16 $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

17 $[ This is 0 and indicates the first index of an array Do not ever change it!!!

18 $] The version + patch level /1000 of the actual interpreter

19 $^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

20 @_ Contains the arguments passed to the subroutine sub a { print $_[0],$_[1],$_[2]; } a 1,2,3; OUTPUT: 123

21 %ENV The environment variables Changing this hash changes the environment for any sub process This variable is heavily used in CGI programs

22 %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

23 Thank you for your kind attention.


Download ppt "Programming in Perl predefined variables Peter Verhás January 2002."

Similar presentations


Ads by Google