1

Just a simple question I want to ask. I am beginner for Perl script, sorry if you feel this question is stupid.

The question is 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;
}

I just want to know that is "uc" can be use when we return to a variable?

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

Thank you very much!

  • 1
    The quick answer is, yes. But why not just test and see? You really should isolate your subs though so that they don't rely on or assign to global variables. Instead pass parameters `sub Thor { my @names = @_; ` And return values. `$desc = Thor(@names)` – Miller Mar 06 '14 at 01:58
  • Are you asking if you can apply `uc` to a statement? – TLP Mar 06 '14 at 02:00
  • @Miller Oh, sorry i did not post the answer that i get.I will edit it. I tested it already. It did not return to Uppercase. – Thor_that_new_in_programming Mar 06 '14 at 02:00
  • You're returning a value from the subroutine, but not actually assigning that returned value to anything. I shall provide a detailed answer instead. – Miller Mar 06 '14 at 02:02
  • @TLP yeap, i am asking can we apply uc on a return statement? – Thor_that_new_in_programming Mar 06 '14 at 02:02
  • @TLP: That comment helps how? And I think you mean *whether*. – Borodin Mar 06 '14 at 02:37
  • @user2709555 Yes, you can apply `uc` to a statement. It does not matter what function the statement is an argument to, `return` or otherwise. – TLP Mar 06 '14 at 03:25
  • You assign to `$desc` and then return `uc $desc`. But you don't change the value in `$desc` which still contains the lower-case version. The simplest fix would be `return $desc = uc $names[0]`, but Miller's rewrite is much better. – Dave Cross Mar 06 '14 at 10:23
  • @TLP: I may have been rather tired at 3:00am but I think that's an unnecessary response. What I mean is that you can apply `uc` only to an *expression*. It wouldn't compile in front of a compound statement, and a simple statement is just an expression. – Borodin Mar 06 '14 at 11:24

1 Answers1

6

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";
    }
}
Miller
  • 34,962
  • 4
  • 39
  • 60
  • Thank you for you time Miller!This is very helpful to me! I really appreciate this! One more question, why we need to "use strict" and "use warnings"? – Thor_that_new_in_programming Mar 06 '14 at 02:07
  • 3
    The short answer is that it will encourage you to make better code, and will also help you find syntax errors a LOT faster. All professionals use it and should. For a more detailed answer, I just googled "stackoverflow why use strict use warnings" and the following thread popped to the front: [Why use strict and warnings?](http://stackoverflow.com/questions/8023959/why-use-strict-and-warnings) – Miller Mar 06 '14 at 02:10
  • That's a nice answer so +1. A recommendation to use only lower case and underscore except for globals would have been the cherry. – Borodin Mar 06 '14 at 02:35