perl

Is Perl's "uc" function enabled to use when return to a variable?


Can I return a variable and apply "uc" when return?

Here is the code:

my $desc = "";

@names = ("thor-12345-4567");
$size  = @names;

Thor();
print $desc;
sub Thor() {
    if ($size ne "0") {
        return uc ($desc=$names[0]);
    }
    $desc = "NA";
    return $desc;
}

Can "uc" can be used when we return to a variable?

When I tried to print $desc, it did not return to uppercase.


Solution

  • Avoid assigning to or relying on global variables in your functions. Instead pass parameters and return values.

    use strict;
    use warnings;
    
    my $desc = Thor("thor-12345-4567");
    
    print $desc;
    
    sub Thor {
        my @names = @_;
    
        if (@names){
            return uc $names[0];
        } else {
            return "NA";
        }
    }