2DBarcode.info

11 The Perl Scripting Language in Objective-C Integrating code 128 barcode in Objective-C 11 The Perl Scripting Language

526 11 The Perl Scripting Language generate, create code-128c none for objective-c projects 2/5 Industrial Accepting output from a process The third statement initia lizes the array @list. Although this statement is not required, it is good practice to include it to make the code easier to read. The next statement opens the $fh lexical handle.

The trailing pipe symbol (. ) in the file-ref (page 51 Code 128A for Objective-C 0) portion of this open statement tells Perl to pass the command line preceding the pipe symbol to the shell for execution and to accept standard output from the command when the program reads from the file handle. In this case the command uses grep to filter the /etc/group file for lines containing the username held in $user. The die statement displays an error message if Perl cannot open the handle.

. $ cat groupfind.pl $user = shift $ENV{"USER"}; say "User $ code 128 barcode for Objective-C user belongs to these groups:"; @list = (); open (my $fh, "grep $user /etc/group . ") or die "Error: $!\n"; w hile ($group = <$fh>) { chomp $group; $group =~ s/(.* ):.*/$1/; push @list, $group; } close $fh; @slist = sort @list; say "@slist"; $ perl groupfind.

pl User sam belongs to these groups: adm admin audio cdrom dialout dip floppy kvm lpadmin ...

. The while structure in gro iPhone Code 128 Code Set C upfind.pl reads lines from standard output of grep and terminates when grep finishes executing. The name of the group appears first on each line in /etc/group, followed by a colon and other information, including the names of the users who belong to the group.

Following is a line from this file:. sam:x:1000:max,zach,helen The line $group =~ s/(.* ):.*/$1/;.

uses a regular expression iPhone USS Code 128 and substitution to remove everything except the name of the group from each line. The regular expression .*: would perform a greedy match of zero or more characters followed by a colon; putting a question mark after the asterisk causes the expression to perform a nongreedy match (page 520).

Putting parentheses around the part of the expression that matches the string the program needs to display enables Perl to use the string that the regular expression matches in the replacement string. The final .* matches the rest of the line.

Perl replaces the $1 in the replacement string with the string the bracketed portion of the regular expression (the part between the parentheses) matched and assigns this value (the name of the group) to $group.. Examples 527 The chomp statement remove code 128 barcode for Objective-C s the trailing NEWLINE (the regular expression did not match this character). The push statement adds the value of $group to the end of the @list array. Without chomp, each group would appear on a line by itself in the output.

After the while structure finishes processing input from grep, sort orders @list and assigns the result to @slist. The final statement displays the sorted list of groups the user belongs to..

opendir and readdir The next example introduce Code 128C for Objective-C s the opendir and readdir functions. The opendir function opens a directory in a manner similar to the way open opens an ordinary file. It takes two arguments: the name of the directory handle and the name of the directory to open.

The readdir function reads the name of a file from an open directory. In the example, opendir opens the working directory (specified by .) using the $dir lexical directory handle.

If opendir fails, Perl executes the statement following the or operator: die sends an error message to standard error and terminates the program. With the directory opened, while loops through the files in the directory, assigning the filename that readdir returns to the lexical variable $entry. An if statement executes print only for those files that are directories ( d).

The print function displays the name of the directory unless the directory is named . or ..

. When readdir has read all files in the working directory, it returns false and control passes to the statement following the while block. The closedir function closes the open directory and print displays a NEWLINE following the list of directories the program displayed.

. $ cat dirs2a.pl #!/usr/bin /perl print "The working directory contains these directories:\n"; opendir my $dir, "." or die "Could not open directory: $!\n"; while (my $entry = readdir $dir) { if (-d $entry) { print $entry, " " unless ($entry eq ".

" . $entry eq ".."); } } clos iPhone barcode 128 edir $dir; print "\n"; $ .

/dirs2a.pl The working directory contains these directories: two one.
Copyright © 2DBarcode.info . All rights reserved.