http://qs321.pair.com?node_id=11115288


in reply to RFC: Perl<->JSON<->YAML<->Dumper : roundtripping and possibly with unicode

G'day bliako,

As requested, here's some comments and suggestions.

Naming

Subroutine names with a leading underscore tend to suggest private methods or implementation details; these are generally for internal use and not for users of module, i.e. not part of the interface — _qquote_redefinition_by_Corion() is a part of the implementation and the leading underscore is appropriate. The other four subroutines (_read_from_* and _write_to_*) are, in my opinion, part of the interface — consider removing the leading underscore from these.

Exporting

I wouldn't export those ten subroutines by default (i.e. via @EXPORT); in fact, I wouldn't export anything by default, instead using @EXPORT_OK as recommended in "Exporter: Selecting What to Export".

In addition to using @EXPORT_OK, I would add %EXPORT_TAGS so that users of the module do not have to write long import lists. The following is an example of what can be done; it is not a recommendation of what you should do, although I do think something along these lines is a good idea.

use Exporter 'import'; our (@EXPORT_OK, %EXPORT_TAGS); BEGIN { my @file = qw{read_from_file write_to_file}; my @fh = qw{read_from_filehandle write_to_filehandle}; my @io = (@file, @fh); my @json = qw{perl2json json2perl}; my @yaml = qw{perl2yaml yaml2perl}; my @dump = qw{perl2dump dump2perl}; my @all = (@io, @json, @yaml, @dump); @EXPORT_OK = @all; %EXPORT_TAGS = ( file => [@file], fh => [@fh], io => [@io], json => [@json], yaml => [@yaml], dump => [@dump], all => [@all], ); }

Layout

Some of your code layout could be improved. Lines like this one are not easy to read:

if( ! _write_to_filehandle($FH, $contents) ){ warn "error, call to ".' +_write_to_filehandle()'." has failed"; close $FH; return undef }

That's 134 characters long; plus there's leading whitespace for indentation. This would be much more readable, at least in my opinion, as:

if( ! _write_to_filehandle($FH, $contents) ) { warn "error, call to ".'_write_to_filehandle()'." has failed"; close $FH; return undef; }

— Ken

Replies are listed 'Best First'.
Re^2: RFC: Perl<->JSON<->YAML<->Dumper : roundtripping and possibly with unicode
by bliako (Monsignor) on Apr 09, 2020 at 19:22 UTC

    Thank you kcott for your comments. I will adopt "Exporting" 100%. For the last one, "Layout", here is the logic to this madness: I try to put all "boring" code in one long line, like : if error then print blah blah blah and return error A long line for me means that all is lost, sub is exiting. If it's a warning I always make it multiline like you suggest. For your suggestion "Naming", I will just wait and see which of those will make it public or not.

    thank you