For Programmers: Free Programming Magazines  


Home > Archive > PERL Miscellaneous > March 2004 > Perl: Generate registration code









You are viewing an archived Text-only version of the thread. To view this thread in it's original format and/or if you want to reply to this thread please [click here]

 

Author Perl: Generate registration code
John

2004-03-27, 12:01 am

I am new to Perl. I want to write a Perl script that generates a
registration code on my website. It's basically a hex of the w of
the year and year, plus and multiplied by some number. I have a
sample of what I would like in VB. Can someone help convert this to
Perl. I'm hoping it's as simple as VB. Thanks for your help...

------- VB Code ------------
Sub RegCode()
Dim strCode as String
strCode = Hex(((DatePart("ww", Date) & Year(Date)) + 12345) * 12345)
Debug.Print strCode
End Sub
---------------------------

John
Richard Morse

2004-03-27, 12:01 am

In article <c2c15952.0403261030.bb12901@posting.google.com>,
john_1998@hotmail.com (John) wrote:

> I am new to Perl. I want to write a Perl script that generates a
> registration code on my website. It's basically a hex of the w of
> the year and year, plus and multiplied by some number. I have a
> sample of what I would like in VB. Can someone help convert this to
> Perl. I'm hoping it's as simple as VB. Thanks for your help...
>
> ------- VB Code ------------
> Sub RegCode()
> Dim strCode as String
> strCode = Hex(((DatePart("ww", Date) & Year(Date)) + 12345) * 12345)
> Debug.Print strCode
> End Sub
> ---------------------------


To get the date, look at the 'localtime' function. It doesn't supply a
w of the year, so if it absolutely must be based off of the w, you
might need to look at various date modules from CPAN (http://search.cpan.org).

When you say "hex of the ...", presumably you mean a hex string
representing the number that you generate. The sprintf function will do
this:
perl -e "print sprintf('%x', 24), qq{\n}"
returns:
18

HTH,
Ricky
Martien Verbruggen

2004-03-27, 12:01 am

On 26 Mar 2004 10:30:13 -0800,
John <john_1998@hotmail.com> wrote:
>
>
> I am new to Perl. I want to write a Perl script that generates a
> registration code on my website. It's basically a hex of the w of
> the year and year, plus and multiplied by some number. I have a
> sample of what I would like in VB. Can someone help convert this to
> Perl. I'm hoping it's as simple as VB. Thanks for your help...


The first thing you need to do is define what you (or VB) means by "the
w of the year", since there are various valid definitions out there
that say when the first w of the year starts.

> strCode = Hex(((DatePart("ww", Date) & Year(Date)) + 12345) * 12345)


To "hex" (awful expression) something in Perl, you can use the sprintf
function. Documentation in perlfunc. You can get the current year with
localtime(), again documented in perlfunc.

To get better help, you should probably also explain what the & operator
does in VB, just to help the people out who have ever been able to bring
themselves to touch that environment.

Martien
--
|
Martien Verbruggen | prepBut nI vrbLike adjHungarian! qWhat's
| artThe adjBig nProblem? -- Alec Flett
|

Andrew Rodland

2004-03-27, 12:01 am

John wrote:

> I am new to Perl. I want to write a Perl script that generates a
> registration code on my website. It's basically a hex of the w of
> the year and year, plus and multiplied by some number. I have a
> sample of what I would like in VB. Can someone help convert this to
> Perl. I'm hoping it's as simple as VB. Thanks for your help...
>
> ------- VB Code ------------
> Sub RegCode()
> Dim strCode as String
> strCode = Hex(((DatePart("ww", Date) & Year(Date)) + 12345) * 12345)
> Debug.Print strCode
> End Sub
> ---------------------------


use Date::Format;

sub regcode {
print sprintf('%x', timetostr('%W%Y', time));
}

or so.

Sponsored Links







Also available: Server administration forum archive | Web Design forum archive | Software forum archive | Hardware reviews archive

Copyright 2008 codecomments.com