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!

In reply to Re: crypt sometimes returning undef by thanos1983
in thread crypt sometimes returning undef by Beaker

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.