Author Topic: cpuinfo in perl - need help  (Read 1326 times)

Offline slax

  • Sr. Member
  • ****
  • Posts: 391
    • PCLinuxOS Gnome Edition
cpuinfo in perl - need help
« on: October 28, 2010, 12:24:20 PM »
hi guys,
i was wondering could someone of you perl programmers help me with a script.

I am trying to add the subroutine that reeds cpu model(s) and ram total.

I tried with this:
Code: [Select]
#cpu info subroutine
sub get_cpu {
open(CPU, 'grep "model name" /proc/cpuinfo');
print $cpu;
$cpu = <CPU>;
return $cpu;
  close(CPU);
}

that was supposed to run a command in cmdline and read an output as string.
But unfortunately it doesn't work :(

If someone could help me it would be great.

here's the full script:
Code: [Select]
#!/usr/bin/perl -w
# basic information on your current pclos gnome release

use Gtk2 -init;
use strict;

sub get_kernelver;
sub get_gnomever;
sub get_ram;
sub get_cpu;

my $quit = sub { Gtk2->main_quit };
my $window = Gtk2::Window->new;

$window->signal_connect('delete_event', $quit);
$window->set_position('center');
$window->set_border_width(8);
$window->set_title('About');
$window->set_default_size(250, 200);
$window->set(allow_grow => 0);
#get kernel version
my $kernelver = get_kernelver();
#get gnome version
my $gnomever = get_gnomever();
my $ram = get_ram();
my $cpu = get_cpu();

my $image = Gtk2::Image->new_from_file('/usr/share/pclos-gnome-info/about.png');
my $label = Gtk2::Label->new;
my $button = Gtk2::Button->new_from_stock('gtk-quit');
my $markup = <<"END";
          <b>       PCLinuxOS                </b>
<span foreground="red">Gnome Version</span>

Release: <span foreground="darkgreen">2010.10</span>
Build: <span foreground="darkgreen">2010/10/26</span>
Kernel: <span foreground="darkgreen">$kernelver</span>
Gnome: <span foreground="darkgreen">$gnomever</span>

$ram
$cpu
END
$label->set_justify('center');
$label->set_markup($markup);
$button->signal_connect('clicked', $quit);

my $box = Gtk2::VBox->new;
$box->set_spacing(8);
$box->pack_start($image,  0, 0, 0);
$box->pack_start($label,  0, 0, 0);
$box->pack_start($button, 0, 0, 0);

$window->add($box);
$window->show_all;

#kernel version subroutine
sub get_kernelver {
my $kernelver = qx(uname -r);
chomp $kernelver;
return $kernelver;
}

#gnome version subroutine
sub get_gnomever {
my $gnomever = qx(rpm -q --qf "%{VERSION}\n" gnome-desktop);
chomp $gnomever;
return $gnomever;
}

#ram info subroutine
sub get_ram {
open(RAM, "grep MemTotal /proc/cpuinfo");
$ram = <RAM>;
return $ram;
close(RAM);
}

#cpu info subroutine
sub get_cpu {
open(CPU, 'grep "model name" /proc/cpuinfo');
print $cpu;
$cpu = <CPU>;
return $cpu;
  close(CPU);
}

Gtk2->main;

exit;



Offline aherkey

  • Full Member
  • ***
  • Posts: 109
Re: cpuinfo in perl - need help
« Reply #1 on: October 28, 2010, 01:19:56 PM »
You are trying to run a system command with a file open statement.
To keep this pure perl you need to open the file /proc/cpuinfo and parse it for the mode name.

Try this:
Code: [Select]
sub get_cpu {
   open(CPU,"</proc/cpuinfo");
   while (<CPU>) {
      chomp;
      if( /^model name.*: (.*$)/) {
         close(CPU);
         return $1;
      }
   }
   close(CPU);
}

This will loop through each line and look for "^model name.*: ".
If it is found the it will put the rest of the line in $1.

You will need to fix your other open statements.

- Andy

Offline slax

  • Sr. Member
  • ****
  • Posts: 391
    • PCLinuxOS Gnome Edition
Re: cpuinfo in perl - need help
« Reply #2 on: October 28, 2010, 02:15:23 PM »
Thanks mate, that solved my problem :)
i fixed the script.

I found on the net that you can use open to read cmd output, but looks like it can't...

Thanks again :)



Offline slax

  • Sr. Member
  • ****
  • Posts: 391
    • PCLinuxOS Gnome Edition
Re: cpuinfo in perl - need help
« Reply #3 on: October 29, 2010, 12:32:18 PM »
Thank you both for the info :)