140 likes | 259 Views
CSC 4630. Perl 5 D. Goelman, April 2013. Content of This Presentation. Command-line arguments Processing files Regular expressions Hashes
E N D
CSC 4630 Perl 5 D. Goelman, April 2013
Content of This Presentation • Command-line arguments • Processing files • Regular expressions • Hashes There’s really just a taste of each topic, with a sample script or two that illustrate it. For more details, see the Llama book, the Cozens tutorial, or what web searches come up with.
Command-line arguments (1) • @ARGV is the array of those arguments • C and Bourne shell users, take care: • $ARGV[0] is the first argument, not the name of the script itself (which is $0 in Perl) • The “special scalar variable” $_ also appears in some examples • If the looping variable isn’t explicitly declared • If we’re reading lines from <STDIN> etc.
Command-line arguments (2) #! /usr/bin/perl -w # or /opt/local/bin/perl -w # idargs print "Here are the command-line args: \n"; foreach $argument (@ARGV) { print "$argument \n"; } To run: $ idargs first second third Or: $ perlidargs first second third
Command-line arguments (3) Here’s how $_ could be used in the same script: #! /usr/bin/perl -w # or the other path if needed # idargsTOO print "Here are the command-line args: \n"; foreach (@ARGV) { print "$_\n"; }
Processing files (1) • open FH, “somefile” connects the filehandle FH with the external file • Often have program “die” if open fails • Often with $! for including error message • Then <FH> used to read lines from the file • In examples, declaring a variable with “my” is Perl’s version of automatic variables (but they are called “lexical” in Perl)
Processing files (2) #! /usr/bin/perl -w # processnamedfile (in this example, it's called "idargs“) print "trying to open \"idargs\" now\n"; open THEFILE, "idargs" or die $!; print "It worked!\n"; # since we got here! my $lineno = 1; while (<THEFILE>) { print $lineno; print ": $_"; $lineno++; }
Processing files (3) #! /usr/bin/perl -w # processfilearg print "trying to process first command-line "; print "argument, which is a file\n"; open THEFILE, $ARGV[0] or die $!; print "It worked!\n"; # since we got here! my $lineno = 1; while (<THEFILE>) { print $lineno; print ": $_"; $lineno++; }
Regular expressions (1) • Format is pretty much $stringvar =~ /regex/ • The regular expressions known to Perl include the ones we’ve seen, and more
Regular expressions (2) #! /usr/bin/perl -w # findastring: locates lines of $ARGV[0] that contain the string “args” print "Here is the first command-line arg, a file to be opened: \n"; print "$ARGV[0]\n"; open THEFILE, $ARGV[0] or die $!; my $lineno = 1; while (<THEFILE>) { if ($_ =~ /args/) { print $lineno; # like grep -n print ": $_"; } $lineno++; }
Regular expressions (3) #! /usr/bin/perl -w # justOnei print "Here is the first command-line arg, a file to be opened: \n"; print "$ARGV[0]\n"; open THEFILE, $ARGV[0] or die $!; my $lineno = 1; while (<THEFILE>) { if ($_ =~ /^[^i]*i[^i]*$/) { print $lineno; # like grep -n print ": $_"; } $lineno++; }
Hashes (1) • This is the third kind of data structure • Like associative arrays in awk • Use % for variables (like $ for scalars or @ for lists) • EG, my %books = (“a” => 1, “b” =>2); • So keys %books is (“a”, “b”) • Use { } for connecting a key to its value • So, per above, $books{“a”} is 1 • The following example also includes the operator split • Its use goes beyond hashes, of course • my @somelist = split /regex/, $string • For the example, start with $ ypcatpasswd >myetcpasswd
Hashes (2) #! /usr/bin/perl -w # shells.pl open MYPWD, "myetcpasswd" or die $!; my $accounts = 0; my %shell; foreach (<MYPWD>) { my @fields = split (/:/, $_); $shell{$fields[6]}++; $accounts++; if ($fields[0] eq "goelman") { printf "goelman\'s shell is $fields[6]"; } } # script continues on the next slide, with wrapups.
Hashes (3) print "Processed $accounts accounts\n"; print "Here are the number of accounts for each shell:\n"; foreach $key (keys %shell) { print "$key has $shell{$key} users\n";