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

Re: two questions

Thread Previous | Thread Next
From:
Chas. Owens
Date:
August 5, 2009 04:02
Subject:
Re: two questions
Message ID:
58ce48dc0908050401k1c70ab06y5dcc71f3dd372a3b@mail.gmail.com
On Wed, Aug 5, 2009 at 05:12, Ed Avis<eda@waniasset.com> wrote:
> sys adm <sysadm <at> computermail.net> writes:
>
>>1. why perl doesn't have a built-in strip() function?
>>each time I need to say $var =~ s/^\s+|\s+//g to strip
>
> Good question.  Perl 6 is fixing this by adding a 'trim' operator.
> The code I use is
>
>    for ($var) { s/\A\s+//; s/\s+\z/ }
>
>>2. what's the standard module or method to generate a random string, for example
>>the string will be used as part of url.
>
> If you want to use the string in a URL then it cannot be truly random, because
> not every character can appear in a URL.  Why not just generate a random number
> and use that as the string?
snip

One common method is to generate a random number and then encode that
number in base 16 or 64:

my $randstr = sprintf "%08x", int rand 2**32;


-- 
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.

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