App-Info
view release on metacpan or search on metacpan
lib/App/Info.pm view on Meta::CPAN
1;
__END__
=head2 Event Examples
Below I provide some examples demonstrating the use of the event methods.
These are meant to emphasize the contexts in which it's appropriate to use
them.
Let's start with the simplest, first. Let's say that to find the version
number for an application, you need to search a file for the relevant data.
Your App::Info concrete subclass might have a private method that handles this
work, and this method is the appropriate place to use the C<info()> and, if
necessary, C<error()> methods.
sub _find_version {
my $self = shift;
# Try to find the revelant file. We cover this method below.
# Just return if we cant' find it.
lib/App/Info/Util.pm view on Meta::CPAN
$file = $util->first_cat_path(['this.txt', 'that.txt'], @paths);
The first argument to this method may be either a file or directory base name
(that is, a file or directory name without a full path specification), or a
reference to an array of file or directory base names. The remaining arguments
constitute a list of directory paths. C<first_cat_path()> processes each of
these directory paths, concatenates (by the method native to the local
operating system) each of the file or directory base names, and returns the
first one that exists on the file system.
For example, let us say that we were looking for a file called either F<httpd>
or F<apache>, and it could be in any of the following paths:
F</usr/local/bin>, F</usr/bin/>, F</bin>. The method call looks like this:
my $httpd = $util->first_cat_path(['httpd', 'apache'], '/usr/local/bin',
'/usr/bin/', '/bin');
If the OS is a Unix variant, C<first_cat_path()> will then look for the first
file that exists in this order:
=over 4
lib/App/Info/Util.pm view on Meta::CPAN
=head2 search_file
my $file = 'foo.txt';
my $regex = qr/(text\s+to\s+find)/;
my $value = $util->search_file($file, $regex);
Opens C<$file> and executes the C<$regex> regular expression against each line
in the file. Once the line matches and one or more values is returned by the
match, the file is closed and the value or values returned.
For example, say F<foo.txt> contains the line "Version 6.5, patch level 8",
and you need to grab each of the three version parts. All three parts can
be grabbed like this:
my $regex = qr/Version\s+(\d+)\.(\d+),[^\d]*(\d+)/;
my @nums = $util->search_file($file, $regex);
Now C<@nums> will contain the values C<(6, 5, 8)>. Note that in a scalar
context, the above search would yield an array reference:
my $regex = qr/Version\s+(\d+)\.(\d+),[^\d]*(\d+)/;
lib/App/Info/Util.pm view on Meta::CPAN
my @regexen = (qr/(one)/, qr/(two)\s+(three)/);
my @matches = $util->multi_search_file($file, @regexen);
Like C<search_file()>, this method opens C<$file> and parses it for regular
expression matches. This method, however, can take a list of regular
expressions to look for, and will return the values found for all of them.
Regular expressions that match and return multiple values will be returned as
array references, while those that match and return a single value will return
just that single value.
For example, say you are parsing a file with lines like the following:
#define XML_MAJOR_VERSION 1
#define XML_MINOR_VERSION 95
#define XML_MICRO_VERSION 2
You need to get each of these numbers, but calling C<search_file()> for each
of them would be wasteful, as each call to C<search_file()> opens the file and
parses it. With C<multi_search_file()>, on the other hand, the file will be
opened only once, and, once all of the regular expressions have returned
matches, the file will be closed and the matches returned.
lib/App/Info/Util.pm view on Meta::CPAN
my @nums = $file->multi_search_file($file, @regexen);
The result will be that C<@nums> contains C<(1, 95, 2)>. Note that
C<multi_file_search()> tries to do the right thing by only parsing the file
until all of the regular expressions have been matched. Thus, a large file
with the values you need near the top can be parsed very quickly.
As with C<search_file()>, C<multi_search_file()> can take regular expressions
that match multiple values. These will be returned as array references. For
example, say the file you're parsing has files like this:
FooApp Version 4
Subversion 2, Microversion 6
To get all of the version numbers, you can either use three regular
expressions, as in the previous example:
my @regexen = ( qr/FooApp\s+Version\s+(\d+)$/,
qr/Subversion\s+(\d+),/,
qr/Microversion\s+(\d$)$/ );
( run in 2.672 seconds using v1.01-cache-2.11-cpan-d7a12ab2c7f )