Function di Perl

Hampir satu bulan saya tidak meng-update blog dan hari ini kebetulan saya lagi menulis beberapa skrip untuk keperluan kantor.

Beberapa bagian saya sudah lupa karena lama tidak menggunakan skrip tersebut. Skripnya adalah bagaimana menulis fungsi dan memanggil dalam program.


=head1 NAME

Lib::Function.pm - Perl module that will be used to populate all function

=head1 SYNOPSIS

B<use Lib::Function.pm>

=head1 LICENSE

This code is copyright by Askari Azikin <I<askari.azikin@ericsson.com>>,
This program comes with ABSOLUTELY NO WARRANTY.
You are free to redistribute this code under the terms of the GNU
General Public License, version 2 or later.

=head1 AUTHOR

Askari Azikin <askari.azikin@ericsson.com>

Copyright (C) 2017

Revision History:

* Apr 16, 2017 Askari Azikin First Draft

=cut

package Lib::Function;

use strict;
use warnings;

sub new
{
     my $class = shift;
     my $self = {
            outfile => shift,   # to store scalar variable
            result_array => [], # to store array variable
     };
     bless ($self, $class);
     return $self;
}

sub write_to_file_format1
{
    my ($self, $outfile, @result) = @_;
    $self->{outfile} = $outfile if defined($outfile);
    push (@{$self->{result_array}}, @result);

    open (FH, '>', $self->{outfile}) or die "open FH filehandle failed $!";
    for (@{$self->{result_array}})
    {
        print FH $_;
    }
    close(FH);
}

1;

Untuk memanggil fungsi di atas dapat menggunakan skrip berikut.


$call_func->write_to_file_format1($result_file, @result);

 

Leave a Reply

Your email address will not be published. Required fields are marked *