1 / 14

CSC 4630

CSC 4630. Perl 5 D. Goelman, April 2013. Content of This Presentation. Command-line arguments Processing files Regular expressions Hashes

marnin
Download Presentation

CSC 4630

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. CSC 4630 Perl 5 D. Goelman, April 2013

  2. 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.

  3. 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.

  4. 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

  5. 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"; }

  6. 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)

  7. 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++; }

  8. 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++; }

  9. Regular expressions (1) • Format is pretty much $stringvar =~ /regex/ • The regular expressions known to Perl include the ones we’ve seen, and more

  10. 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++; }

  11. 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++; }

  12. 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

  13. 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.

  14. 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";

More Related