Changeset fc92e6e2 for perl/lib


Ignore:
Timestamp:
Aug 20, 2009, 1:29:54 AM (15 years ago)
Author:
Anders Kaseorg <andersk@mit.edu>
Branches:
master, release-1.10, release-1.4, release-1.5, release-1.6, release-1.7, release-1.8, release-1.9
Children:
0b5168d
Parents:
4cbb1b4
git-author:
Anders Kaseorg <andersk@mit.edu> (08/20/09 01:21:30)
git-committer:
Anders Kaseorg <andersk@mit.edu> (08/20/09 01:29:54)
Message:
BarnOwl::quote: accept a list of strings to be quoted individually.

Signed-off-by: Anders Kaseorg <andersk@mit.edu>
File:
1 edited

Legend:

Unmodified
Added
Removed
  • perl/lib/BarnOwl.pm

    rcff58b4 rfc92e6e2  
    290290}
    291291
    292 =head2 quote STRING
    293 
    294 Return a version of STRING fully quoted to survive processing by
    295 BarnOwl's command parser.
     292=head2 quote LIST
     293
     294Quotes each of the strings in LIST and returns a string that will be
     295correctly decoded to LIST by the BarnOwl command parser.  For example:
     296
     297    quote('zwrite', 'andersk', '-m', 'Hello, world!')
     298    # returns "zwrite andersk -m 'Hello, world!'"
    296299
    297300=cut
    298301
    299302sub quote {
    300     my $str = shift;
    301     return "''" if $str eq '';
    302     if ($str !~ /['" ]/) {
    303         return "$str";
     303    my @quoted;
     304    for my $str (@_) {
     305        if ($str eq '') {
     306            push @quoted, "''";
     307        } elsif ($str !~ /['" ]/) {
     308            push @quoted, "$str";
     309        } elsif ($str !~ /'/) {
     310            push @quoted, "'$str'";
     311        } else {
     312            (my $qstr = $str) =~ s/"/"'"'"/g;
     313            push @quoted, '"' . $qstr . '"';
     314        }
    304315    }
    305     if ($str !~ /'/) {
    306         return "'$str'";
    307     }
    308     $str =~ s/"/"'"'"/g;
    309     return '"' . $str . '"';
     316    return join(' ', @quoted);
    310317}
    311318
Note: See TracChangeset for help on using the changeset viewer.