in reply to Re^5: XML::Twig Support for Parameter Entities (do)
in thread XML::Twig Support for Parameter Entities

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'

Replies are listed 'Best First'.
Re^7: XML::Twig Support for Parameter Entities (AAaD)
by tye (Sage) on Aug 13, 2015 at 07:37 UTC

    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'

        My testing shows that 'do' makes use of @INC.

        So it seems that I've found two problems with do's bit:

        except that it's more efficient and concise, keeps track of the current filename for error messages, searches the @INC directories, and updates %INC if the file is found.

        For me, 'do' does "search @INC" and does "update %INC", despite the docs.

        - tye