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

Re: Bug in code

by graff (Chancellor)
on Nov 20, 2012 at 03:48 UTC ( #1004653=note: print w/replies, xml ) Need Help??

in reply to Bug in code

Where does the user get the values that are being typed in manually? Wouldn't it be easier and more reliable for the user to store these values in a simple little text file? It would be very easy to make your script read from a file instead of the keyboard. (If the user is reading these numbers from some other data file, it would be even better for the script to read that data file, instead of making the user type the values manually while your script is running.)

Reading input from a file will make it a lot easier to debug (and easier to ask for help: show the input to the script, and tell us what you'd like to see as output for the given input). For testing purposes, you can even included test data as part of the source code file. Here's an example of a script that allows the user to name a file on the command line, but if no file name is given, it reads sample data from the source code file itself:

#!/usr/bin/perl use strict; my $source = "test data"; if ( @ARGV ) { $source = shift; open( DATA, '<', $source ) or die "$source: $!\n Usage: $0 [data.file]\n"; } my @inputs; while (<DATA>) { chomp; push @inputs, $_; } # do some stuff with @inputs, as you see fit... e.g. my $sum; my $input_list; for my $value ( @inputs ) { $sum += $value if ( $value =~ /^-?[\d.]+$/ ); $input_list .= " $value"; } printf( "There were %d values in '$source': %s\n The numeric sum is %f +\n", scalar @inputs, $input_list, $sum ); __DATA__ 5 10 non_numeric 15 20

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: note [id://1004653]
and all is quiet...

How do I use this? | Other CB clients
Other Users?
Others making s'mores by the fire in the courtyard of the Monastery: (4)
As of 2018-06-19 01:07 GMT
Find Nodes?
    Voting Booth?
    Should cpanminus be part of the standard Perl release?

    Results (111 votes). Check out past polls.