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

Hello All, I have a script which asks for a password and stored the content in $password=<>, but the password is r;OFTaf#a2Mt9d\1 , how can I store the password and not have the backspace interpreted as an escape . I've tried "\"r;OFTaf#a2Mt9d\1\"" and "\'r;OFTaf#a2Mt9d\1 \'" but none works. I even tried pass=> \Q$password\E Thank you

Replies are listed 'Best First'.
Re: Escape variables
by Marshall (Canon) on Jan 06, 2016 at 06:28 UTC
    Please post a simple snippet of your code, how you ask for the password, how you capture it and how you store it. Your O/S is also perhaps relevant.
      Below is the portion of my code relevant to the issue I am having, its a script to login to another system via ssh
      my $username; my $password; print "Please enter username to connect to the system:" ; chomp($username=<>); print "Please enter password\n"; chomp($password=<>);
        I think you you are getting what you want, but your printout statement is misleading you?

        #!usr/bin/perl use strict; my $in = 'r;OFTaf#a2Mt9d\1'; #single quote is literal quote #this is what your input should get print $in, "\n"; #print $in "as is", not within quotes =prints r;OFTaf#a2Mt9d\1
        a script to login to another system via ssh

        Don't mess with passwords, use public key authentication. See PuTTY FAQ "Remember Password" and Using public keys for SSH authentication. (Note that using public key authentication is not PuTTY specific, it's a basic feature of SSH.)

        Alexander

        --
        Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)
        I have some quibbles about how you accept input. In a standard command line I/F leading or trailing spaces should not matter. But this looks to me like it should "work". How are you printing $password?
        I don't know what the rest of your code does.
        Code like this should get the input token and strip out the leading and trailing white-space.
        #/usr/bin/perl use warnings; use strict; my $username; print "Please enter username to connect to the system:" ; $username=<>; $username =~ s/^\s+//; #trim leading whitespace $username =~ s/\s+$//; #trim trailing whitespace #this includes \r, \n, etc. print $username, "\n";
Re: Escape variables
by AnomalousMonk (Archbishop) on Jan 06, 2016 at 19:21 UTC

    A simple experiment under Windows 7 shows that, as others have noted, entering a backslash via STDIN should not be a problem:

    c:\@Work\Perl>perl -wMstrict -le "chomp(my $pw = <>); print qq{>>$pw<<}; " r;OFTaf#a2Mt9d\1 >>r;OFTaf#a2Mt9d\1<<
    Once you have the string in a variable, there should be no further problem: as shown in the example above, Perl does not re-interpolate interpolated strings.

    Can you provide a short, self-contained executable example that shows the problem you're facing?


    Give a man a fish:  <%-{-{-{-<