0

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?

Community
  • 1
  • 1
Ramy
  • 20,541
  • 41
  • 103
  • 153

6 Answers6

5

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.
}
jwodder
  • 54,758
  • 12
  • 108
  • 124
2

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 "") {
2

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
}
1

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.

David W.
  • 105,218
  • 39
  • 216
  • 337
0

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';
}
perreal
  • 94,503
  • 21
  • 155
  • 181
0

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.

David
  • 6,462
  • 2
  • 25
  • 22
  • 1
    I see nothing in the question to suggest that the OP is actually trying to determine whether a file exists. – jwodder Oct 28 '13 at 20:41