Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.

...

However, because the grep() block removes /bin/sh from any input line that contains it, it modifies the @users list so that no user has /bin/sh!

Compliant

...

Solution (grep())

This compliant solution does the same thing but does not modify the @users array.

Code Block
bgColor#ccccff
langperl
open( PASSWD, "<", "/etc/passwd") or croak "error opening /etc/passwd: stopped"
my @users = <PASSWD>;
my @shell_users = grep +(m|/bin/sh|), @users;
foreach my $user (@shell_users) {
  $user =~ s|/bin/sh||;
  print "Shell User: $user";
}

Compliant Solution (apply())

This compliant solution does the same thing but uses List::MoreUtils::apply() which guarantees not to modify its input list.

...