in reply to Losing or overwritting values

Testing with my new indentations, lemme know what you think:
#!/usr/bin/perl use CGI::Carp 'fatalsToBrowser'; use strict; use warnings; use CGI; my $query = CGI->new; my $usedpw = "logfile.txt"; my @chars; print $query->header; # opening for reading open(USEDPW, "< $usedpw") or die "poo poo on reading $!"; flock USEDPW, 1; # Storing the file handle, not a scalar, into an array for full readin +g my @used_pw = <USEDPW>; close(USEDPW); # remove trailing whitespace chomp my $used_pw; while (<USEDPW>) { my $pw = @chars[map{rand @chars} (1..17)]; } unless ($_ ne my $usedpw) { print "Your unique ID is: $pw\n"; } my $pw; open(USEDPW, "> $usedpw") or die "crap on me for writing $!"; flock USEDPW, 2; print USEDPW "$pw\n"; close(USEDPW);


"Age is nothing more than an inaccurate number bestowed upon us at birth as just another means for others to judge and classify us"

sulfericacid

Replies are listed 'Best First'.
(jeffa) 2Re: Losing or overwritting values
by jeffa (Bishop) on Jan 04, 2003 at 19:37 UTC
    Almost ... try this instead:
    while (<USEDPW>) { my $pw = @chars[map{rand @chars} (1..17)]; } unless ($_ ne $usedpw) { print "Your unique ID is: $pw\n"; }
    or this:
    while (<USEDPW>) { my $pw = @chars[map{rand @chars} (1..17)]; } unless ($_ ne $usedpw) { print "Your unique ID is: $pw\n"; }
    Whether you like to hug the open brace (example 1) or put it's on it's own line (example 2) is a matter of personal preference - both are correct. However, you really need to line the closing brace up with what started it. Also, i recommend using tabs instead of spaces - you can always use a tool like expand to convert each tab to any number of spaces. For comments to the left of code, well, i try to avoid them, but when i have to have to have them i use spaces to line them up. Keep up the good work. :)

    Oh yeah ... you can simplify the unless block like so:

    print "Your unique ID is: $pw\n" if $_ eq $usedpw;
    ... and double negatives are bad habits, notice that i replaced unless with if and ne with eq.

    jeffa

    L-LL-L--L-LL-L--L-LL-L--
    -R--R-RR-R--R-RR-R--R-RR
    B--B--B--B--B--B--B--B--
    H---H---H---H---H---H---
    (the triplet paradiddle with high-hat)
    
      Whether you like to hug the open brace (example 1) or put it's on it's own line (example 2) is a matter of personal preference - both are correct. However, you really need to line the closing brace up with what started it.

      Lining up the braces with keywords is also a matter of personal style. I do it and I wish every one did. Unfortunately, some people don't. Really, it's all a matter of style and there is no correct or incorrect way to do it. I think the real important thing is consistency.

      Also, i recommend using tabs instead of spaces

      I strongly disagree with this. Use spaces. Tabs just end up being an additional hassle for the programmer. Tabstops have to be set correctly in order for the code to look good. That's not a huge problem but it is an annoyance. The real problem occurs when a tab-using programmer comes along and modifies source code written by a space-using coder (or vice-versa.)

      So, once again the answer is consistency. The reason I advocate spaces is that they are inherently consistent and do not rely on an editor or terminal variable as tabs do. A space is a space is a space whereas a tab is eight spaces or maybe four or perhaps two or possibly three and sometimes six... etc.

      I know some people advocate using tabs for indentation and spaces for alignment. In my estimation, that's only half a solution. Worse, it creates its own problems later when the code needs to be maintained.

      I'd break my tab key off if I didn't use it for completion in my shell.

      -sauoq
      "My two cents aren't worth a dime.";
      

        Like you said ... this is all opinion, the important thing is consistency. (And if you don't use vi ... )

        However, i will continue to fight for tabs over spaces when indenting on the left side. The right side was made for spaces, but using spaces on the left side is just too much typing for me. "Let's see, do i hit the tab 3 times or the space bar 12 times?" I am sorry, but using spaces on the left side is simply having to do more work than i really need to do. I started out using spaces, switched to tabs, and i have been using tabs ever since. Besides:
        # need to convert 4 spaces to a tab? perl -pe 's/ /\t/g' foo.pl # need to convert a tab to 4 spaces? perl -pe 's/\t/ /g' foo.pl
        These could be made more robust, but i think the point has been made. As long as we can easily convert from one to the other, we shouldn't worry about whether or not we use tabs or spaces, as long as we are consistent. But me? I use tabs. I am just too lazy not to.

        "The reason I advocate spaces is that they are inherently consistent and do not rely on an editor or terminal variable as tabs do."

        And this is exactly the same reason why i advocate tabs. If i am on a smaller screen, i can set my tabstop in vi to 3. If i am on a larger screen, i can set the tabstop to 6. It is more flexible. Thanks to expand, i can deliver my code with tabs converted to any number of spaces. Let the computer do the work.

        P.S. (FWIW), this is a heated argument at the Computer Science department at my University. The camps have picked sides and the line has been drawn. Some professors teach their students to use tabs, others spaces. Neither side has convinced the other that they are right, and i doubt they ever will. Now, let's get back to Perl! ;)

        UPDATE:

        jeffa

        L-LL-L--L-LL-L--L-LL-L--
        -R--R-RR-R--R-RR-R--R-RR
        B--B--B--B--B--B--B--B--
        H---H---H---H---H---H---
        (the triplet paradiddle with high-hat)