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

Adding Default Values [for BEGINNERS]

by brusimm (Pilgrim)
on Dec 21, 2006 at 22:36 UTC ( #591210=perlquestion: print w/ replies, xml ) Need Help??
brusimm has asked for the wisdom of the Perl Monks concerning the following question:

Hello, I am asking for assistance from any monk wandering by this place of solitude and confusion. I am new, and I am working on an exercise problem I have created for myself from various sources. I have perused the halls of the monks, google, the cookbook (Which got me closer to my idea of a goal) and Learning Perl.

I wish to add a default value to my script and I do not understand how to apply it to the script. The file I am looking to open is called somelist. (disclaimer): It's hard for me to see some things, so my semi colons are in odd locations for now, so I can readily see where they are , and if any are missing. At this point in time, that is pretty helpful to me. So please be patient. Here is the script.

#!/usr/local/bin/perl print "\n" ; print "Which file do you want to open? " ; # we are asking which file + to open $file0 = <STDIN> ; $file = $file0 || "somelist"; #here's my effort to apply a default 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. chomp($file) ; # print "\n" ; print "Which driver do you want to see? " ; # we are asking to output + info from a particular line chomp ($name = <STDIN>) ; # the more idiomatic way # Our requested file is accessed and displayed $bibfile = "$file" ; open (IN, "<$bibfile") or die "Can't open $bibfile\n"; print "File Contents:\n" ; while ($line = <IN>) { chomp($line) ; ($Drivers,$Points_Rank,$TotalPts,$Behind,$Starts,$Poles,$Wins,$Top5, +$Top10,$DNF,$Winnings) = split /\,/, $line; if ($Drivers=~ m/$name/i) { print " $Drivers, $Points_Rank, $Top10, $Wins, $Starts, $DNF \n"; } # } close IN ;
Yes, I'm a nascar fan. I can't help it.. I got hit in the head. SO anyway, when I apply the default, all it says is "Can't open". BUT if I pull out my attempt to add a default variable, this script works fine.

My real issue is that though I've seen the different types of default value examples, I do not know how to apply them to a script properly. In particular, this script.

My real learning curve comes from seeing examples applied, rather than reading the definitions of things, hence, my mental block. Thank you for your time.

Comment on Adding Default Values [for BEGINNERS]
Download Code
Re: Adding Default Values [for BEGINNERS]
by GrandFather (Cardinal) on Dec 21, 2006 at 22:50 UTC

    For a start always use strictures: use strict; use warnings;.

    It may be smart to chomp the input you read from <STDIN>:

    my $file = <STDIN>; chomp $file; $file ||= "somelist";

    DWIM is Perl's answer to Gödel
Re: Adding Default Values [for BEGINNERS]
by ikegami (Pope) on Dec 21, 2006 at 22:52 UTC

    $file0 is not blank. It contains \n. Just use chomp to remove the newline before checking if the variable is "empty".

    my $file = <STDIN>; chomp($file); $file ||= "somelist";

    The above will also use the default if 0 (zero) is entered. The following avoids that problem:

    my $file = <STDIN>; chomp($file); $file = "somelist" if not length $file;

    By the way, you really, really, really should use use strict; and use warnings;.

Re: Adding Default Values [for BEGINNERS]
by OfficeLinebacker (Chaplain) on Dec 21, 2006 at 22:54 UTC

    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.
Re: Adding Default Values [for BEGINNERS]
by ikegami (Pope) on Dec 21, 2006 at 22:57 UTC

    What follows is a sample construct for asking again instead of using a default.

    my $file; for (;;) { print "Which file do you want to open? "; $file = <STDIN>; chomp($file); last if length $file; print "Please supply a file name.\n"; }
Re: Adding Default Values [for BEGINNERS]
by brusimm (Pilgrim) on Dec 21, 2006 at 23:27 UTC
    Thanks. Those ideas seem to work, and the funny thing is, i do know about chomping and forgot to do it. I use -w when calling the scripts.

    One pickle.. I am using winXp, and ActivePerl and when i add USE STRICT, my script blows up on me with GLOBAL SYMBOL error statements. It pretty much complains about everything from line 22 and down, "requiring explicit package name..." for the different variables the script encounters.

    Also, after adding the chomp, and pulling out the \n print, I have to hit return twice, but it does use the provided default. Any thoughts?

    Again, thank you everyone.

      Adding use strict; requires that you declare variables using my. For example in:

      my ($Drivers, $Points_Rank, $TotalPts, $Behind, $Starts, $Poles, $Wi +ns, $Top5, $Top10, $DNF, $Winnings) = split /\,/, $line;

      the my is required.


      DWIM is Perl's answer to Gödel
      I have to hit return twice

      For each and every occurrence of <STDIN> you should have to hit return once (and once only).
      In making your changes have you inadvertently introduced another <STDIN> ?

      Cheers,
      Rob
        Thanks to everyone for your patience and insight. As always, it's been an excellent learning experience.

Log In?
Username:
Password:

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://591210]
Approved by GrandFather
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (6)
As of 2014-09-19 23:51 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    How do you remember the number of days in each month?











    Results (151 votes), past polls