There are a few problems with the code you have posted (as noted by others and which I won't repeat here). However, once all of those little bugs are fixed you also might want to consider reworking the logic of the code. Here is a cleaned up version of your code, largely following your posted code in level and style (ie, not trying to add anything beyond what you are already doing), but rearranging the logic somewhat:

#!/usr/bin/perl -w use strict; # first initialize necessary variables: my $err_msg = "Authorization Failed: please try again\n"; my $cusr = 'bob'; my $cpword = 'pass'; # next obtain the user's data: my($usr, $pword); print "What is your username? "; $usr = <STDIN>; chomp($usr); print "Please enter your password:\n"; $pword=<STDIN>; chomp($pword); # now we can authenticate or die unless ($usr eq $cusr and $pword eq $cpword) { die $err_msg; } print "Authentication Complete!\n"; # authorized file processing may ensue: my @lines; my $file = 'memberlist.txt'; open (FILE, $file) || die "Can't open $file: $!"; while(<FILE>){ push @lines,$_; } close FILE; # now do whatever with @lines. print @lines; __END__

I am not saying the above is optimal --- it can be shortened and simplified further. But, we've now broken out the logic into 4 self contained tasks: 1)setting initial values, 2)obtaining user authentication data, 3)perform the authentication or die with a message, 4)read and otherwise process the file. In your original, those last three tasks were all intermixed in the code rather than proceeding logically from one stage to the next. This reordering not only reduces the complexity of the code (no more nested conditionals), it makes it easier to code and test section by section in the first place --- which can greatly help in avoiding (or at least catching early) the scattering of syntactical errors and ommissions that crept into your code. It is also good practice for when you want to break out logical tasks into subroutines.


In reply to Re: Variable/if question... by danger
in thread Variable/if question... by tretin

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.