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

Hi,
I’m running the following program:
#! /usr/local/bin/perl BEGIN { push ( @INC, "$ENV{HOME}/Filesys-DiskSpace-0.05/lib"); # Filesys:: +DiskSpace - Perl df } use strict; use warnings; require Filesys::DiskSpace; # file system /home or /dev/sda5 my $dir = "/home"; # get data for /home fs my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = Filesys::Dis +kSpace::df $dir; # calculate free space in % my $df_free = (($avail) / ($avail+$used)) * 100.0; # display message my $out = sprintf("Disk space on $dir == %0.2f\n",$df_free); print $out;
And Failed with the following error:
Can't call method "Filesys::DiskSpace::df" without a package or object reference at ./try.pl line 27.
Please advice.
Thanks.

Replies are listed 'Best First'.
Re: import external package
by JavaFan (Canon) on Jan 22, 2009 at 19:40 UTC
    You choosed to do the require at run time. Which means that at compile time, Perl considers:
    Filesys::DiskSpace::df $dir;
    to be an indirect object reference.

    One solution is use 'use' instead of require. Another is use explicite parenthesis when calling Filesys::DiskSpace::df.

    And if you use "use lib", you don't need the BEGIN block, and don't have to push on @INC yourself.

Re: import external package
by kyle (Abbot) on Jan 22, 2009 at 19:44 UTC

    I'm guessing this will work if you add the parentheses on your df call.

    my ($fs_type, $fs_desc, $used, $avail, $fused, $favail) = Filesys::Dis +kSpace::df( $dir );

    Perl seems to think you're trying to use an indirect method call (like "new Data::Dumper" instead of "Data::Dumper->new").

    Since you modify @INC in a BEGIN block, I think you can just use Filesys::DiskSpace instead of using require. If you still want to use require for some reason, you'll have to separately import its df to use it without parens.

Re: import external package
by Anonymous Monk on Jan 23, 2009 at 13:19 UTC