Ricardo Lucca
Ricardo Lucca

Reputation: 168

Why a function without returning value has a value in perl?

I'm trying to undestand why a function without a value being returned has a value defined. Could someone explain?

This is the code:

perl -le 'sub xxx { print 2; } print defined(xxx);'

The console output is:

2
1

I was expecting that the value returned was undef and a defined value....

$ perl -v

This is perl 5, version 22, subversion 1 (v5.22.1) built for x86_64-linux-gnu-thread-multi

Upvotes: 0

Views: 106

Answers (1)

choroba
choroba

Reputation: 241868

print is documented to return a value:

Returns true if successful.

And subroutines are documented to return the last expression evaluated when no explicit return is present.

(In the absence of an explicit return, a subroutine, eval, or do FILE automatically returns the value of the last expression evaluated.)

Upvotes: 6

Related Questions