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

Thanks for the help everyone.

To answer your questions, yes I am using use strict, use warnings, and use diagnostics. The purpose of the || is to act as an or operator, allowing either a 'y' or a 'Y' or 'n'.

Dave, thanks for pointing out =~, I had forgot about this and it was exactly what I needed.

Thanks again,

-ryan

Monks, I have a script that will generate random passwords based on user specified criteria. I'm trying to add the ability to export the password to a txt file, however I'm coming up with two small, probably easy to fix errors.

My problem is that when I use  eq instead of  == for the comparison, the script will not create the txt file.

When I use  ==, the script will automatically create the txt file and display the warning that should only be displayed if the user enters something other than "y" or "n".

Below is the segment of code which is causing the problem.

print "Do you want to export your password to a text file? (y/n) "; chomp(my $export = <STDIN>); if ($export == q*y||Y*) { open (FH, '>>password.txt') or die $!; print FH "Your password is: ", @password, "\n"; close FH; } elsif ($export == q*n||N*) { print "Your password is: ", @password, "\n"; } else { print "Enter either y or n to continue. \n" ; }

As I said, I'm sure this is probably a simple error to fix, however, I haven't been able to figure it out yet.

Any help would be appreciated.

Replies are listed 'Best First'.
Re: Text output problem
by toolic (Bishop) on Aug 11, 2011 at 00:08 UTC
    Use lc to make your input case-insensitive:
    use warnings; use strict; my @password = ('foo'); print "Do you want to export your password to a text file? (y/n) "; chomp(my $export = <STDIN>); $export = lc $export; if ($export eq 'y') { open (FH, '>>password.txt') or die $!; print FH "Your password is: ", @password, "\n"; close FH; } elsif ($export eq 'n') { print "Your password is: ", @password, "\n"; } else { print "Enter either y or n to continue. \n" ; }
    You do use strict and warnings, don't you? See also Equality Operators.
Re: Text output problem
by davido (Cardinal) on Aug 11, 2011 at 00:08 UTC

    I don't see what's leading up to populating $export, nor what leads in to your if() statement. But something does seem odd. Do you ever expect $export to contain the literal text 'y||Y'? Or do you intend || to be treated like some "or" operator?

    It's possible that you really need:

    if( $export =~ m/y|Y/ ) {....}

    Which can also be written as:

    if( $export =~ m/y/i ) {....}

    Or...

    if( $export =~ m/[yY]/ ) { .... }

    But probably should be more like:

    if( $export =~ m/^y/i ) {....}

    As that will assure that it's not picking up some random 'y' buried anywhere in the input.


    Dave

Re: Text output problem
by jethro (Monsignor) on Aug 11, 2011 at 00:13 UTC

    What do you expect 'y||Y' to do? It is a simple string. If || is meant as an or operator, operators usually won't work in strings. So if you compare $export to this and the user didn't type 'y||Y' (and why should he), your if-clause will never be true

    Use if ($export eq 'y' or $export eq 'Y') { ..., for example.

Re: Text output problem
by cdarke (Prior) on Aug 11, 2011 at 13:41 UTC
    You appear to be trying to use shell pattern matching, as supported by Korn shell and Bash inside [[...]]. Perl does not support that type of pattern matching, it uses Regular Expressions.

    In addition, == in Perl is used for arithmetic comparisons, eq is used for textual comparisons, and =~ is used for matches.

    So:
    if ($export =~ /^q.*y$|^Y/)
    etc.