perlperl-module

Couldn't read config file: No such file or directory at ./try.pl line 8


I am not being able to read the contents of the file tutc.txt. I want to write a subroutine to read the contents of a file which will be called from the perl script.

My module is named Module.pm

package Module;

use warnings;
use strict;
use Carp;
use feature "switch";
no warnings 'experimental::smartmatch';

# Constructor and initialisation 
 sub new {                               #class method
    my $class = shift;              #shift without arguments is shift @_ , takes 1st element of argument array
    my $self = {@_};                #created a Hash reference with @_ helping to store values in a hash
    bless ($self, $class);          #turning self into an object by telling which class it belongs to without hardcode name in
    $self->{_created} = 1; #syntax for accessing the contemts of a hash: refrence $object_name->{property_name}.
    return $self;
  }

 #reading from config file
 sub read {
    my ($self, $file) = shift;
    my $self = @_;
    open my $config_fh, $file or return 0;
    $self->{_filename} = $file;     # Store a special property containing the name of the file

    my $section;
    my $config_name;
    my $config_val;

    while (my $line = <$config_fh>)
    {
            chomp $line;
            given ($line) {
                    when (/^\[(.*)\]/)
                            {
                            $section = $1;
                            }
                    when (/^(?<key>[^=]+)=(?<value>.*)/)
                    {
                            $section //= '';
                            $self->{"$section.$config_name"} = $config_val;
                    }
            }
    }
close $config_fh;

return $self;
}

  sub fetch {
    my ($self, $key) = shift;
    return $self->{$key};
 }

My perl file looks like the following:

 #!/usr/bin/perl

 use Module;
 use strict;
 use warnings;

 my $value = Module->new();
 $value->read('/Users/hhansraj/git/edgegrid-curl/tutc.txt') or die "Couldn't        read config file: $!";
 print "The author's first name is ",$value->fetch('author.firstname'),"\n";

My text file looks like the following: [author] firstname=Doug lastname=Sheppard

 [site]
 name=Perl.com
 url=http://www.perl.com/

Solution

  • In your "read" subroutine, it looks like the first two lines of code (listed below) may be the source of your problem.

    my ($self, $file) = shift;
    my $self = @_;
    

    In the first line, you're removing the first element of the @_ array (arguments to the subroutine) and putting that into the $self variable. And nothing is being entered into the $file variable. In the second line, you are redeclaring the $self variable and are assigning to it the size of what's left of the @_ array. I suspect that you're code is assigning the value/data to the $self variable that you are wanting.

    Since the $file variable is not getting assigned any value, that is probably creating an issue with the open function. Also, you did not specify the file mode in your attempt to open the file. To just fix the missing mode specification to specify read only mode, you can change the following line:

    open my $config_fh, $file or return 0;
    

    to be

    open (my $config_fh, "<", $file) or return 0;