user218001
user218001

Reputation: 35

issue with creating perl script to imitate linux command "ifconfig"

I have an assignment in which I am to create a Perl script in Linux to imitate the command ifconfig. This command basically shows you information about your network interfaces. I have already created the program, but a few lines are giving me some issues, I would appreciate if anyone could correct the code for me. The errors I am getting says that $get_iface_data requires an explicit package name at line 8, however I do not know how to declare that.

#!/usr/bin/perl
use strict;
use warnings;
use Net::Int::Stats;
use Net::Ifconfig::Wrapper;

my $Iface   = $ARGV[0];
my $rx_packets  = $get_Iface_data->value($Iface, 'rx_packets');
my $Iface_Info = Net::Ifconfig::Wrapper::Ifconfig('list', '', '', '');
print "\tether ". $Iface_Info->{$Iface}{'ether'}."\n";

My assignment basically requires me to get an interface as input, and display the info about that interface, as the ifconfig command would do. I also used two packages, Net::Int::Stats and Net::Ifconfig::Wrapper. The only difference between my script and the ifconfig command is my script will require an interface as parameter

Upvotes: 0

Views: 421

Answers (2)

David W.
David W.

Reputation: 107040

Well, where do you defined $get_Iface_data?

The $foo->bar is a method call syntax. This means that $foo is some sort of object and bar is a method that can be used on that object.

Do you understand Object Oriented Programming and how Perl uses it? Perl has an excellent tutorial to help you get started.

What it comes done to is that you can't use a particular method (think subroutine) except on an object of that class. From this snippet of code, and what your error states, you never defined $get_Iface_data, so you have to define it. In this case, you have to create the object:

my $get_Iface_data = Net::Int::Stats->new();

Now, you can use the various Net::Int::Stats methods on the %get_Iface_data object:

my $rx_packets  = $get_Iface_data->value($Iface, 'rx_packets');

Upvotes: 1

imran
imran

Reputation: 1560

You are just missing the line where you create the Net::Int::Stats object:

#!/usr/bin/perl
use strict;
use warnings;
use Net::Int::Stats;
use Net::Ifconfig::Wrapper;

my $Iface   = $ARGV[0];
my $get_Iface_data = Net::Int::Stats->new();
my $rx_packets  = $get_Iface_data->value($Iface, 'rx_packets');
my $Iface_Info = Net::Ifconfig::Wrapper::Ifconfig('list', '', '', '');
print "\tether ". $Iface_Info->{$Iface}{'ether'}."\n";

Upvotes: 0

Related Questions