Title: More perl
1More perl
- Special scalar
- _ is used as a buffer
- output of the last command
- the last line rad from a file
- functions with no argument operates on _
- Array commands
- Argument vector _at_ARGV
- last_element ARGVARGV
- next_element shift(_at_ARGV)
- split splits _ on spaces into an array
- (var1, var2...) split(, line)
splits a string on into scalars
2More perl
!/usr/bin/perl -w animal"horse" "does
work" animal"lion" "eats you" animal"cow"
"gives milk" foreach var (sort keys
animal) print("var animalvar\n")
3More perl
!/usr/bin/perl -w while(ltgt)
print ------------------------- !/usr/bin/per
l -w open(FILE, "ARGV0") while(ltFILEgt)
print _ close(FILE) -------------------------
!/usr/bin/perl -w open(FILE,
"ARGV0") while(line ltFILEgt) print
line close(FILE)
4More perl
!/usr/bin/perl -w open(FILE, "/bin/ps aux
") while(ltFILEgt) print if
/httpd/ close(FILE) --------------------------
----- !/usr/bin/perl -w open(FILE, "/bin/ps
aux ") while(line ltFILEgt) if (line
/httpd/) print line close(FILE)
5More perl
!/usr/bin/perl -w open(FILE, "text") while(lin
e ltFILEgt) line s/ /_/g
substitute every blank to _ line
s/er/var/g substitute every string er to
var print line close(FILE)
!/usr/bin/perl -w open(FILE, "text") while(lin
e ltFILEgt) line tr/ab/ba/
switch a's and b's line tr/a-z/A-Z/
translate to capital letters print
line close(FILE)
6More perl
!/usr/bin/perl -w if (ARGV lt 1) print("Not
enough arguments\n") exit(1) while
(next_arg shift(_at_ARGV)) if (!(-f next_arg
-d next_arg)) print("No such file
next_arg") next (dev,ino,mode,nli
nk,uig,gid,rdev,size) stat(next_arg)
octal sprintf("o",mode 0777)
ass_arrayoctal . next_arg . " size
(".size."), mode (".octal.")\n" print("In
order LEAST secure first!\n\n") foreach file
(reverse sort keys(ass_array))
print("ass_arrayfile")
7More perl
- Example (copy several lines to a file)
open(FILE,gt/home/user/filename) print FILE
ltltENDMARKER From here every line is copied as it
is. No formatting is done. You can still expand
variables like var if you are in a
loop ENDMARKER foreach item (_at_array_of_something)
print FILE ltltENDMARKER The file is still
open and you can append different values of
item ENDMARKER print FILE ltltENDMARKER Here
you can finalize the file in some
way ENDMARKER close(FILE)