Beefy Boxes and Bandwidth Generously Provided by pair Networks
There's more than one way to do things
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

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":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others exploiting the Monastery: (3)
As of 2024-04-19 22:54 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found