Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

Re: crypt sometimes returning undef

by thanos1983 (Vicar)
on Feb 14, 2018 at 11:59 UTC ( #1209131=note: print w/replies, xml ) Need Help??


in reply to crypt sometimes returning undef

Hello Beaker,

I was reading the documentation of crypt and I noticed from the sample code:

if (crypt($word, $pwd) ne $pwd) { die "Sorry...\n"; } else { print "ok\n"; }

They are using first the word and then the password. It is not related to your problem but I think it would be more clear in future reference:

my $cryptedpass = crypt($passwrd1, substr($never_blank_value, 0, 2));

The only reason that I can imagine that you might have $cryptedpass as blank is that you have $never_blank_value. You are using substr and you are cutting off the first 2 characters of the string. In case the string it has less than 2 characters then you will end up with undef value. Sample of proposed code:

#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $never_blank_value = 'T'; my @passset = ('a'..'k', 'm'..'n', 'p'..'z', '2'..'9'); my $passwrd1 = ""; for (my $i=0; $i<8; $i++) { $passwrd1 .= $passset[int(rand($#passset + 1))]; } die 'Too small \$never_blank_value' if length $never_blank_value < 2; my $cryptedpass = crypt($passwrd1, substr($never_blank_value, 0, 2)); say $cryptedpass; __END__ $ perl test.pl Too small \$never_blank_value at test.pl line 13.

Sample of error:

#!/usr/bin/perl use strict; use warnings; use feature 'say'; my $never_blank_value = 'T'; my @passset = ('a'..'k', 'm'..'n', 'p'..'z', '2'..'9'); my $passwrd1 = ""; for (my $i=0; $i<8; $i++) { $passwrd1 .= $passset[int(rand($#passset + 1))]; } # die 'Too small \$never_blank_value' if length $never_blank_value < 2 +; my $cryptedpass = crypt($passwrd1, substr($never_blank_value, 0, 2)); say $cryptedpass; __END__ perl test.pl Use of uninitialized value $cryptedpass in say at test.pl line 15.

Update: Based on the link 0007695: Crypt bug that fellow Monk poj point it out you might be passing non acceptable characters. If this is the case add this to your code:

die 'Not accepted characters at \$never_blank_value' unless ($never_blank_value =~ /^[a-zA-Z0-9.\/]+$/);

Hope this helps, BR.

Seeking for Perl wisdom...on the process of learning...not there...yet!

Replies are listed 'Best First'.
Re^2: crypt sometimes returning undef
by Beaker (Beadle) on Feb 15, 2018 at 11:05 UTC

    Thank you, I believe this is the issue.

    My debug for an instance of this undef value has $never_blank_value as T_ (after substr)

    $never_blank_value contains azAZ09\-_ so it's likely this would break if it contains the hyphen or underscore, I will run a regex over $never_blank_value to filter out these disallowed characters.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1209131]
help
Chatterbox?
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others surveying the Monastery: (2)
As of 2018-08-20 05:09 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Asked to put a square peg in a round hole, I would:









    Results (190 votes). Check out past polls.

    Notices?