CGI/Perl Guide | Learning Center | Forums | Advertise | Login
Site Search: in

  Main Index MAIN
INDEX
Search Posts SEARCH
POSTS
Who's Online WHO'S
ONLINE
Log in LOG
IN

Home: Perl Programming Help: Advanced:
return value from function

 



webhay
Novice

Jan 1, 2003, 10:01 AM

Post #1 of 5 (1664 views)
return value from function Can't Post

i made function that generate random NUmber and letter but i want it to return the random number.


sub gensid {
@alpha=("q","w","e","r","t","y","u","i","o","p","l","k","j","h","g","f","d","s","a","m","n",
"b","v","c","x","z","6","7","8","9","0","5","4","3","2","1","z");

for($i=0; $i<32; $i++) {
$letter = (int(rand $#alpha));
$sid = $alpha[$letter];
}

}

i need to get the value like this:
$sid = &gensid;


thanks
webhay


Jasmine
Administrator / Moderator

Jan 1, 2003, 10:31 AM

Post #2 of 5 (1659 views)
Re: [webhay] return value from function [In reply to] Can't Post

To answer your question....

Code
sub gensid {  
my $sid = '';
my @alpha= qw/ q w e r t y u i o p l k j h g f d s
a m n b v c x z 6 7 8 9 0 5 4 3 2 1 / ;

$sid .= $alpha[ int rand @alpha ] for 1 .. 32;
return $sid;
}

my $sid = gensid();

But I recommend using either [url=http://search.cpan.org/author/SHERZODR/CGI-Session-3.11/]CGI::Session or one of options shown below instead of just using random characters to create session ids (I'm assuming you're creating session ids because of the $sid).

From CGI Programming with Perl:


Code
use Digest::MD5; 

my $md5 = new Digest::MD5;
my $remote = $ENV{REMOTE_ADDR} . $ENV{REMOTE_PORT};
my $id = $md5->md5_base64( time, $$, $remote );
$id =~ tr|+/=|-_.|; # Make non-word characters URL-friendly


or what Apache::Session uses:


Code
use MD5; 

sub generate_id {
return substr(MD5->hexhash(time(). {}. rand(). $$. 'blah'), 0, 16);
}


References:

  • [url=http://perlmonks.org/index.pl?node_id=10102]Re: Simple Session?
  • [url=http://perlmonks.org/index.pl?node_id=36996](Ovid) Re: sessions: MD5 versus random strings


Please note that this question belonged in the Beginners forum -- it is not an Advanced question.


(This post was edited by Jasmine on Jan 1, 2003, 10:31 AM)


Paul
Enthusiast

Jan 2, 2003, 4:04 AM

Post #3 of 5 (1652 views)
Re: [webhay] return value from function [In reply to] Can't Post

If you are going to generate a random string, make it easy for yourself a least :)


Code
sub my_rand { 
my @rand = ('A'..'Z','a'..'z',0..9);
my $high = 32;
my $rand;
$rand .= $rand[rand @rand] for (0..$high);
return $rand;
}



(This post was edited by Paul on Jan 2, 2003, 4:05 AM)


webhay
Novice

Jan 2, 2003, 10:58 AM

Post #4 of 5 (1647 views)
Re: [Jasmine] return value from function [In reply to] Can't Post

thanks your script very cute and effective

webhay

In Reply To


uri
Thaumaturge

Jan 19, 2003, 9:18 PM

Post #5 of 5 (1583 views)
Re: [Jasmine] return value from function [In reply to] Can't Post


In Reply To
To answer your question....

Code
sub gensid {  
my $sid = '';
my @alpha= qw/ q w e r t y u i o p l k j h g f d s
a m n b v c x z 6 7 8 9 0 5 4 3 2 1 / ;

$sid .= $alpha[ int rand @alpha ] for 1 .. 32;
return $sid;
}



Please note that this question belonged in the Beginners forum -- it is not an Advanced question.


well, let's fix that problem by adding some advanced comments Smile

since @alpha is declared and initialized inside the sub but it is readonly, it can be moved to before the sub itself. then it will get initialized only once at load time.

but that means that other subs could either access @alpha or have a name collision with it. so you just wrap both @alpha and the sub in one block:


Code
my @alpha= qw/ q w e r t y u i o p l k j h g f d s 
a m n b v c x z 6 7 8 9 0 5 4 3 2 1 / ;

sub gensid {
my $sid = '';

$sid .= $alpha[rand @alpha ] for 1 .. 32;
return $sid;
}


notice that we also dropped the int as array indexing already provides an integer context.

finally we can be cute and reduce the code to one line:

Code
   return join '', map $alpha[rand @alpha], 1 .. 32;


let perl do more of the work for you. Cool

uri

 
 


Search for (options) Powered by Gossamer Forum v.1.2.0

Web Applications & Managed Hosting Powered by Gossamer Threads
Visit our Mailing List Archives