in reply to Is require still required?
It is not obsolete, require is used. One use case:
Usually there's a line reading
as the last evaluated statement in a module file, since require demands a true value from an included file.1;
As LanX pointed out, use is sort of a wrapper around require, but it doesn't return anything, it throws away the value returned by require. It is even a syntax error trying to assign from it:
$result = use Some::Module; # illegal
So you cannot assign from use, but you can from require. This is useful to load e.g. a config stash or getting back a data structure captured from Data::Dumper or Data::Dump into a file:
# file hash.pl # anonymous hash reference { name => 'foo', data => [1,2,3], } __END__
#!/usr/bin/perl # file program.pl use strict; use warnings; use Data::Dump; my $var = require './hash.pl'; dd $var; __END__
Output:
{ data => [1, 2, 3], name => "foo" }
Of course, you can do arbitrary computations in the required file.
If you just want the last computed value from a file without having to declare an our variable in both source and target, or using Exporter, this is the way to do it - if you expect a true value to be returned. Otherwise, you would do FILE.
|
---|
Replies are listed 'Best First'. | |
---|---|
Re^2: Is require still required?
by choroba (Cardinal) on Feb 02, 2024 at 00:25 UTC | |
Re^2: Is require still required?
by tobyink (Canon) on Feb 02, 2024 at 10:13 UTC | |
by shmem (Chancellor) on Feb 02, 2024 at 11:41 UTC | |
by haj (Vicar) on Feb 02, 2024 at 13:34 UTC | |
by shmem (Chancellor) on Feb 03, 2024 at 11:07 UTC | |
by choroba (Cardinal) on Feb 03, 2024 at 20:04 UTC | |
|