Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

Re^4: XML::Twig Support for Parameter Entities (require)

by shmem (Chancellor)
on Aug 13, 2015 at 07:03 UTC ( [id://1138383]=note: print w/replies, xml ) Need Help??


in reply to Re^3: XML::Twig Support for Parameter Entities (errors)
in thread XML::Twig Support for Parameter Entities

It is pretty darn rare for people to care about the return value from a 'require'

I used that quite often, as in

use Data::Dumper; sub DEVEL() { 1 }; # get sample data my hashref = DEVEL ? require 'sampledata.pl' : do_something_costly(); # later on if ( DEVEL ) { open my $fh, 'sampledata.pl' or die "sampledata.pl: $!"; print Dumper($hashref); }

Also, I use function files for autoloading (function.al) which return an anonymous sub and encapsulate private data in their lexical file scope (see also AutoReloader).

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

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

    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        

      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        

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://1138383]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others scrutinizing the Monastery: (9)
As of 2024-04-18 13:21 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found