Programming in Perl predefined variables Peter Verhs January 2002

  • View

  • Download

Embed Size (px)

Text of Programming in Perl predefined variables Peter Verhs January 2002

  • Programming in Perlpredefined variablesPeter VerhsJanuary 2002.

  • Predefined VariablesGlobal, module independent variablesuse English; defines English namesOnly the most important variables are detailed here, consult the manual

  • $_ $ARGDefault input and pattern matching variablewhile() reads into $_s/// m// tr// uses $_

  • $nSub patterns of the previous m// or s/// operation

    "apple" =~ m/(.)(.)\2le/;print $1," ",$2;OUTPUT:a p

  • $` $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.

  • $+ $LAST_PAREN_MATCHThe last bracket matched by the last search pattern.

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

  • @+ @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.

  • $. $NR $INPUT_LINE_NUMBERopen(F,"");$l = ;print $.;$l = ;print $.;close F;OUTPUT:12

    Actual value depends on what the $/ record separator is. (See next slide.)

  • $/ $RS $INPUT_RECORD_SEPARATOR A string (not a regexp and not only a single character!) that separates records in the inputundef $/; makes slurp mode (read the whole file in a single read as a big string

  • $/ referencing an integer$/ = \3;open(F,"");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.

  • $| $OUTPUT_AUTOFLUSH$| = 1; to get automatic flush of output after each print statement on the selected channelUseful when used on sockets orSTDERR and STDOUT in debug environment

  • $\$ORS $OUTPUT_RECORD_SEPARATOR$, $OFS $OUTPUT_FIELD_SEPARATOR$, is printed between two items on he print list$\ is printed after each print statementBoth are null string by default

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

  • $@ $EVAL_ERRORThe 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";

  • $$ $< $> $( $)

    $$ $PID $PROCESS_IDProcess ID (read only)$< $UID $REAL_USER_IDReal user ID of the process$> $EUID $EFFECTIVE_USER_IDEffective user id$( $GID $REAL_GROUP_IDThe real group id of the process$) $EGID $EFFECTIVE_GROUP_IDThe effective group id of the process

  • $0 $PROGRAM_NAMEThe name of the programOn some system if you assign value to this variable that name may be seen on the output of the program ps

  • $[This is 0 and indicates the first index of an arrayDo not ever change it!!!

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

  • $^O $OSNAMEThe name of the operating system the program runs onOn my test NT it prints:

    MSWin32 using ActivePerlcygwin using Cygwin Perl

  • @_Contains the arguments passed to the subroutinesub a { print $_[0],$_[1],$_[2]; }a 1,2,3;OUTPUT:123

  • %ENVThe environment variablesChanging this hash changes the environment for any sub process

    This variable is heavily used in CGI programs

  • %SIGsub 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

  • Thank you for your kind attention.