molv has asked for the wisdom of the Perl Monks concerning the following question:

Hello monks, I've been lurking around here and finally decided to create an account to post from. I've written this script (my first script) to ftp to a server and use a config file for login credentials.

use strict; use warnings; use Net::FTP; use YAML; use Data::Dumper; sub main { my $host; my $username; my $password; # step 1: open file open my $fh, '<', 'config.yml' or die "can't open config file: $!"; # step 2: convert YAML file to perl hash ref my $config = LoadFile($fh); print Dumper($config), "\n"; my $ftp = Net::FTP->new($host) or die "Can't open $host\n"; $ftp->login($username, $password) or die "Cant log $username in\n"; my $dir = "/hd1/Logs"; $ftp->cwd($dir) or die "Can't cwd to #dir\n"; my $file_to_get = "filetoget"; $ftp->get($file_to_get) or die "Can't get $file_to_get from $dir\n"; } main();

I am getting this error "Undefined subroutine &main::LoadFile called at test.pl line 18." and have absolutely no idea why. Wisdom much appreciated.

Replies are listed 'Best First'.
Re: undefined subroutine
by pryrt (Abbot) on Apr 05, 2016 at 17:13 UTC

    YAML::LoadFile is exportable, but you didn't export it.

    use YAML qw(LoadFile);

    or fully qualify the call:

    my $config = YAML::LoadFile($fh);

      Thanks for replying with assistance. I've attempted both of these changes and am still receiving the same error. I must be doing something wrong, could you explain further if you don't mind?

        Sure. I don't know what you've done to your script, but here's one which runs to completion and gives no such error message.

        #!/usr/bin/env perl use strict; use warnings; use YAML qw/LoadFile/; open my $fh, '<', '/dev/null'; my $config = LoadFile ($fh);

        Examine this code and compare it to your code. Start altering this code one line at a time to make it ever closer to your code, running it after each addition. If you hit a problem you will then know precisely which change caused the problem.

        This code works fine for me.

        use strict; use warnings; use YAML qw(LoadFile); use Data::Dumper; # step 1: open file open my $fh, '<', 'config.yml' or die "can't open config file: $!"; # step 2: convert YAML file to perl hash ref my $config = LoadFile($fh); print Dumper($config), "\n";

        Although I have my doubts this is the reason, you could try upgrading your YAML.

        Note that YAML's own documentation recommends switching to YAML::XS.

        Are you really getting this error?

        "Undefined subroutine &main::LoadFule called at test.pl line 18."