in reply to Re^4: XML::Twig Support for Parameter Entities (require)
in thread XML::Twig Support for Parameter Entities

Maybe you should stop, then. There is good reason for most people not paying attention to the return value from 'require'.

For your use case, 'do' would be more appropriate than 'require'.

$ echo 13 > lucky.pl $ perl -l print do 'lucky.pl'; print require 'lucky.pl'; print do 'lucky.pl'; __END__ 13 1 13 $

Which actually makes me a little sad. Being able to reasonably use the return value from 'require' would have some nice benefits.

- tye        

Replies are listed 'Best First'.
Re^6: XML::Twig Support for Parameter Entities (%INC)
by shmem (Chancellor) on Aug 13, 2015 at 07:29 UTC

    Yes, of course :-) do updates %INC, and a subsequent require will just return 1, telling that the file has already been loaded. This is actually a feature, it is a conditional do.

    $ echo 13 > lucky.pl $ perl -l print do 'lucky.pl'; if (1 == require 'lucky.pl') { print "already loaded."; delete $INC{"lucky.pl"}; } print require 'lucky.pl'; print do 'lucky.pl'; __END__ 13 already loaded. 13 13

    update: updated code with conditional block

    perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'

      Yes, I realize that 'require' not reparsing the file is a 'feature'. Was that supposed to help justify why you want to use 'require' to get back values instead of using 'do'? If so, I didn't understand it.

      Using a tool that can easily not give you back the value that you want to get back because of something that could have been done in code quite distant from the code at hand is not what I would call a 'feature'.

      - tye        

        Loading a datastructure from a file not related to @INC once, 'do' and 'require' amount to the same. For a conditional 'do', I could check %INC instead of using 'require' and checking the return value, so you have a point here. For autoloading anonsubs as really private subs into a package, things are different, since 'require' checks @INC.

        Yes, it is a bit sad that require doesn't return the same value from the first call at subsequent calls. For the above case, the AUTOLOAD block has to handle that via a private hash.

        perl -le'print map{pack c,($-++?1:13)+ord}split//,ESEL'