Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"

My program it doesn't work could you tell me my mistakes?

by Anonymous Monk
on Nov 08, 2001 at 04:18 UTC ( #123961=perlquestion: print w/replies, xml ) Need Help??
Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

print "What is your name?\n"; $input=<STDIN>; $name=inputcutter($input); print "What is your age?n\"; $input=<STDIN>; $age=inputcutter($input); print "Your are $name and your age is @age years old.\n"; sub inputcutter{ $s=chomp $input; return ($s); }

Edit (davorg): added <code> tags.

2005-10-13 jdporter moved from Tutorials (!)

Replies are listed 'Best First'.
Re: My program it doesn't work could you tell me my mistakes?
by monkeygirl (Pilgrim) on Nov 08, 2001 at 06:46 UTC

    Here is your program wrapped in code tags, and formatted so it's a little easier to read. I've made comments explaining what's going wrong/on within the code.

    print "What is your name?\n"; $input=<STDIN>; $name=inputcutter($input); # Be careful with that slash. You just escaped your # double-quote. print "What is your age?n\"; $input=<STDIN>; $age=inputcutter($input); # You don't have an array called @age in your code. # Try $age instead. print "Your are $name and your age is @age years old.\n"; sub inputcutter { # Here, you're neglecting to fetch the data you just # passed to here (in @_, FYI). If you do # $input = shift;, you'll grab the first element of # @_, which is what you need. # On this line, $s holds whether or not the chomp was # successful, not the chomped version of $input. You # can just chomp $input; and forget about $s. $s=chomp $input; return ($s); }

    It is highly recommended you use strict; and run with Warnings and Taint. I'm not going to rehash the reasons why, when there is already a node on PM such as Use strict warnings and diagnostics or die just waiting to be read.

    Also, remember to include your shebang line at the top of your program (usually #!perl or #!/usr/bin/perl), or else your program won't run. And if you're on a Windows box, you have to execute the program like this: perl or  perl -T

    If the program is still giving you problems, please reply back to this thread for additional help. But I implore you to be more descriptive of the problem as it will increase the chances of us being able to help you out.

    Update: As blakem points out, Anonymous Monk probably did have the <STDIN> after all. Node edited to reflect changes.

    If Bill Gates can name a company after his "bedroom" problems, I can have a stupid sig that points it out.
      Since I almost tripped myself up on this yesterday...

      When someone posts code w/o code tags, things like '<STDIN>' tend to get parsed out as invalid html tags. That's why you'll see things like

      $input = ;
      </code> It was actually entered as:
      $input = <STDIN>;
      but got munged along the way.


Re: My program it doesn't work could you tell me my mistakes?
by virtualsue (Vicar) on Nov 10, 2001 at 19:44 UTC
    Here's a simple, "idiomatic" alternative to your code which runs with warnings and strict syntax checking enabled.. monkeygirl has already pointed out the problems with your original version.
    #!/usr/bin/perl -w use strict; print "What is your name?\n"; chomp (my $name = <stdin>); print "What is your age?\n"; chomp (my $age = <stdin>); print "You are $name and your age is $age years old.\n";

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://123961]
[Discipulus]: Corion are you would able to realize such thing? O_O
[Corion]: In the same vein I have a script that automates Firefox to enter some data into another system. It's not faster than the people using the script if they were to do it manually, but they prefer not having to check the data and not having typos when ...
[Corion]: ... entering the data
[Corion]: Discipulus: I don't know whether I could really do that, but the init process itself mostly launches other processes, and the whole startup is just following a path of dependencies and making sure they are all running. Which basically is what ...
[Discipulus]: when at work my time is (temporarly) owned by the firm, so i do not care (coworkers whatch movies.. I code Perl)
[Corion]: ... make already does, except for files instead of programs. But maybe with some /proc hackery, that could be eliminated and one could use plain make :-D
[choroba]: systemd just makes is asynchronous
[choroba]: so, make -j
[Corion]: Discipulus: Yeah - but when writing Perl to save time (instead of having fun), it helps to look whether you're actually saving time ;) Why spend 5 minutes doing manually what you can spend three years automating? ;)
[Corion]: choroba: Oh, yeah :-D

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (6)
As of 2017-07-27 09:34 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (408 votes). Check out past polls.