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

incompatibility of package-block and __DATA__ #22613

Open
happy-barney opened this issue Sep 20, 2024 · 14 comments
Open

incompatibility of package-block and __DATA__ #22613

happy-barney opened this issue Sep 20, 2024 · 14 comments

Comments

@happy-barney
Copy link

happy-barney commented Sep 20, 2024

When using package block syntax and __DATA__ in single file, __DATA__ doesn't belong to package
but to the main.

__DATA__ documentation states:

Text after __DATA__ may be read via the filehandle "PACKNAME::DATA",
    where "PACKNAME" is the package that was current when the __DATA__ token
    was encountered.

Technically it is correct, __DATA__ token is not specified inside package block (ie, package block prevents usage of __DATA__)
But with code like in example that may be misleading.
It may also harm adoption of syntax.

Example:

use strict;
use warnings;

package Foo {
    print while <DATA>;
}

__DATA__
hello
world

Output

Name "Foo::DATA" used only once: possible typo at example.pl line 7.
readline() on unopened filehandle DATA at example.pl line 7.

All Perls v5.12 .. v5.40 are affected.

Proposal:

a) support __DATA__ inside package block (ie, it will be treated as } as well
b) treat insignificant content following } as content still belonging to the latest package block

@Tux
Copy link
Contributor

Tux commented Sep 20, 2024

I do not see that warning when using __DATA__, but I do see it when using __END__

@mauke
Copy link
Contributor

mauke commented Sep 20, 2024

I get

hello
world

as expected.

@happy-barney
Copy link
Author

@mauke @Tux ouch, oops, I mentioned package block and typed example without it :-( ... editing issue

@mauke
Copy link
Contributor

mauke commented Sep 20, 2024

That is working as expected. The whole point of the package Foo { ... } syntax is that everything outside the braces is not part of the Foo package.

@happy-barney
Copy link
Author

@mauke that I know, I mentioned that in technically part.

I also mentioned possible confusions and fact, that there is no alternative to attach __DATA__ to package except of old package syntax

@Leont
Copy link
Contributor

Leont commented Sep 20, 2024

there is no alternative to attach DATA to package except of old package syntax

I get that you might not like that aesthetically, but when is that a practical problem?

@happy-barney
Copy link
Author

@Leont bad wording, likely to confuse

rest is only convenience - forcing inconsistent syntax across code base or, in case of class, weird syntax for class (especially if there is still hope of increasing usage of Perl)

@iabyn
Copy link
Contributor

iabyn commented Sep 22, 2024 via email

@shlomif
Copy link
Contributor

shlomif commented Sep 22, 2024

In addition, I'd like to note I feel __DATA__ is too abused and overused. I recommend using here-docs or https://metacpan.org/pod/File::ShareDir rather than the global-variable-like-*DATA

@happy-barney
Copy link
Author

@shlomif too doesn't mean that its usage isn't advocated as well

@Tux
Copy link
Contributor

Tux commented Sep 23, 2024

In addition, I'd like to note I feel __DATA__ is too abused and overused. I recommend using here-docs or https://metacpan.org/pod/File::ShareDir rather than the global-variable-like-*DATA

I wholeheartedly disagree. I use __DATA__ and __END__ a lot and I have never in my life used File::ShareDir nor did I ever feel the need for that.

Main reason is that scripts with __END__/__DATA__ hold that data within and are easy to copy to other hosts whereas external files need to be copied seperately with all possible isseus.

Here-docs are file for a single-use short piece of data, but when dealing with a list or longer data structures or text pieces to apply tests to (like: does this parse), here-docs only blur the code

My € 2.00

@haarg
Copy link
Contributor

haarg commented Oct 2, 2024

Everything here is working as expected, and I don't think there's anything worth changing. __DATA__ is inherently a file-based feature, and trying to make it interact with inner lexical scopes isn't going to work well. class isn't any different from package here, and can be written without an enclosing block.

If you need a block of data inside a scope, you can use a heredoc.

@happy-barney
Copy link
Author

@haarg I will repair your sentence: "Everything here is working as implemented", because this issue is about to change "as expected".

@haarg
Copy link
Contributor

haarg commented Oct 2, 2024

No, I chose my words intentionally. Based on how these features work, doing something other that how they work right now would be very strange.

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

No branches or pull requests

8 participants