locate ($pattern, [ $database ],
[ -rex = 1> ], [ -rexopt => 'e'|'i'|'ie' ], [
$coderef ])
Scans a slocate/locate-db file for a given
$pattern . $pattern may
contain globbing-characters or it can be a POSIX regular expression if
-rex is true. It figures out the type of
$database and does the right thing. If
$database is neither a locate- nor a slocate-db, it
will croak.
"locate()" can take three
additional parameters. A string is taken to be the
$database that should be searched:
print locate "*.mp3", "/usr/var/locatedb";
If no database is given, locate() looks up the value of
the LOCATE_PATH environment variable and uses its value as the database.
If this string is empty, it gives up.
Passing a code-reference makes locate() call the code
for each match it finds in the database. The current match will be in
$_:
locate "*.mp3", sub { print "MP3 found: $_\n" };
This means that no huge return list has to be built and it is
therefore more suitable for scans that return a lot of matches.
Eventually, you can specify two options -rex and
-rexopt. When -rex is true, the pattern will be treated as
a POSIX regular expression. Note that those are not Perl regular
expressions but the rather limited regular expressions that you might
know from programs such as grep(1) and the lot. Per default, a
match is tried case-sensitively.
With -rexopt you have slightly finer control over the
regex matching. Setting it to "i" will
make the pattern case-insensitive. Setting it to
"e" allows you to use the Extended
regular expressions as defined by POSIX. Those two values can be bundled
to "-rexopt => 'ie'" should you so
desire.
All arguments except the first ($pattern)
can be given in arbitrary order. Therefore, the following lines are all
equivalent:
locate $pat, -rex => 1, "locatedb", sub { print $_ };
locate $pat, sub { print $_ }, -rex => 1, "locatedb";
locate $pat, "locatedb", sub { print $_ }, -rex => 1;
# etc.
In list context it returns all entries found. In scalar
context, it returns a true or a false value depending on whether any
matching entry has been found. It is a short-cut performance-wise in
that it immediately returns after anything has been found.
If $coderef is provided, the function
never returns anything regardless of context.