Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[doc] join with undef values #22205

Open
hexcoder- opened this issue May 10, 2024 · 7 comments
Open

[doc] join with undef values #22205

hexcoder- opened this issue May 10, 2024 · 7 comments

Comments

@hexcoder-
Copy link

Where

https://perldoc.perl.org/blead/functions/join as of 05/10/2024.

Description

join EXPR,LIST

  1. The documentation does not state how the return value is being built, when LIST contains 'undef' values.

F.e.:
my $rec = join(':', undef, undef);
does return ':' (with warnings).

What is missing in the documentation is that LIST items are stringified so that undefined values will be transformed to empty strings yielding the result of two empty string LIST items.

(In my first reading I overlooked the statement that join ALWAYS returns a string. So I assumed a single LIST item of undef would result in an undef value).

@hexcoder- hexcoder- changed the title [doc] [doc] join with undef values May 10, 2024
@Grinnz
Copy link
Contributor

Grinnz commented May 10, 2024

All string functions interpret their arguments as strings, with the exception of length which special-cases undef inputs.

@hexcoder-
Copy link
Author

Thanks!
My main investigation was #22207, which had me look up join´s documentation.

@mauke
Copy link
Contributor

mauke commented May 10, 2024

The very first sentence:

Joins the separate strings of LIST into a single string with fields separated by the value of EXPR, and returns that new string.

Mentions both that list elements are treated as strings and that the return value is a string.

@hexcoder-
Copy link
Author

I agree. The implication is that undefs are not preserved, which I would have liked to be told explicitly.

Ok, if I am the only person to find this description very terse and incomplete regarding "undefined", fell free to reject this issue.

@guest20
Copy link

guest20 commented May 12, 2024

@hexcoder- The counter-question is, how could undefs be preserved in a string when all strings are defined?

@hexcoder-
Copy link
Author

@guest20: when all strings are defined, there are obviously no undefs to be preserved.
I would modify the question like this "what should join do when there are undefined arguments?"

The motivation for this issue/question came from my "code review" of

my ($val) = join( "', '", DB::eval(@_) );

and the following line
$val = ( ( defined $val ) ? "'$val'" : 'undef' );

where the defined operator is applied to the result of a join.

@Grinnz
Copy link
Contributor

Grinnz commented May 16, 2024

I think you missed the point of the question: strings are by definition defined, not in the argument list but in all of Perl. So when undef is passed to a function that takes strings, it is always interpreted as the empty string with a warning.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

5 participants