[Courses] [Perl] Part 6: Data Extraction with m//
Karine Delvare
kproot at nerim.net
Mon May 9 00:44:54 EST 2005
On Fri, 06 May 2005 16:11:00 +0100
Dan <dan at cellectivity.com> wrote:
> LinuxChix Perl Course Part 6: Data Extraction with m//
>
> 4) Exercise
>
> Modify your program from last week's exercise, the one that reads
> /etc/passwd, so that it outputs only your home directory and your
> shell (not the rest of the line).
#!/usr/bin/perl -w
use strict;
open MY_INPUT, "< /etc/passwd" or die "Couldn't open input file: $!";
print "Please enter a username:\n";
chomp(my $user = <STDIN>);
my $found = 0;
my $home;
my $shell;
while (defined(my $line = <MY_INPUT>))
{
if (($home, $shell) = ($line =~ /^$user:.*:(.*):(.*)$/))
{
print "Home is: $home\nShell is: $shell\n";
$found = 1;
}
}
if (!$found)
{
print "This user does not exist.\n";
}
close MY_INPUT;
Provided I'm sure shell is the last info and home the one before that, I decided to use a greedy match for all the info between the user and the home directory.
Does greedy / non-greedy affect performances?
--
Karine Delvare
http://edhel.nerim.net/
More information about the Courses
mailing list