×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Contact US

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help on Return statement

Help on Return statement

Help on Return statement

(OP)
I have used the following syntax that I found in the "Perl CD BookShelf" by O'Reilly (by the way, this CD is really fantastic).

Unfortunatly, I don't understand the return statement...
Can somebody explain this?

sub trim {
    my @out = @_;
    for (@out) {
        s/^\s+//;
        s/\s+$//;
    }
    return wantarray ? @out : $out[0];
}

Thanks,

Thierry

RE: Help on Return statement

from O'Reilly's "Programming Perl",.....

quote...
Trinary ?: is the conditional operator, just as in C.  It words as:

TEST_EXPR ? IF_TRUE_EXPRESS : IF_FALSE_EXPR

much like an if-then-else, except that it can safely be embedded within other operations and functions.  If the TEST_EXPR is true, only the IF_TRUE_EXPR is evaluated, and value of the expression becomres the value of th entire expression.  Otherwise, only the IF_FALSE_EXPR is evaluated, and it value becomes the value of the entire expression.
.....end quote.

So, if 'wantarray' is a string or a function or variable that evaluates true, then the RETURN spits out the entire array, @out.  If 'wantarray' is a function or variable that evaluates to false, then the RETURN spits out the first element of the array, @out.

'hope this helps.....

 
 
 
 keep the rudder amid ship and beware the odd typo

RE: Help on Return statement

Also, remember that, if wantarray does not exist, it evaluates to false.  If it is a string or it is a number !=0, it will evaluate to true.


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Help on Return statement

why wouldn't wantarray exist? wantarray is a perl function. a really cool one at that. this line is saying that if you call the subroutine in a scalar context, it provides a scalar return, otherwise it returns an array.

adam@aauser.com

RE: Help on Return statement

I was talking about a more generic instance of the ? statment.  Actually, I had no idea what the wantarray function did.  Is it built in?  

If it weren't built in, then it might not exist, and if it did not exist, it would evaluate as false.  That's all I was saying.

Sounds like a cool function though.  I'll have to remember that.


Sincerely,
 
Tom Anderson
CEO, Order amid Chaos, Inc.
http://www.oac-design.com

RE: Help on Return statement

it is built in, at least in 5.6, i'm not sure about previous versions of perl, because i dont have a previous version to test with.

adam@aauser.com

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close