Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight

Interactive Perl script

by Irishboy24 (Sexton)
on Aug 27, 2009 at 15:26 UTC ( #791679=perlquestion: print w/replies, xml ) Need Help??
Irishboy24 has asked for the wisdom of the Perl Monks concerning the following question:

Hey Guys

I have a simple script, which ipon execution asks a question, if the answer is Y it will print out something. here is the code.

#!/usr/bin/bash set -x echo -n "can you write device drivers?" read answer if [ $answer = Y ] then echo "wow, you must be very skilled" else echo "neither can i, i am just shell script" fi

My problem is that i have been trying to convert it to perl and unable to do so. can someone please help.

thanks Arsenalboy

Thank you for your replies perl monks, i am sorry i was unable to provide the code as i really dint understand how to approach it. i am very new to perl and this was really helpful in my learning of the subject. thanks a lot guys. Perl monks rock!!

Replies are listed 'Best First'.
Re: Interactive Perl script
by SuicideJunkie (Vicar) on Aug 27, 2009 at 15:42 UTC

    First, show your perl code or we can't point out where to fix it.

    Some common things to check if you've already gone through the intro above:

    • Did you chomp() the newline off the end of the input?
    • Are you using string comparison (eq) or numeric (==) to compare your strings, or a regex?
    • Did you consider case sensitivity?

Re: Interactive Perl script
by bv (Friar) on Aug 27, 2009 at 15:46 UTC

    Trivial example:

    print "Can you read this? "; my $answer = <STDIN>; if ($answer =~ /^y(?:es)?$/i) { print "Excellent\n"; } else { print "Then how did you answer?\n"; }

    This has the benefit of accepting any of y, Y, yes, YES or Yes (to name a few).

    $,=' ';$\=',';$_=[qw,Just another Perl hacker,];print@$_;

      Thank you very much. your response was really helpful. appreciate your help

Re: Interactive Perl script
by Utilitarian (Vicar) on Aug 27, 2009 at 15:54 UTC
    Okay as a fellow Irish Arsenal fan I'll help, but really you should show what you have tried as this site is for those interested in learning Perl rather than those interested in getting code for free</rant>
    #!/usr/bin/perl use strict; use warnings; sub confirm{ my $question=shift; my $reply = ""; print "$question (y/n)?"; while ($reply !~ m/^[yn]/i){ # allow for pedants who reply "yes" or + "now" chomp($reply=<STDIN>); } return $reply=~m/^y/i ? 1 : undef; } print confirm("can you write device drivers")?"wow, you must be very s +killed\n":"neither can i, i am just shell script\n";
    BTW: the Dublin Perl mongers group is holding a re-inauguration at OSBarCamp, feel free to come along.

      Hey, Thank you very much. your response was really helpful. appreciate your help. BTW i am Indian, i just like the way the Irish drink and am a very very big fan of Arsenal. cheers for your reply!! thanks! Go Gunners

        Ahh, you probably won't be in Dublin for OSBarCamp so.
        While my script answers your immediate question, the point of it was to understand all the elements within it.
        • use strict and warnings, seems a pain at first but it will do 70% of your debugging
        • When you have something like a confirmation dialog make a routine out of it, because you'll need to do it again. These routines can ultimately be built up into your own modules which take the drudgery out of development
        • The ternary operator is quite useful(<condition>?true:false;)
        • But as has been said above, in order to learn you have to make your own mistakes and the satisfaction of solving a puzzle is a great memory enforcement
        BTW: Arshavin, brilliant or what ;)
Re: Interactive Perl script
by moritz (Cardinal) on Aug 27, 2009 at 15:42 UTC
    Why are you unable to do so? If it's because you don't know perl, I recommend reading perlintro, or maybe even a book - "Learning Perl" is quite good.

    If there's something else that stops you, what is it?

    Perl 6 - links to (nearly) everything that is Perl 6.
Re: Interactive Perl script
by biohisham (Priest) on Aug 27, 2009 at 15:45 UTC
    Try this one out, and there is many other ways to do it

    print "Can you write device drivers?"; my $answer =<>; chomp; if($answer=~/\byes\b/i){ print "Wow, you must be very skilled\n"; }else{ print "Neither can I...."; }

    Excellence is an Endeavor of Persistence. Chance Favors a Prepared Mind.
Re: Interactive Perl script
by bichonfrise74 (Vicar) on Aug 28, 2009 at 00:48 UTC
    Try this. I would assume a lot of introduction to Perl would have this type of example.
    #!/usr/bin/perl use strict; print "can you write device drivers? "; chomp(my $input = <> ); ($input eq 'Y') ? print 'wow, you must be very skilled' : print 'neither can i, i am just shell script';

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://791679]
Approved by AnomalousMonk
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others pondering the Monastery: (3)
As of 2017-02-26 02:38 GMT
Find Nodes?
    Voting Booth?
    Before electricity was invented, what was the Electric Eel called?

    Results (371 votes). Check out past polls.