Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re: crypt sometimes returning undef

by thanos1983 (Parson)
on Feb 14, 2018 at 11:59 UTC ( [id://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 a–zA–Z0–9\-_ 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
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1209131]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2024-04-19 11:46 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found