-
Notifications
You must be signed in to change notification settings - Fork 273
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
Enable UTF-8 in the source code of the skeleton app #782
Conversation
Since the skeleton app generated by `dancer2 gen` is by default configured to use UTF-8 (in config.yml), it makes sense to enable UTF-8 in the source code of the main module (lib/${APP_NAME}.pm) as well. Thus Unicode string literals are not double encoded and are rendered properly. Consider this route: get '/' => sub { "щука\n" }; # stored in a file with UTF-8 encoding Before change it would give: $ curl localhost:3000 Ñ�Ñ�ка After change: $ curl localhost:3000 щука ♥
UTF-8 in config.yml has little to do with 'use utf8' in perl scripts. The latter enables you to have the source code itself in UTF-8, thus enabling for example utf characters in variable names, in string constants. Personally I don't see a reason to use utf8 in the source code, unless someone actually needs it, but that is not very common in my experience (source code that is capable of dealing with unicode data does not neccesarily need 'use utf8'). On the other hand I don't really see any negative aspects of it. Maybe just the one, that it can create confusion, when someone is not that familiar with unicode. |
Hey @DavsX, thanks for the quick reaction! You are right the correlation is very small. I was indeed referring to "string constants" (sorry if my phrasing "Unicode string literals" was obscure). IMO need of non-ASCII string constants is arguable from a point of debugging and a not native English speaker, but I leave it up to you (-: I should say that I came up with this change after reading remarks to the
I think one not familiar with Unicode might be confused either If this change is discouraged, then maybe change phrasing in the mentioned config file remark? Namely avoid using "magic" and "should not care". What do you think? |
Well, you are right, that comment in this form is not 100% correct. I personally don't have any preference what regards of rewriting that comment or adding 'use utf8', so it is best to wait for the 'bosses' to come and comment on this :) |
We are already trying to import
|
Yeah, I ran across that code last night and wondered if that was the same intent. I think that code does not work, because you don't
|
Putting 'use utf8' in Dancer2.pm should not affect your own Dancer2 scripts in the way you think. If we add use utf8 in the scaffolding, it would mean that you would HAVE to remember to put it in every package of yours when that is needed. Dancer2 apps rarely consist of only one .pm file. On the other hand the current utf8->import would mean, that in every file in which you use Dancer2; would automatically get the 'use utf8'. That is a more general approach I think, altough if it is not working then we should do something about it. |
I meant the I checked and utf8 pragma is... special. We could import it using In general I'm not in favor of writing utf8 in source code and being more explicit when you do. |
I'm voting for "we already have In short, I vote to merge. |
I've added some simple tests (merged in 02f4894) but these pass on all versions of perl we test with TravisCI. Playing devil's advocate, assuming the added tests are correct then if this is merged, then for some users removing the @mvuets could you provide what version(s) of Dancer2 and what platform(s) you ran into this issue? |
We were calling $pragma->import which should "just work" to apply the pragma to the caller. #782 hints that the utf8 pragma may not be imported correctly. By using Import::Into we have a consistent way to import pragmas (or anything else may wish to) into the caller.
I ran @veryrusty's unit test and it passed on my (ordinal) system as well. Though the original problem remained - First off, I'd like to stress out once more -
Now another bit - you can invoke
The last basic piece - Now combine all bits together. To
Most importantly:
Now, back to the concrete example - the unit test. It works simply because it loads utf8.pm via
Summary.
That's it, thanks. I would like to hear your thoughts. |
@mvuets++, hell, @mvuets+5 ! Thanks for spending the time investigating this and the excellent feedback. I even learnt something new! 😄 @xsawyerx its not that the utf8 pragma is "special" - all pragmas are; eg. what do you expect this to do?
The suggestion to use Import::Into has been merged (#821). That effectively does
I think this can now be closed - though we may want to update the Changes file to reflect this issue is also resolved. |
@mvuets++ Wow! I will add this to the Changes. Amazing work! |
** Happy new year! ** [ ENHANCEMENT ] * GH #778: Avoid hard-coded static page location. (Dávid Kovács) * Speed up big uploads significantly. (Rick Myers) * GH #821: Use Import::Into to import pragmas. (Russell Jenkins) * GH #782: Fix utf8 pragma import. (Maxim Vuets) * GH #786: Perlbrew fix. (Dávid Kovács) * GH #622: Refactoring. (James Raspass) [ DOCUMENTATION ] * GH #713: Change order of statements in Cookbook to not imply that D2::P::Ajax::ajax() calls have priority. (Sawyer X)
Since the skeleton app generated by
dancer2 gen
is by default configured touse UTF-8 (in config.yml), it makes sense to enable UTF-8 in the source code of
the main module (lib/${APP_NAME}.pm) as well. Thus Unicode string literals are
not double encoded and are rendered properly.
Consider this route:
Before change it would give:
After change:
♥