Reputation: 21261
Here is the entirety of my perl script:
#!/usr/bin/perl
use v5.10;
use strict;
#use P4;
print "enter location of master_testplan.conf:";
my $master_testplan_conf = <>;
if (chomp($master_testplan_conf) eq "")
{
$master_testplan_conf = 'suites/MAP/master_testplan.conf';
}
print ":" . $master_testplan_conf . ":";
referencing this answer, I thought this would work. However it's not getting the default value inside the if statement for some reason.
What am I doing wrong?
Upvotes: 0
Views: 3078
Reputation: 107040
A few things:
Chomp changes the string, and returns the number of character chomped. After that input line, chomp $master_testplan_conf
is most likely to 1
, so you're comparing 1
to the null string.
You can do it this way:
chomp ( $master_testplan_conf = <> );
if you want to do everything on a single line.
That will read your input and do the chomp in one step. Also, the <>
operator will take files from the command line and <>
will be the first line of the first file on the command line. If you don't want to do that, use <STDIN>
:
chomp ( $master_testplan_conf = <STDIN> );
You may want to sanitize your user's input. I would at least remove any leading and ending blanks:
$master_testplan_conf =~ s/^\s*(.*?)\s*$/$1/; # Oh, I wish there was a "trim" command!
This way, if the user accidentally presses spacebar a few times, you don't pick up the spaces. You also may want to test for the file's existence too:
if ( not -f $master_testplan_conf ) {
die qq(File "$master_testplan_conf" not found);
}
I also recommend to use:
if ( not defined $master_testplan_conf or $master_testplan_conf eq "" ) {
for your if
statement. This will test whether $master_test_conf
is actually defined and not merely a null string. Right now, this doesn't matter since the user has to at least enter a \n
. The $master_testplan_conf
stroll will never be null.
However, it may matter if you decide to use Getopt::Long.
Upvotes: 1
Reputation: 97958
A regex can be handy to check without altering anything:
if ($master_testplan_conf =~ /^\s*$/)
{
$master_testplan_conf = 'suites/MAP/master_testplan.conf';
}
to check undef also:
if (!defined $master_testplan_conf || $master_testplan_conf =~ /^\s*$/)
{
$master_testplan_conf = 'suites/MAP/master_testplan.conf';
}
Upvotes: 0
Reputation: 6571
You're interested in the file and not the string, per se, so use Perl file tests, instead. In this case, use the file test for existence (-e
):
if (-e $master_testplan_conf) {
This gets to the heart of the matter and lets you know whether the input exists in the file system, or not.
Upvotes: 0
Reputation:
From the documentation on chomp:
..It returns the total number of characters removed from all its arguments..
So you need to chomp first and then compare to the empty string. For example:
chomp($master_testplan_conf = <>);
if ($master_testplan_conf eq "") {
// set default value
}
Upvotes: 2
Reputation: 57490
chomp
does not work that way. It directly modifies the variable passed to it and returns the number of characters chomped off. Do this instead:
chomp $master_testplan_conf;
if ($master_testplan_conf eq "") {
# etc.
}
Upvotes: 5
Reputation: 19226
chomp
modifies its argument and does not return it, so you have to rewrite your condition into something like:
chomp($master_testplan_conf);
if ($master_testplan_conf eq "") {
Upvotes: 2