Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Some Weird Probelm

by emcb (Beadle)
on Dec 11, 2001 at 10:51 UTC ( #130880=perlquestion: print w/ replies, xml ) Need Help??
emcb has asked for the wisdom of the Perl Monks concerning the following question:

Hi, I have just written a script that generates a random password. I am using perl 5.0.6, and heres the code:
################################################################# # Random Password Generator ################################################################# use strict; # The Alpha/Number Dict Files $DICT_A = "/usr/perl/dict_a.txt"; $DICT_N = "/usr/perl/dict_n.txt"; # Open The Dict Files open( ALPHAN, "<$DICT_A" ) || die "Cannot Open Aplhanumeric Dict File: + $!"; open( NUMBER, "<$DICT_N" ) || die "Cannot Open Numeric Dict File: $!"; # Create Associative Arrays my @alpha = <ALPHAN>; my @number = <NUMBER>; # Chomp! chomp @alpha; chomp @number; # Close The Dict Files close ALPHAN; close NUMBER; # Set The Random Password my $ranpwd = $alpha[rand @alpha],$number[rand @number],$number[rand @n +umber],$alpha[rand @alpha],$alpha[rand @alpha],$alpha[rand @alpha];
And it tells me this:
global symbol "$DICT_A" requires explicit package name @ 8 global symbol "$DICT_N" requires explicit package name @ 9 global symbol "$DICT_A" requires explicit package name @ 11 global symbol "$DICT_N" requires explicit package name @ 12 Execution of /ranpwd.pl aborted. due to compilation errors
Anyone Know Why? Cheers, Elfyn

Comment on Some Weird Probelm
Select or Download Code
Re: Some Weird Problem
by dws (Chancellor) on Dec 11, 2001 at 11:05 UTC
    You're using strict, which is good. That means that you need to add declare your variables. Try
    my $DICT_A = "/usr/perl/dict_a.txt"; my $DICT_N = "/usr/perl/dict_n.txt";
    Also, your code to assemble $ranpwd isn't doing what you expect. The string concatenation operator in Perl is '.', not ','.

Re: Some Weird Probelm
by dvergin (Monsignor) on Dec 11, 2001 at 11:37 UTC
    dws nailed it (as usual).

    As a side note, your comment:    # Create Associative Arrays
    does not correspond to what you have in the code. It seems clear from what you are doing that @alpha and @number are both just arrays and not associative arrays (i.e. hashes).

    An array is a simple series of values (which is what you have) which retain their order and are accessed by an index value.

    An associative array or hash is a group of pairs of values which are not guaranteed to be in any particular order; the first value in each pair is used as a key to the other which is used as the value associated with that key.

    The comment line is not essential to the function of your code but this seemed worth mentioning so that you get used to the correct use of the terminology.

Re: Some Weird Probelm
by strat (Canon) on Dec 11, 2001 at 17:01 UTC
    Another way to solve this problem is using constants.
    e.g.
    use constant DICT_A => "/usr/perl/dict_a.txt"; use constant DICT_N => "/usr/perl/dict_n.txt";
    and replace every $DICT_A with DICT_A
    I consider it a good thing to declare variables that are not changed in a script as constants... :-)

    Best regards,
    perl -e "print a|r,p|d=>b|p=>chr 3**2 .7=>t and t"

Re: Some Weird Probelm (boo)
by boo_radley (Parson) on Dec 11, 2001 at 19:39 UTC

      How about skipping the openning of the external file and just doing something simple like this:

      #!/usr/bin/perl -w use strict; # Make declarations of variables. my @alpha = ('a'..'z', 'A'..'Z'); my @numeric = (0 .. 9); my ($a_pwd0, $a_pwd1, $a_pwd2, $a_pwd3, $n_pwd0, $n_pwd1); # Choose random letters and numbers for each string. $a_pwd0 = $alpha[int rand @alpha]; $a_pwd1 = $alpha[int rand @alpha]; $a_pwd2 = $alpha[int rand @alpha]; $a_pwd3 = $alpha[int rand @alpha]; $n_pwd0 = $numeric[int rand @numeric]; $n_pwd1 = $numeric[int rand @numeric]; # Print password: print "\n\n", "Your password is: ", $a_pwd0,$n_pwd0,$n_pwd1,$a_pwd1, $a_pwd2,$a_pwd3, "\n\n"; exit;

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2014-12-27 06:01 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    Is guessing a good strategy for surviving in the IT business?





    Results (176 votes), past polls