dagolden / ToolSet

(Perl) Load your commonly-used modules in a single import
http://search.cpan.org/dist/ToolSet
4 stars 2 forks source link

How to export values with spaces? #2

Closed hakonhagland closed 8 years ago

hakonhagland commented 8 years ago

I am not able to export module arguments containing white spaces. For example, with the Data::Printer module, I can use

use Data::Printer
  caller_info    => 1,
  caller_message => q{Printing line __LINE__ of __FILENAME__:};

If I try to transfer this to my ToolSet package:

package my_defaults;

use base 'ToolSet'; 
use v5.16;
use Encode ();

ToolSet->use_pragma( 'strict' );
ToolSet->use_pragma( 'warnings' );
ToolSet->use_pragma( qw/feature :5.16/ );
ToolSet->use_pragma( 'utf8' ); 
ToolSet->use_pragma( qw/open :std :utf8/ ); 
ToolSet->use_pragma( qw/warnings FATAL utf8/ );
ToolSet->export(
    'Data::Printer'    => [
        caller_info    => 1,
        caller_message => q{Printing line __LINE__ of __FILENAME__:},
    ],
);
1;

the argument q{Printing line __LINE__ of __FILENAME__:} is split into serveral at white space..

How to achieve this?

hakonhagland commented 8 years ago

Added a pull request trying to fix the problem.

jayallen commented 8 years ago

Wow, I was just coming here to report the same issue with using ToolSet with Data::Printer except that I usually use the hashref form:

use Data::Printer {
  caller_info    => 1,
  caller_message => q{Printing line __LINE__ of __FILENAME__:}
};

The forcing of qw() on the export argument hash values seems to make it impossible either way.

jayallen commented 8 years ago

With Data::Printer, a workaround is using an rc_file:

ToolSet->export(
    'DDP' => [ rc_file => "/path/to/my/.dataprinter" ],
);
xdg commented 8 years ago

I'm concerned with the hashref approach of #3 because I think it could get fragile. It also leads me to wonder what to do about import() subs that expect hash references or hash references as the value from some key:

use Foo -someopt => { foo => 1, bar => 2 };

I wonder if giving a way of passing a literal string to the eval would be the most flexible -- then people can put in whatever they need. Eg. having a reference to a string mean that the string should be passed through verbatim:

ToolSet->export( Foo => \"-someopt => { foo => 1, bar => 2 }" );

In the Data::Printer case, it could look like this:

$DP_args = <<'HERE';
        caller_info    => 1,
        caller_message => q{Printing line __LINE__ of __FILENAME__:},
HERE

ToolSet->export( 'Data::Printer'    => \$DP_args );

What do you think? I have a branch that has this working, but would like feedback before I ship it.

hakonhagland commented 8 years ago

Good idea! I did not realize that you could use package $caller inside an eval like that. Nice. It also seems to work well for my test cases.

xdg commented 8 years ago

Great! I've shipped it to CPAN.