1 # Copyright (c) 2002-2009, Mikhael Goikhman
3 # This program is free software; you can redistribute it and/or modify
4 # it under the terms of the GNU General Public License as published by
5 # the Free Software Foundation; either version 2 of the License, or
6 # (at your option) any later version.
8 # This program is distributed in the hope that it will be useful,
9 # but WITHOUT ANY WARRANTY; without even the implied warranty of
10 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 # GNU General Public License for more details.
13 # You should have received a copy of the GNU General Public License
14 # along with this program; if not, write to the Free Software
15 # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
17 package General
::Parse
;
20 use vars
qw(@ISA @EXPORT);
24 get_token cut_token get_tokens cut_tokens eqi nei
27 # currently backslashes are ignored and this is a bit buggy
34 $quote = substr($line, 0, 1) if $line =~ /^["'`]/;
37 if ($line =~ /^(.*?)$quote\s*(.*)$/) {
40 } elsif ($line eq "") {
47 return wantarray ?
($token, $line) : $token;
50 # returns the next quoted token, modifies the parameter (ref to line)
53 my ($token, $rest) = get_token
($$line_ref);
63 $$line_ref =~ s/^\s+//;
64 while ($$line_ref ne "" && $limit-- > 0) {
65 push @tokens, cut_token
($line_ref);
67 return wantarray ?
@tokens : \
@tokens;
70 sub get_tokens
($;$) {
72 my $limit = shift || 1000;
74 return cut_tokens
(\
$line, $limit);
79 return lc($a) eq lc($b);
84 return lc($a) ne lc($b);
87 # ----------------------------------------------------------------------------
91 General::Parse - parsing functions
97 my $string = q{Some "not very long" string of 6 tokens.};
98 my $token1 = cut_token(\$string); # $token1 = "Some";
99 my ($token2, $token3) = cut_tokens(\$string, 2);
100 my @subtokens = get_tokens($token2); # ("not", "very", "long")
101 my $subtoken1 = get_token($token2); # the same as $subtokens[0]
102 my $ending_array_ref = get_tokens($string); # ["of", "6", "tokens."]
106 This package may be used for parsing a string into tokens (quoted words).
117 Returns the first token of the given string without changing the string itself.
119 If the string is empty or consists of only spaces the returned token is undef.
121 If the caller expects a scalar - the token is returned. If it expects an
122 array - 2 values returned, the token and the rest of the string.
126 String (scalar) to be parsed.
131 Or, in array context, array of 2 scalars: token and the rest of string.
142 Returns the first token of the given string, the input string is cut to
143 contain tokens starting from the second one.
145 If the string is empty or consists of only spaces the returned token is undef
146 and the string is changed to an empty string.
150 String (scalar) to be parsed.
165 Returns all or the requested number of tokens of the given string without
166 changing the string itself.
168 The returned array may contain less tokens than requested if the string
169 is too short. Particularly, the returned array is empty if the string is empty
170 or consists of only spaces.
172 If the caller expects a scalar - a reference to the token array is returned.
173 If it expects an array - the token array is returned.
177 String (scalar) to be parsed, and optional limit (integer) for number of
182 Tokens (array of scalars or array ref of scalars depending on context).
193 Returns the requested number of tokens of the given string, the string is cut
194 to contain the tokens starting from the first non returned token.
196 The returned array may contain less tokens than requested if the string
197 is too short. Particularly, the returned array is empty if the string is empty
198 or consists of only spaces.
200 If the caller expects a scalar - a reference to the token array is returned.
201 If it expects an array - the token array is returned.
205 String (scalar) to be parsed, and limit (integer) for number of
210 Tokens (array of scalars or array ref of scalars depending on context).
221 Similar to B<eq>, but case-insensitive, gets 2 strings, returns boolean.
232 Similar to B<ne>, but case-insensitive, gets 2 strings, returns boolean.
239 Mikhael Goikhman <migo@homemail.com>
242 # ============================================================================