Hi ravi45722,

Ignoring the illicit nature of the example, this code is about as basic an example of a complete object oriented implementation from scratch in Perl as I have ever seen.

I have taken it from the blog post at <link removed because it was an unapproved site or something>.

Edit: Should have also mentioned, don't even think of doing OO from scratch in perl until after you have completely read at least perlootut, perlreftut, and perlref and have a good handle on hashes and references. If you don't have a good handle on hashes, read perldata completely. After that, OO in perl is actually pretty straight forward and nowhere near as nightmarish or hackish as some suggest. Also if you're not going to read the whole thing, at least keep a copy of perlobj around for reference as perlootut mainly only covers doing OO perl using class builders which do the boilerplate of constructors and accessors for you.
#!/usr/bin/perl -w # # plants.pl # A CLI to the medicinal plants database. # (C) 2016 Mischa Capazario v.0.1 Tue Feb 23 17:32:19 SAST 2016 # use lib "Plants/"; use Salvia; use strict; &main(); sub main { if( defined $ARGV[0] ) { if ( $ARGV[0] eq 'add' || $ARGV[0] eq 'update' ) { &addStrain(); exit; } elsif ( $ARGV[0] eq 'show' ) { &showStrain(); exit; } elsif ( $ARGV[0] eq 'remove' ) { &removeStrain(); exit; } else { &showHelp("Valid argument expected"); exit; } } else { &showHelp("Valid argument expected"); exit; } } sub addStrain { my $Salvia = Salvia->new(); my $Strain = $ARGV[1] or &showHelp("Valid argument expected"); $Salvia->{'Name'} = $Strain; $Salvia->{$Strain}->{'THC'} = $ARGV[2] or &showHelp("Valid + argument expected"); $Salvia->{$Strain}->{'CBD'} = $ARGV[3] or &showHelp("Valid + argument expected"); $Salvia->{$Strain}->{'THCV'} = $ARGV[4] or &showHelp("Vali +d argument expected"); $Salvia->{$Strain}->{'Verified'} = $ARGV[5] or &showHelp("Vali +d argument expected"); $Salvia->{$Strain}->{'Description'} = $ARGV[6] || undef; $Salvia->add($Salvia); return; } sub showStrain { my $Salvia = Salvia->new(); my $Strain = $ARGV[1]; # or &showHelp("Valid arguments expected"); if ( defined $Strain ) { print "Strain: $Strain\n"; my $StrainHash = $Salvia->get($Strain); die "Strain not found. You can add it with $0 add" unless $Str +ainHash; foreach my $key (sort keys %{$StrainHash} ) { print " $key: $StrainHash->{$key} "; } print "\n"; } else { my $StrainsHash = $Salvia->get(); print "Strains: \n"; foreach my $strain (sort keys %{$StrainsHash} ) { print " $strain "; } print "\n\nRun $0 show for more details.\n"; } } sub removeStrain { my $Salvia = Salvia->new(); my $Strain = $ARGV[1] or &showHelp("Valid arguments expected"); $Salvia->delete($Strain); } sub showHelp { my $msg = shift ||''; die <<EOH $0: $msg Help: $0 add <STRAIN> <THC Value> <CBD Value> <THCV Value> <Verified (yes/no +)> \t\t - Add new herbs strain, <STRAIN>. Specify cannabi +noid \t\t\t values as numbers between 1 and 5. $0 update <STRAIN> <THC Value> <CBD Value> <THCV Value> <Verified (yes +/no)> \t\t - Update herbs strain, <STRAIN>. $0 show \t - Show list of strains $0 show <STRAIN>\t - Show information on <Strain> $0 remove <STRAIN>\t - Remove <Strain> EOH }
#!/usr/bin/perl -w # # Salvia.pm # Class definition for Salvia object # (C) 2016 Mischa Capazario v.0.1 Tue Feb 23 16:54:06 SAST 2016 # use MLDBM qw(DB_File Storable); use Fcntl; package Salvia; my $dataFile = qq|Plants/Salvia.bdb|; sub new { my $self = shift; return bless {}, $self; } sub add { my $self = shift; my $Strain = shift; return $self->update($Strain); } sub get { my $self = shift; my $Strain = shift; my %Salvia; my $dbm = tie %Salvia, 'MLDBM', $dataFile or die $!; undef $dbm; if( defined $Strain ) { if ( defined $Salvia{$Strain} ) { return \%{$Salvia{$Strain}}; } } else { return \%Salvia; } } sub update { my $self = shift; my $Strain = shift; my %Salvia; my $dbm = tie %Salvia, 'MLDBM', $dataFile or die $!; my $strain = $Strain->{'Name'}; $Salvia{$strain} = $Strain->{$strain}; undef $dbm; untie %Salvia; return %Salvia; } sub delete { my $self = shift; my $Strain = shift; my %Salvia; my $dbm = tie %Salvia, 'MLDBM', $dataFile or die $!; delete($Salvia{$Strain}); undef $dbm; untie %Salvia; return; } 1;

In reply to Re: Learning classes in Perl by zenadu
in thread Learning classes in Perl by ravi45722

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.