0

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

RobEarl
  • 7,862
  • 6
  • 35
  • 50
user218001
  • 35
  • 1
  • 8
  • First of all you should tell us what error message you get. You do not even use `$get_Iface_data` and I guess you can not call any method on it. Strings are no objects in Perl but a string is what you get from `STDIN`. – matthias krull Apr 17 '13 at 20:00
  • I have added the error i got, and corrected my code – user218001 Apr 17 '13 at 20:10

2 Answers2

1

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');
David W.
  • 105,218
  • 39
  • 216
  • 337
  • Use of uninitialized value $int in regexp compilation at /usr/local/share/perl/5.14.2/Net/Int/Stats.pm line 113. Use of uninitialized value $int in hash element at /usr/local/share/perl/5.14.2/Net/Int/Stats.pm line 157. Use of uninitialized value $Iface in hash element at ./test2.pl line 11. Use of uninitialized value in concatenation (.) or string at ./test2.pl line 11. ether – user218001 Apr 17 '13 at 22:23
  • @user218001 when you ran the script, did you pass an interface in the command line, i.e. `perl test2.pl eth0`? – imran Apr 18 '13 at 02:55
  • @user218001 - You got _Use of uninitialized value $Iface in hash element at ./test2.pl line 11_. Where is `$Iface` getting it's value? See that `my $Iface = $ARGV[0]`? Are you passing the interface name into the program when you run it? As imran already commented? – David W. Apr 18 '13 at 14:24
0

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";
imran
  • 1,560
  • 10
  • 8