Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: added apply CS

...

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 Solution grep()

This compliant solution does the same thing but does not modify the the @users array 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.

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

...