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

I have a subroutine that generates a random/secure password. This password is then sent to a different subroutine that changes the password. I am running into a problem when a password contains a special character that is meaningful to perl. An example is below:

#!/usr/bin/perl -w use strict; my $user="bobjones"; my $password="abc$123a"; test($user,$password); sub test { my $user = shift; my $password = shift; print "$user - $password\n"; }

Output when run:
Use of uninitialized value at ./test.pl line 5.
bobjones - abca


Basically, I do not want the $ to be interpreted here, and it would be nice to not have to replace $ with \$.

Replies are listed 'Best First'.
Re: How to deal with subroutine parameters that have $, @, or %?
by james2vegas (Chaplain) on Sep 03, 2009 at 21:20 UTC
    Your password generator is unlikely to be doing this, use ' instead of " for your test, or get the test data from a __DATA__ section instead. If its a generated string, there won't be interpolation, f.e.

    #!/usr/bin/perl -w use strict; my $user="bobjones"; my $password='abc$123a'; test($user,$password); sub test { my $user = shift; my $password = shift; print "$user - $password\n"; }
    or
    #!/usr/bin/perl -w use strict; use String::Random; my $user="bobjones"; my $password= generateme(); test($user,$password); sub test { my $user = shift; my $password = shift; print "$user - $password\n"; } sub generateme { my $random = String::Random->new(); return $random->randpattern('ss!ccnC!cncc'); }

    Using String::Random in place of your random password generator. It will produce strings containing @s, %s and $s, without the need of interpolation.