Reputation: 85
What's going on is I'm parsing an /etc/passwd file to pull out the usernames and first/last name of each person in the file. Can I use a regex to get in to my if statement? Here's an example of a line I'm trying to match:
$parameter = cvwalters:*:14608:140608:Chris V. Walters,N/A,N/A,N/A:/home/cvwalters:/bin/bash
if ($parameter =~ /(.+):(.+):(.+):(.+):(.+),(.+),(.+),(.+):(.+):(.+)/){
my $uid =$1;
my $fname = $5 =~ /^\W+/;
my $lname = $5 =~ /\W+$/;
push (@results, $uid, $fname, $lname);
}
Does perl return a true boolean there to the if statement and allow the rest to execute? If not, how can I make that happen?
Upvotes: 0
Views: 118
Reputation: 902
Try this below code:
my $var = "cvwalters:*:14608:140608:Chris V. Walters,N/A,N/A,N/A:/home/cvwalters:/bin/bash";
my @arr = split /[:,]/, $var;
my $uid = $arr[0];
my ($fname) = $arr[4] =~ /^(\w+)/;
my ($lname) = $arr[4] =~ /(\w+)$/;
push(@results,$uid,$fname,$lname);
Upvotes: 0
Reputation: 85
So this would be the better option then I take it....
$parameter = cvwalters:*:14608:140608:Chris V. Walters,N/A,N/A,N/A:/home/cvwalters:/bin/bash
my ($uid, $junk, $junk2, $junk3, $name, $junk4, $junk5) = split /:/, $parameter;
my $fname = $name =~ /^\w+/;
my $lname = $name =~ /\w+$/;
push (@results, $uid, $fname, $lname);
Upvotes: 0
Reputation: 20852
Yes, in boolean context, the match operator is a boolean operator.
But please take the advice of @squiguy, use split.
There is even a passwd module on CPAN.
http://search.cpan.org/dist/Unix-ConfigFile/PasswdFile.pm
Upvotes: 1