More perl - PowerPoint PPT Presentation

1 / 7
About This Presentation
Title:

More perl

Description:

the last line rad from a file. functions with no argument operates on $_ Array commands ... mode ('.$octal.')n'; print('In order: LEAST secure first!nn' ... – PowerPoint PPT presentation

Number of Views:18
Avg rating:3.0/5.0
Slides: 8
Provided by: iuH
Category:
Tags: more | octal | perl

less

Transcript and Presenter's Notes

Title: More perl


1
More 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

2
More perl
  • Examples (hash array)

!/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")
3
More perl
  • Example (reading a file)

!/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)
4
More perl
  • Regular expressions

!/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)
5
More perl
  • Example (substitution)

!/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)
  • Example (translation)

!/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)
6
More perl
  • Example (many things)

!/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")
7
More 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)
Write a Comment
User Comments (0)
About PowerShow.com