in reply to Re: creating a class with encapsulation ?
in thread creating a class with encapsulation ?

so i have my code down fine, it compiles. how do i convert it to a class ? any suggestions ? on how to create an object and methods ?
#!/usr/bin/perl use strict; use warnings; my $in = ("/home/ki/Downloads/currenthumanccds.txt"); my $out = ("/home/ki/output.txt"); open (IN,"/home/ki/Downloads/CCDS.current.txt") or die "Cant open file +: $!"; open (OUT,">/home/ki/output.txt") or die "Cant open file: $!"; while(<IN>){ chomp; my@fields = (split/\t/); if ($fields[0] =~ m/16/ && $fields[6] eq"-" && $fields[5] =~ m/Pub +lic/){; print OUT "$fields[0] $fields[2] $fields[6] $fields[5 +]\n"; } }

Replies are listed 'Best First'.
Re^3: creating a class with encapsulation ?
by Athanasius (Archbishop) on Apr 27, 2016 at 04:41 UTC

    Here is the standard approach:

    (1) In file X/CCD/Human.pm (where X is some directory listed in @INC):

    package CCD::Human; use strict; use warnings; sub new { my ($class, $in_filename, $out_filename) = @_; my %self = ( in => $in_filename, out => $out_filename, ); return bless \%self, $class; } sub parse { my ($self) = @_; open(my $in, '<', $self->{in}) or die "Cannot open file '$self->{in}' for reading: $!"; open(my $out, '>', $self->{out}) or die "Cannot open file '$self->{out}' for writing: $!"; while (<$in>) { chomp; my @fields = split /\t/; print $out "$fields[0] $fields[2] $fields[6] $fields[ +5]\n" if ($fields[0] =~ /16/ && $fields[6] eq '-' && $fields[5] =~ /Public/); } close $in or die "Cannot close file '$self->{in}': $!"; close $out or die "Cannot close file '$self->{out}': $!"; }

    (2) Client code would use this class as follows:

    use CCD::Human; ... my $human = CCD::Human->new ( '/home/ki/Downloads/currenthumanccds.txt', '/home/ki/output.txt', ); ... $human->parse();

    Disclaimers:

    1. The above compiles but is otherwise untested.
    2. This doesn’t look like a use-case for which a class is either required or appropriate. Simply putting your code into a subroutine within a module would likely be a better approach.

    Hope that helps,

    Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

      im having trouble running it i tried your client code as a test file nothing happened. no output file was created. and yes i set the class as .pm

        It works correctly for me. A few things to check:

        • Does your module file CCD/Human.pm begin and end like this?

          package CCD::Human; ... 1; # return a true value
        • Does your client code use CCD::Human;?

        • Are the data items on each line of .../home/ki/Downloads/currenthumanccds.txt separated by tab characters?

        • Is the file .../home/ki/output.txt created when you run the script? What does it contain?

        Hope that helps,

        Athanasius <°(((><contra mundum Iustus alius egestas vitae, eros Piratica,

        it isn't supposed to produce any output, also its incomplete, you have to finish it