Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Perl argument missing

Tags:

perl

I have a method called new. When calling new I'm passing an argument, but when I run the application I get the error that there is no argument or the argument is empty.

StepReader.pm

package StepReader;

use strict;
use warnings;

sub new {   
    # Variable for file path
    my $class = shift;
    my ($filename) = @_;
    if (!defined($filename) or length($filename) == 0) { die 'file path argument is required!'} # check if argument is empty or not!
    # Open my file
    open(my $fh, '<:encoding(UTF-8)', $filename)
        or die "Could not open file '$filename' $!";

    # Print the file!
    while (my $row = <$fh>) {
        chomp $row;
        print "$row\n";
    }

    return bless {}, $class;
}

1;

example.pm

use strict;
use warnings;

use lib 'src/';
use StepReader;

StepReader::new('assets/glasses.STEP');

output

file path argument is required! at src/StepReader.pm line 10.

or

Use of uninitialized value $filename in print at src/StepReader.pm line 10.

I hope someone is able to help me. Thanks in advance!

like image 995
mHvNG Avatar asked Feb 06 '26 03:02

mHvNG


1 Answers

Using :: when invoking the method call does not pass in the class as a parameter. Essentially, your first parameter in new() is the filename, which is being put into the $class variable.

Change:

StepReader::new('assets/glasses.STEP');

...to:

StepReader->new('assets/glasses.STEP');
#         ^^
like image 147
stevieb Avatar answered Feb 09 '26 09:02

stevieb



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!