Jyoti wrote:
Can someone explain me what these symbols mean in regular expression:

my $trim = sub {local($_)=shift;
     $$_ =~ s/^\s*//;

^ means that you want to anchor the pattern at the beginning of the string in $$_. \s is a character class that matches the whitespace characters " ", "\t", "\f", "\r" and "\n". * is a pattern modifier, it says that the previous expression (the \s character class) can appear any number of times in the string, including zero times.

     $$_ =~ s/\s*$//;};

$ means that you want to anchor the pattern at the end of the string in $$_.

The use of the * modifier is inefficient as it means that the string is always modified because every string has zero whitespace at the beginning and end. It would be better to use the + modifier which would only modify the string if there is actually whitespace at the beginning or end.

Also, the use of a reference to the variable stored in $_[0] and the use of $_ locally in a subroutine seems like a lot of misdirection when the @_ array is already aliased to the variables passed to the subroutine so that would probably be better written as:

my $trim = sub {
    $_[ 0 ] =~ s/^\s+//;
    $_[ 0 ] =~ s/\s+$//;
    };



John
--
Those people who think they know everything are a great
annoyance to those of us who do.        -- Isaac Asimov

--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Reply via email to