Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl Monk, Perl Meditation
 
PerlMonks  

Losing or overwritting values

by sulfericacid (Deacon)
on Jan 04, 2003 at 18:52 UTC ( [id://224288]=perlquestion: print w/replies, xml ) Need Help??

sulfericacid has asked for the wisdom of the Perl Monks concerning the following question:

Running into the same problem as my last node. I rewrote the entire script hoping for better functionality but it still prints to screen: Your unique ID is.. without the $pw. So somewhere it is being overwritten with an undefined value or it's not ever becoming defined. Any suggestions?

#!/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 $pw) { 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);

Thanks,

"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'.
•Re: Losing or overwritting values
by merlyn (Sage) on Jan 04, 2003 at 19:26 UTC
    Not commenting on the rest of the problem, but since I saw this code in some of the replies as well..
    open(USEDPW, "> $usedpw") or die "crap on me for writing $!"; flock USEDPW, 2;
    This code is always wrong. You have already zeroed the file before you've gotten permission to zero the file.

    You need to open the file for reading and writing without altering it, get the flock, then proceed. Many correct examples exist. This is not one of them.

    -- Randal L. Schwartz, Perl hacker
    Be sure to read my standard disclaimer if this is a reply.

Re: Losing or overwritting values
by pfaut (Priest) on Jan 04, 2003 at 19:21 UTC

    I think this is what you are trying to achieve. I've commented each line so that you can understand exactly what's going on there.

    #!/usr/bin/perl use strict; use warnings; use CGI; use CGI::Carp 'fatalsToBrowser'; my $query = CGI->new; print $query->header; my $usedpw = "logfile.txt" ; my @chars = ( "A" .. "Z", "a" .. "z", 0 .. 9, qw(! @ $ % ^ & *) ); # opening for reading open(USEDPW, "<", $usedpw) or die $!; # shared lock flock USEDPW, 1; # read entire file into array, one line per element my @usedpw = <USEDPW>; # close the file close(USEDPW); # remove new line chars from passwords chomp @usedpw; my $pw; my $used; # loop generating passwords until we create one that's unique do { # generate a password $pw = join @chars[map{rand @chars} (1..17)]; # assume it's not used $used = 0; # compare generated password to each existing password for (@usedpw) { # if the generated password has been used before, # set the used flag and abort the for loop $used=1,last if $_ eq $pw; } } while ($used); # display the password print "Your unique password is: $pw\n"; # open for append (>>) and save the new password open(USEDPW, ">>", $usedpw) or die $!; # exclusive lock flock USEDPW, 2; # write new password to end of file print USEDPW "$pw\n"; # close the file close(USEDPW);
    --- print map { my ($m)=1<<hex($_)&11?' ':''; $m.=substr('AHJPacehklnorstu',hex($_),1) } split //,'2fde0abe76c36c914586c';
Re: Losing or overwritting values
by RhetTbull (Curate) on Jan 05, 2003 at 01:22 UTC
    Here's the problem (or one of them):
    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";
    You have 2 separate variables named $pw. You used my which is a Good Thing but forgot that my lexically scopes your variables. That means that the my $pw inside the while(<USEDPW>) loop is a completely different variable than the one declared outside the loop. pfaut fixed it in his post but didn't tell you why he did it. You need to declare the $pw variable outside the loop so it stays in scope. For example:
    #notice we declare it OUTSIDE the loop my $pw; while (<USEDPW>) { $pw = @chars[map{rand @chars} (1..17)]; } unless ($_ ne my $usedpw) { print "Your unique ID is: $pw\n"; } open(USEDPW, "> $usedpw") or die "crap on me for writing $!"; flock USEDPW, 2; print USEDPW "$pw\n";
    You should have gotten a "Use of uninitialized value in print" warning when you ran your code.
Re: Losing or overwritting values
by sulfericacid (Deacon) on Jan 04, 2003 at 19:05 UTC
    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
      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.";
        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://224288]
Approved by virtualsue
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others chilling in the Monastery: (4)
As of 2024-04-26 06:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found