wrote in news:1141246133.873937.255500
@t39g2000cwt.googlegroups.com:
> Subject: newbie question
Please read the posting guidelines for this group.
> What does the following do?
>
> ($input) = $input =~ /^\s*(.+)\s*$/;
It matches $input in list context and stores the captured match in
$input.
The purpose seems to be to trim leading and trailing spaces, but it
fails because the capture group matches any character. See:
D:\Home\asu1\UseNet\clpmisc> cat r.pl
#!/usr/bin/perl
use warnings;
use strict;
my $s = "\t \t \n test \n\n\n";
($s) = $s =~ m{ \A \s* (.+) \s* \z}x;
print "-$s-\n";
__END__
D:\Home\asu1\UseNet\clpmisc> r
-test -
Compare that to:
D:\Home\asu1\UseNet\clpmisc> cat r.pl
#!/usr/bin/perl
use warnings;
use strict;
my $s = "\t \t \n test \n\n\n";
($s) = $s =~ m{ \A \s* (\S+) \s* \z}x;
print "-$s-\n";
__END__
D:\Home\asu1\UseNet\clpmisc> r
-test-
Note that this will not work if $s is
$s = ""\t \t \n test me \n\n\n";
A better way to trim leading and trailing spaces in this case would be:
$s =~ s{\A\s+}{};
$s =~ s{\s+\z}{};
Sinan
--
A. Sinan Unur <>
(reverse each component and remove .invalid for email address)
comp.lang.perl.misc guidelines on the WWW:
http://mail.augustmail.com/~tadmc/cl...uidelines.html