Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling

Re: Adding Default Values [for BEGINNERS]

by OfficeLinebacker (Chaplain)
on Dec 21, 2006 at 22:54 UTC ( #591215=note: print w/replies, xml ) Need Help??

in reply to Adding Default Values [for BEGINNERS]

Well, since you've exposed your dirty little secret, and that takes courage, I'm going to join you: I too follow NASCAR. Go #20.

Anyway, here's a quick and dirty fix. Note the first two lines after !#. That's the most important part. The second most important part, and the part that fixes your problem, is chomping the input BEFORE looking at it to determine whether you want to use the default file. Before you chomp the input, it has a newline character in it, which resolves to true if you test it. If the person only hit enter, and you chomp the value, THEN you have a value that will test false.

Note there are lots of other ways to improve your code and this is "quick and dirty" at best.

#! /your/path/to/perl use strict; use warnings; print "\n"; print "Which file do you want to open? "; # we are asking which file+ +to open my $file0 = <STDIN>; chomp($file0); my $file = $file0 || "somelist"; #here's my effort to apply a defaul +t t+o the input if it is blank. # THIS IS WHAT I WOULD LIKE TO DO -> if stdin is blank, i'd like to as ++sign $file a variable. print "OK, I will look in $file.\n"; # print "\n"; print "Which driver do you want to see? "; # we are asking to output+ + info from a particular line chomp(my $name = <STDIN>); # the more idiomatic way print "Aah, $name, truly a great driver.\n"; # Our requested file is accessed and displayed my $bibfile = "$file"; open(IN, "<$bibfile") or die "Can't open $bibfile\n"; print "File Contents:\n"; while (my $line = <IN>) { chomp($line); my ($Drivers,$Points_Rank,$TotalPts,$Behind,$Starts,$Poles,$Wins,$To +p5,$Top10,$DNF,$Winnings) = split /\,/, $line; if ($Drivers=~ m/$name/i) { print " $Drivers, $Points_Rank, $Top10, $Wins, $Starts, $DNF \n"; } # } close IN;

I like computer programming because it's like Legos for the mind.

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others contemplating the Monastery: (4)
As of 2021-11-29 18:29 GMT
Find Nodes?
    Voting Booth?

    No recent polls found