Search code examples
perlvoid

Warning in perl useless use of private variable in void context


I have a script that take a string and puts a '.html' at the end of a string. If the string already has a '.hmtl" i want to strip it, and then add the html later It works ok but I get a warning and dont understand the warning. so if i run the script withthe string foo_foo.html, I don't want the result to be foo_foo.html.html, I put in the if statement

if ($input =~ s/\.html$//)

so it strips the 'dot html' from the string

#!/usr/bin/perl
use strict;
use warnings;

# Check if a string is provided as an argument
if (@ARGV != 1) {
    die "Usage: $0 'string'\n";
}

# Get the input string from the command line arguments
my $input = $ARGV[0];

if ($input =~ s/\.html$//) {
        $input ;
}


# Convert all characters to lowercase
$input = lc($input);

# Replace spaces with underscores
$input =~ s/ /_/g;

# Print the modified string
print "<!--- \"$input.html\" -->\n";
print "     \n";
print "     \n";
print "$input.html"

I get this warning

 Useless use of private variable in void context at 
./perl_make_html_string_cut_html.pl line 14.

The script works ok - just the warning is bothering me.


Solution

  • It is this part:

    if ($input =~ s/\.html$//) {
            $input ;   # <----- error line
    }
    

    Perhaps you meant to put a print statement there? As of now it is just a variable in void context, like the error says.

    You could just do:

    $input =~ s/\.html$//;
    

    And skip the if-clause. I would perhaps add /i flag to remove text regardless of case.

    If you have a text editor that shows line numbers, you can just see which line is reporting the error yourself.