in reply to 'return:' instead of 'return'

I think you are really creating a label named return here, followed by a constant expression. As that expression is the last expression evaluated, it is used as return value.

Look at this code: It returns 5 and gives a "Useless use of a constant in void context" warning:

sub foo { return: 3; return 5; }

Alexander

--
Today I will gladly share my knowledge and experience, for there are no sweeter words than "I told you so". ;-)

Replies are listed 'Best First'.
Re^2: 'return:' instead of 'return'
by Boldra (Curate) on Jun 12, 2009 at 09:31 UTC
    "Useless use of a constant in a void context"

    Sorry, that's an artefact of my test code, actually what he was doing is more like:
    sub cmd { my ($cmd) = @_; $cmd =~ s/\//\\/ig; my $ret = system($cmd); return: $ret; }
    which is horrible for other reasons, but it doesn't produce the above warning. It was only thanks to perltidy that I noticed that there was something funny going on on the last line.


    - Boldra
      but it doesn't produce the above warning
      That is because in your code, it's the last statement in the sub. So by default, it's the value that is returned, even without a real return.
        And the last statement in a sub seems to be excluded from 'useless use of constant' warnings, I didn't know that.

        However, the other reason I didn't see "Useless use of a constant in a void context" is that my colleagues code has a variable (not a constant) after the 'return:'.


        - Boldra