Programming in Perl

Download Report

Transcript Programming in Perl

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
• while(<>) reads into $_
• s/// m// tr// uses $_
$n
• Sub 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 a
mat ppl
pos e
Due to Perl implementation
bugs there is performance
penalty using any of these
variables.
$+ $LAST_PAREN_MATCH
• The 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 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 nth sub match.
$. $NR $INPUT_LINE_NUMBER
open(F,"test.pl");
$l = <F>;
Actual value depends on what
print $.;
the $/ record separator is.
(See next slide.)
$l = <F>;
print $.;
close F;
OUTPUT:
12
$/ $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
$/ 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.
$| $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
$\
$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
$? $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_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";
$$
$<
$>
$(
• $$ $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
$)
$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
$[
• This is 0 and indicates the first
index of an array
• Do not ever change it!!!
$]
• The version + patch level /1000 of
the actual interpreter
$^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
@_
• Contains the arguments passed to
the subroutine
sub a {
print $_[0],$_[1],$_[2];
}
a 1,2,3;
OUTPUT:
123
%ENV
• The environment variables
• Changing this hash changes the
environment for any sub process
• This variable is heavily used in CGI
programs
%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
Thank you for your kind attention.