FAQ
Hi,

On shell, successful command returns exit status of 0.

As a best practice what status value shall a Perl function return.

Going by the fact that Perl function returns the value of last command
in it, I think function should return non-zero for a success.

Cheers,
Parag

Search Discussions

  • Uri Guttman at Jan 12, 2011 at 9:38 pm
    "PK" == Parag Kalra writes:
    PK> Hi,
    PK> On shell, successful command returns exit status of 0.

    PK> As a best practice what status value shall a Perl function return.

    a shell command is NOT a function call. comparing them is useless.

    PK> Going by the fact that Perl function returns the value of last command
    PK> in it, I think function should return non-zero for a success.

    and why would you think that? how would you know what the real return
    value is (from return) if it returned non-zero for success? try to define
    success for all functions?

    uri

    --
    Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
  • Dr.Ruud at Jan 12, 2011 at 10:47 pm

    On 2011-01-12 22:23, Parag Kalra wrote:

    On shell, successful command returns exit status of 0.

    As a best practice what status value shall a Perl function return.
    A function can return other kinds of values too.

    Going by the fact that Perl function returns the value of last command
    in it, I think function should return non-zero for a success.
    It all depends. Read for example DBI(3), and see that some functions and
    methods return handles, other return the number of touched rows (with
    '0E0' as a special true zero), some will return a textual string, etc.

    Often a worker function returns an undef (or an empty list) as a sign
    that something didn't work out. This enables coding like:

    my $foo = bar( @baz )
    or die "huh? bar() failed!";

    There are also subs that don't return anything. You can for example
    write a sub that should change its parameters in place if the sub is
    called in void context, for example C<trim( $text )>.

    --
    Ruud
  • Octavian Rasnita at Jan 13, 2011 at 6:29 am
    From: "Parag Kalra" <paragkalra@gmail.com>
    Hi,

    On shell, successful command returns exit status of 0.

    As a best practice what status value shall a Perl function return.

    Going by the fact that Perl function returns the value of last command
    in it, I think function should return non-zero for a success.

    Cheers,
    Parag


    Perl doesn't use functions, but subroutines or methods, so they don't need
    to return something if you don't want them to return something.
    If they return something they work like a function.

    So the subroutines may return arrays or scalar variables which can also be
    references to other kinds of variables if you need those variables as a
    result of the subroutine.

    If you just want to find if a subroutine returned true or false, you can
    just return 1 or 0.

    Octavian
  • Uri Guttman at Jan 13, 2011 at 7:01 am
    "OR" == Octavian Rasnita writes:
    OR> Perl doesn't use functions, but subroutines or methods, so they don't

    don't say that. subs and functions are just synonyms. it is how you use
    the sub that changes its meaning.

    OR> need to return something if you don't want them to return something.
    OR> If they return something they work like a function.

    subs in perl ALWAYS return something, either the value from return or
    the last evaluated expression. even an empty sub {} returns undef/()
    depending on wantarray. it is up the caller to use or ignore the return
    value.

    uri

    --
    Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
  • Shawn wilson at Jan 13, 2011 at 7:12 am

    subs in perl ALWAYS return something, either the value from return or
    the last evaluated expression.
    What do you mean by this?

    sub nothing {
    my $something = 5;
    if ( $something == 5) {}
    }

    ... will return 'undef' and not 5 or anything else, right?
  • John W. Krahn at Jan 13, 2011 at 7:18 am

    Octavian Rasnita wrote:
    From: "Parag Kalra" <paragkalra@gmail.com>
    On shell, successful command returns exit status of 0.

    As a best practice what status value shall a Perl function return.

    Going by the fact that Perl function returns the value of last command
    in it, I think function should return non-zero for a success.

    Perl doesn't use functions, but subroutines or methods, so they don't
    need to return something if you don't want them to return something.
    If they return something they work like a function.
    You are thinking of a language like BASIC where there is a distinction
    between functions, which return a value, and subroutines, which return
    nothing, and there are separate keywords to define each one.

    Perl has operators/functions and subroutines (and keywords.)

    print;

    That is an example of an operator.

    print();

    And that is an example of a function.

    So the subroutines may return arrays or scalar variables
    Subroutines in Perl can only return lists of scalar values, never arrays.

    which can also
    be references to other kinds of variables if you need those variables as
    a result of the subroutine.

    If you just want to find if a subroutine returned true or false, you can
    just return 1 or 0.
    If you want to return a false value it is usually better to use return
    with no value:

    return;

    As that will return the correct false value in both scalar and list context.



    John
    --
    Any intelligent fool can make things bigger and
    more complex... It takes a touch of genius -
    and a lot of courage to move in the opposite
    direction. -- Albert Einstein
  • Uri Guttman at Jan 13, 2011 at 7:24 am

    "sw" == shawn wilson writes:
    subs in perl ALWAYS return something, either the value from return or
    the last evaluated expression.
    sw> What do you mean by this?

    sw> sub nothing {
    sw> my $something = 5;
    sw> if ( $something == 5) {}
    sw> }

    sw> ... will return 'undef' and not 5 or anything else, right?

    from perldoc perlsub:

    A "return" statement may be used to exit a subroutine, optionally
    specifying the returned value, which will be evaluated in the
    appropriate context (list, scalar, or void) depending on the context of
    the subroutine call. If you specify no return value, the subroutine
    returns an empty list in list context, the undefined value in scalar
    context, or nothing in void context. If you return one or more
    aggregates (arrays and hashes), these will be flattened together into
    one large indistinguishable list.

    If no "return" is found and if the last statement is an expression, its
    value is returned. If the last statement is a loop control structure
    like a "foreach" or a "while", the returned value is unspecified. The
    empty sub returns the empty list.

    i never said subs return something useful but something is always
    available to the caller on the stack. this is one (of many) reasons i
    say to (almost) always use explicit returns. it guarantees you know what
    you are returning, it tells the reader what is being returned and it
    avoids bugs. one classic bug is some code that returns the last
    expression and someone adds code after that breaking the return. another
    bug is when the last statement is some compound thing like if/else and
    it may return the right thing but it is hard to tell what is going
    on. and as the docs say a loop control will be unspecified in what it
    returns. so use explicit return statements to avoid all of these
    issues. simple and important.

    there are some special cases where no return statement may be better. i
    won't go into those now.

    uri

    --
    Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
  • Shawn wilson at Jan 13, 2011 at 7:35 am

    On Jan 13, 2011 2:24 AM, "Uri Guttman" wrote:
    "sw" == shawn wilson <ag4ve.us@gmail.com> writes:
    subs in perl ALWAYS return something, either the value from return or
    the last evaluated expression.
    sw> What do you mean by this?

    sw> sub nothing {
    sw> my $something = 5;
    sw> if ( $something == 5) {}
    sw> }

    sw> ... will return 'undef' and not 5 or anything else, right?

    from perldoc perlsub:

    A "return" statement may be used to exit a subroutine, optionally
    specifying the returned value, which will be evaluated in the
    appropriate context (list, scalar, or void) depending on the context of
    the subroutine call. If you specify no return value, the subroutine
    returns an empty list in list context, the undefined value in scalar
    context, or nothing in void context. If you return one or more
    aggregates (arrays and hashes), these will be flattened together into
    one large indistinguishable list.

    If no "return" is found and if the last statement is an expression, its
    value is returned. If the last statement is a loop control structure
    like a "foreach" or a "while", the returned value is unspecified. The
    empty sub returns the empty list.

    i never said subs return something useful but something is always
    available to the caller on the stack. this is one (of many) reasons i
    say to (almost) always use explicit returns. it guarantees you know what
    you are returning, it tells the reader what is being returned and it
    avoids bugs. one classic bug is some code that returns the last
    expression and someone adds code after that breaking the return. another
    bug is when the last statement is some compound thing like if/else and
    it may return the right thing but it is hard to tell what is going
    on. and as the docs say a loop control will be unspecified in what it
    returns. so use explicit return statements to avoid all of these
    issues. simple and important.

    there are some special cases where no return statement may be better. i
    won't go into those now.
    I dig what you're saying about always using return. However I don't (have
    never used / seen) a case where a sub returns last expression. An example
    maybe?
  • Uri Guttman at Jan 13, 2011 at 7:47 am
    "sw" == shawn wilson writes:
    sw> I dig what you're saying about always using return. However I
    sw> don't (have never used / seen) a case where a sub returns last
    sw> expression. An example maybe?

    the classic case which is used in the constant pragma is:

    sub FOO() { 'foo' }

    the last (and only) expression is returned. the () make it a prototype
    with no args so it will reduce to the constant value at compile time.

    it is mostly when you see stuff like this:

    sub foo {

    # code here

    $stuff ;
    }

    $stuff is returned if it falls through to the that line. simple.

    as i said don't do that unless it is a special case. use explicit return
    statements.

    uri

    --
    Uri Guttman ------ uri@stemsystems.com -------- http://www.sysarch.com --
    ----- Perl Code Review , Architecture, Development, Training, Support ------
    --------- Gourmet Hot Cocoa Mix ---- http://bestfriendscocoa.com ---------
  • Dr.Ruud at Jan 13, 2011 at 8:02 am

    On 2011-01-13 08:18, John W. Krahn wrote:

    If you want to return a false value it is usually better to use return
    with no value:

    return;
    I prefer subs to normally return a single scalar, which can be a direct
    value like undef, or a reference to a more complex data structure, etc.

    If you allow subs that do C<return;>, then you need to be careful if
    elsewhere in the code there are things like:

    my $h = { a => foo(), b => bar() };

    because if both foo() and bar() would just return, this ends up in meaning:

    my $h = { a => b };

    and that is clearly not what was meant by the coder.


    To prevent this, you can change the code to

    my $h = { a => scalar foo(), b => scalar bar() };

    but that feels like fixing it at the wrong end to me.


    Yeah, maybe the '=>' should enforce scalar context, though that would
    just lead to other surprises.


    Simple rule: a sub that in normal circumstances returns a scalar, should
    not do C<return;> but do C<return undef;> in the special case.

    --
    Ruud
  • John Delacour at Jan 13, 2011 at 10:39 am

    At 02:34 -0500 13/01/2011, shawn wilson wrote:
    I dig what you're saying about always using return. However I don't (have
    never used / seen) a case where a sub returns last expression. An example
    maybe?
    #!/usr/local/bin/perl
    use strict;
    use feature qw(say);

    say &SUB();
    sub SUB {
    my $word = "My word!";
    }

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
groupbeginners @
categoriesperl
postedJan 12, '11 at 9:24p
activeJan 13, '11 at 10:39a
posts12
users7
websiteperl.org

People

Translate

site design / logo © 2021 Grokbase