John Doe John Doe - 7 months ago 36
Perl Question

Perl - Multiple condition if statement without duplicating code?

This is a Perl program, run using a terminal (Windows Command Line). I am trying to create an "if this and this is true, or this and this is true" if statement using the same block of code for both conditions without having to repeat the code.

if ($name eq "tom" and $password eq "123!") elsif ($name eq "frank" and $password eq "321!") {

print "You have gained access.";

} else {

print "Access denied!";




if ( $name eq 'tom' && $password eq '123!'
    || $name eq 'frank' && $password eq '321!'
) {

(use the high-precedence && and || in expressions, reserving and and or for flow control, to avoid common precedence errors)


my %password = (
    'tom' => '123!',
    'frank' => '321!',

if ( exists $password{$name} && $password eq $password{$name} ) {