Solved

Small OOP program in perl

Posted on 2004-08-24
3
357 Views
Last Modified: 2012-05-05
Hi,


I need a small example to store userid and password in a flat file.
I would like to do as...
I would like to create a module, in that i have two functions.
1. for creating user id
2. for creating password

for creating userid, i will pass two inputs (ie, unique_number, userid)
for creaing password, i will pass two inputs(ie, unique_number,password).
userid function will store the unique number and userid in a flat file.
password function will store the password in the flat file .

In my main program i am passing these values to module.
Can you please help me to write a sample program for that?


Thanks and Regards,
S
0
Comment
Question by:sureshp
3 Comments
 
LVL 12

Expert Comment

by:geotiger
ID: 11884698
The following is just a quick shot. It did not check user or password existance and did encrpt password neither. It just demonstrate how you could construct your module and how to use it. Hope this is helpful.

GT

# -- your main program
package main;
use Sureshp::Users;

my $obj = bless {}, "main";

$obj->create_userid(1, 'test_user1');
$obj->create_password(1,'secret');

exit;


# --- your package file under a folder in your @INC

package Sureshp::Users;     # you can use an module name that you see fit

use strict;
use vars qw(@ISA $VERSION @EXPORT @EXPORT_OK);
use Carp;

$VERSION = 0.01;
@ISA = qw(Exporter);
@EXPORT = qw(create_userid create_password);
@EXPORT_OK = qw(
    create_userid create_password
    );

sub create_userid {
    my $s = shift;
    my ($n, $id, $fn) = @_;

     $fn = '/my/user/id/file.txt' if ! $fn;
    open FN, ">>$fn" or die "Could not write to file: $!\n";
    print FN "$n,$id\n"
    close FN;
    return;
}

sub create_password {
    my $s = shift;
    my ($n, $pwd, $fn) = @_;

     $fn = '/my/user/id/password.txt' if ! $fn;
    open FN, ">>$fn" or die "Could not write to file: $!\n";
    print FN "$n,$pwd\n"
    close FN;
    return;
}
0
 
LVL 1

Accepted Solution

by:
boazgOLD earned 200 total points
ID: 11885049
looking at what geotiger wrote, i feel i may have misunderstood you and created something more complex then you asked for, making it o you can change things, and use a diferent file and stuff, but...

main program:

use userFF

my $ff=new userFF ('flatfile.txt',0);
$ff->setuid(123,456);
$ff->setpwd(123,"foo");
$ff->setuid(12,4526);
$ff->setpwd(13,"bar");
$ff->setuid(13,4564);
$ff->setpwd(12,"moo");
$ff->setuid(1231,4576);
$ff->setpwd(1231,"cow");


userFF.pm:

package userFF;

sub readfile { #read function
      my($this)=pop;
      open(in,$this->{flatfile}) or return; #open file or return if file does not exist
            $this->{hash}={}; #fresh blank hash
      while (<in>) { #read
            chomp; #remove newline
            my($unum,$uid,$pwd)=split /,/; #split line, store uneque num in $unum, uid in $uid, password in $pwd
            $this->{hash}->{$unum}=[$uid,$pwd]; #store in hash
      }
      close(in);
}

sub writefile
{
      my($this)=pop;
      open(out,">".$this->{flatfile}); #open file
      foreach $key (keys (%{$this->{hash}}))#loop thru hash
      {
            print out "$key,". $this->{hash}->{$key}->[0] .','. $this->{hash}->{$key}->[1]."\n"; #print
      }
      close (out);


}


sub new
{
      my($package,$file,$volatile)=@_;
      my $this={};
      bless $this, $package; #bless
      $this->{hash}={};#create hash for storage in case file does not exist
      $this->{flatfile}=$file || "flatfile.txt"; #set filename
      $this->{volatile}=$volatile; #set volitile
      $this->readfile();
      return $this;
}

sub setuid
{
      my ($this,$unum,$uid) = @_;
      if($this->{volatile}) #check stability
      {
            $this->readfile;
      }
      $this->{hash}->{$unum}->[0]=$uid; #set
      if($this->{volatile}) #check stability
      {

            $this->writefile;
      }
}

sub setpwd
{
      my ($this,$unum,$pwd) = @_;
      if($this->{volatile}) #check stability
      {
            $this->readfile;
      }
      $this->{hash}->{$unum}->[1]=$pwd; #set
      if($this->{volatile}) #check stability
      {
            $this->writefile;
      }
}

sub DESTROY
{
      my ($this)=shift;
$this->writefile(); #on exit write all data
}
1;

0

Featured Post

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

On Microsoft Windows, if  when you click or type the name of a .pl file, you get an error "is not recognized as an internal or external command, operable program or batch file", then this means you do not have the .pl file extension associated with …
In the distant past (last year) I hacked together a little toy that would allow a couple of Manager types to query, preview, and extract data from a number of MongoDB instances, to their tool of choice: Excel (http://dilbert.com/strips/comic/2007-08…
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
Both in life and business – not all partnerships are created equal. As the demand for cloud services increases, so do the number of self-proclaimed cloud partners. Asking the right questions up front in the partnership, will enable both parties …

863 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

20 Experts available now in Live!

Get 1:1 Help Now