Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses
 
PerlMonks  

Re^2: unexpected STDIN

by GhodMode (Pilgrim)
on Apr 30, 2011 at 12:06 UTC ( [id://902172]=note: print w/replies, xml ) Need Help??


in reply to Re: unexpected STDIN
in thread unexpected STDIN

Thanks CountZero.

My code was actually just a smaller part of the whole that I was using to isolate the problem I was experiencing. I wasn't having a problem with processing the response from the user, though.

I like your solution, but it's a little hard to read and it doesn't work for my "neVer" answer.

At the risk of being less perlish, I'm using a less concise version that will be a little easier (for me) to read and understand when I look at the code again in the future:

if ($removeall == 1) { unlink $origin; } elsif ($removeall == 0) { print "Remove '$origin'? ([Y]es, [N]o, [A]ll, ne[V]er; default: N) + "; chomp(my $answer = lc(<STDIN>)); print "answer: |$answer|\n" if (DEBUG); if (($answer eq 'y') or ($answer eq 'yes')) { unlink $origin; } if (($answer eq 'a') or ($answer eq 'all')) { unlink $origin; $removeall = 1; } if ($answer eq ('v' or 'never')) { $removeall = -1; } }

--
Ghodmode
www.ghodmode.com/blog

Replies are listed 'Best First'.
Re^3: unexpected STDIN
by cdarke (Prior) on Apr 30, 2011 at 17:55 UTC
    As with most code, this can be improved further, yet still keep your own style. You are testing to see if $answer is 'y', and then testing to see if it is 'a'. It can't be both! No point in testing for 'a' or 'v' if it is 'y'. And what if the user mis-types, for example 'Q' (users never do what you want them to)?
    Also, you are not reporting if the unlink fails. autodie is an easy way of doing that without hacking your code too much:
    use strict; use warnings; use autodie; my $origin = 'somefile'; # inserted for testing my $removeall = 0; # inserted for testing if ($removeall == 1) { unlink $origin; } elsif ($removeall == 0) { print "Remove '$origin'? ([Y]es, [N]o, [A]ll, ne[V]er; default: N) + "; chomp(my $answer = lc(<STDIN>)); #print "answer: |$answer|\n" if (DEBUG); if (($answer eq 'y') or ($answer eq 'yes')) { unlink $origin; } elsif (($answer eq 'a') or ($answer eq 'all')) { unlink $origin; $removeall = 1; } elsif ($answer eq ('v' or 'never')) { $removeall = -1; } elsif ($answer ne '' and $answer ne 'n') { print "Invalid response: '$answer' (taken as N)\n"; } }
Re^3: unexpected STDIN
by CountZero (Bishop) on May 01, 2011 at 08:06 UTC
    and it doesn't work for my "neVer" answer
    Yes, it does: $answer =~ m/^v|(?:never)$/i

    If you are using a relatively modern Perl (5:10 or later) you can even use the Perl switch construct (which is called given ... when).

    use Modern::Perl; my $answer = <STDIN>; given ($answer) { when (/^y(?:es)?$/i) { say 'yes'; } when (/^no?$/i) { say 'no'; } when (/^a(?:ll)?$/i) { say 'all'; } when (/^v|(?:never)$/i) { say 'never'; } default { say 'default'; } }

    CountZero

    A program should be light and agile, its subroutines connected like a string of pearls. The spirit and intent of the program should be retained throughout. There should be neither too little or too much, neither needless loops nor useless variables, neither lack of structure nor overwhelming rigidity." - The Tao of Programming, 4.1 - Geoffrey James

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://902172]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-25 04:55 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found