develooper Front page | perl.beginners | Postings from August 2009

Re: two questions

Thread Previous | Thread Next
From:
John W. Krahn
Date:
August 4, 2009 23:25
Subject:
Re: two questions
Message ID:
4A7925B8.4050007@shaw.ca
sys adm wrote:
> 
> 1. why perl doesn't have a built-in strip() function?

Why doesn't BASIC have built-in regular expressions?  Why doesn't C have 
built-in strings?  Why doesn't $LANGUAGE have built-in $FEATURE? 
Because that is the way the language was designed.


> each time I need
> to say $var =~ s/^\s+|\s+//g to strip the blank space before and after
> the variable, specially if this is a CGI var.

That actually strips *all* whitespace from the variable:

$ perl -le'my $var = " one  two  three "; print "*$var*"; $var =~ 
s/^\s+|\s+//g; print "*$var*"'
* one   two  three *
*onetwothree*

According to the FAQ you want to do it like this:

s/^\s+//, s/\s+$// for $var;


> 2. what's the standard module or method to generate a random string,
> for example the string will be used as part of url.

AFAIK there is no standard module but you could do something like this:

my @chars = ( 'a' .. 'z', 'A' .. 'Z', 0 .. 9 );
my $string_length = 30;

my $random_string = join '', map $chars[ rand @chars ], 1 .. $string_length;




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

Thread Previous | Thread Next


nntp.perl.org: Perl Programming lists via nntp and http.
Comments to Ask Bjørn Hansen at ask@perl.org | Group listing | About