newbie <STDIN> question

L

leegold

What if a user presses enter for the line number?
Please see below.
How will I test for that and make $linenum default to 1 ?
IE. what's the value of STDIN when someone just hits return?
So I can test for that and make default eq. to 1.
Or is there some automagic?
Thanks

my $linenum;
......snip...

print "What's your line number? [1]\n"; $linenum = <STDIN>;
chomp(&linenum);
IF (not $linenum =~ m/^\d+$/) {
die "only positive integers accepted\n";
}
......
 
C

Chris Mattern

leegold said:
What if a user presses enter for the line number?
Please see below.
How will I test for that and make $linenum default to 1 ?
IE. what's the value of STDIN when someone just hits return?

It occurs to me that in the time it took you to write this
post, you could've written a test script that would tell
you the answer.

Chris Mattern
So I can test for that and make default eq. to 1.
Or is there some automagic?
Thanks

my $linenum;
.....snip...

print "What's your line number? [1]\n"; $linenum = <STDIN>;
chomp(&linenum);
IF (not $linenum =~ m/^\d+$/) {
die "only positive integers accepted\n";
}
.....
 
D

Default

What if a user presses enter for the line number?
Please see below.
How will I test for that and make $linenum default to 1 ?
IE. what's the value of STDIN when someone just hits return?
So I can test for that and make default eq. to 1.
Or is there some automagic?
Thanks

my $linenum;
......snip...

print "What's your line number? [1]\n"; $linenum = <STDIN>;
chomp(&linenum);
IF (not $linenum =~ m/^\d+$/) {
die "only positive integers accepted\n";
}
......
Ahh i'm also a newbie but this might just do the trick.
I'd suggest waiting for other more experienced users here to verify this.

use ExtUtils::MakeMaker qw(prompt);

$default = 'yes';
$ans = prompt("Do Something? (yes/no)?", ($default ? 'yes' : 'no'));
$ans = lc($ans);
print "\nans = $ans\n";
if ($ans eq 'yes')
{
print "\nDo something here.\n";
}
else
{
print "\nNot doing something here.\n";
}
print "\nans = $ans\n";
 
T

Tad McClellan

leegold said:
Please see below.


Please see the Posting Guidelines that are posted here frequently.

print "What's your line number? [1]\n"; $linenum = <STDIN>;
chomp(&linenum);
^
^

Where is the linenum() subroutine defined?

IF (not $linenum =~ m/^\d+$/) {
^^
^^

Syntax error.

If you give us not-Perl code, can we give you a not-answer followup?
 
L

leegold

Gunnar said:
After the line

chomp($linenum);

you can for instance add:

$linenum ||= 1;

Thank you. An empty var will evaluate to false.
I read it but it did not "click" till I saw your post.
Thanks again for the help.

Lee G.
 
A

Anno Siegel

What if a user presses enter for the line number?
Please see below.
How will I test for that and make $linenum default to 1 ?
IE. what's the value of STDIN when someone just hits return?
So I can test for that and make default eq. to 1.
Or is there some automagic?
Thanks

my $linenum;
......snip...

print "What's your line number? [1]\n"; $linenum = <STDIN>;
chomp(&linenum);
IF (not $linenum =~ m/^\d+$/) {
die "only positive integers accepted\n";
}
......
Ahh i'm also a newbie but this might just do the trick.
I'd suggest waiting for other more experienced users here to verify this.

use ExtUtils::MakeMaker qw(prompt);

Ugh. MakeMaker is for an entirely different purpose. Yes, it happens
to export a routine "prompt()" that can be used here, but it carries
a *huge* amount of luggage that is used for makefile generation and
serves no purpose here.

Look at the Term::ReadLine module for a prompter.

Anno
 
E

Eric J. Roode

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

(e-mail address removed) (leegold) wrote in
print "What's your line number? [1]\n"; $linenum = <STDIN>;
chomp(&linenum);
IF (not $linenum =~ m/^\d+$/) {
die "only positive integers accepted\n";
}
.....

Please post your real code, not a re-typed copy. How do you expect people
to help you (in general) if you're not showing your real code?

To answer your question, why don't you print out the value and see what it
is? Examine it in the debugger. The debugger is a wonderful tool for
learning the language.
- --
Eric
$_ = reverse sort $ /. r , qw p ekca lre uJ reh
ts p , map $ _. $ " , qw e p h tona e and print

-----BEGIN PGP SIGNATURE-----
Version: PGPfreeware 7.0.3 for non-commercial use <http://www.pgp.com>

iQA/AwUBP8NNmGPeouIeTNHoEQKpOQCeJskwXUngt22QRjGWKX25qrtDh6QAoI9h
/+HfYYVChpIVvegGXUV4eqQj
=lHDI
-----END PGP SIGNATURE-----
 
D

Default

Ugh. MakeMaker is for an entirely different purpose. Yes, it happens
to export a routine "prompt()" that can be used here, but it carries
a *huge* amount of luggage that is used for makefile generation and
serves no purpose here.

Look at the Term::ReadLine module for a prompter.

Anno
Thanks for the advice.. ill check that module out right away.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
474,142
Messages
2,570,819
Members
47,367
Latest member
mahdiharooniir

Latest Threads

Top