Results 1 to 8 of 8
Hello everyone,
I am trying to write a perl program that displays the name of my machine, the version of kernel and Linux I use and some other stuff. I ...
- 01-23-2008 #1Linux User
- Join Date
- Jul 2007
- Location
- Greece
- Posts
- 277
Fiddling around with perl....
Hello everyone,
I am trying to write a perl program that displays the name of my machine, the version of kernel and Linux I use and some other stuff. I have found almost all the commands that print out the information I want but can you please tell me, how can I make the script look up on these information?
Thank you all!!
- 01-23-2008 #2
Hi Goude,
The "commands" you found do the lookup so you don't need to. Other ways (re-inventing the wheel) include reading files in your /proc (pseudo)directory and executing shell commands and parsing their output (bad idea for many reasons, one of which is security).
Since you are having a go with Perl, it is definitely worth finding things out about cpan. Using it you can add modules in Perl (something like adding librarys for C/C++ or jars for Java) that will give you more advanced "commands".
Good luck.
- 01-23-2008 #3
Do you mean "How can my Perl script find this information out on its own?" or "How can my Perl script take advantage of these other commands?"? If the former, it is, as Nautilus says, rather unnecessary, but /proc is a good place to look for all sorts of information about your system. Environment variables are also useful (for instance, $ENV{'HOSTNAME'} gives you your hostname).
If you mean how can you utilize the existing commands, there are several approaches:
The first two are probably preferred methods, since they allow you to parse the text. I don't know that I would consider doing this a "bad idea" (as Nautilus proposes), but it certainly is a bad idea to rely on external commands for critical purposes (at least, without sufficient checking), as these could always be different than you expect them to be.Code:$var = `uname -a`; # Executes "uname -a" and stores the output in $var. You might usually want to use an array here, since most commands will give more than one line of output. open PIPE, "uname -a |"; # Executes "uname -a" and opens PIPE as a pipe to that command. You can read from PIPE just like a regular filehandle. system("uname -a"); # Executes "uname -a" just like it had been done from the shellDISTRO=Arch
Registered Linux User #388732
- 01-23-2008 #4By calling it bad idea I mean that you have to spend more time developing and you need to write extra code in order to validate what goes in the EXECs, i.e if there is user input you have to validate and make sure it doesn't do anything nasty.I don't know that I would consider doing this a "bad idea" (as Nautilus proposes)
In 2 words this kind of approach has these disadvantages:
1. You have to write more lines of code instead of just a one-liner.
2. You have to validate what you pass inside the shell code.
3. Because of 1 and 2 you have more code to manage, so more prone to bugs and more difficult to maintain if we are talking about a bigger project and not just a script.
On the other hand there are more complex requirements that executing shell code inside a Perl program will simplify the whole script. But again every programmer has their own way, especially when they use Perl.
Anyway, I think I'd better stop here cause we are moving far away from the original thread!
- 01-25-2008 #5Linux User
- Join Date
- Jul 2007
- Location
- Greece
- Posts
- 277
Thank you for your replies guys.
You did confuse me
a bit. All I am trying to do is, as cabhan put it is to make my script find this information out on its own.
The only thing I managed to write so far is a script that displays the hostname.
Here it is:
use Sys::Hostname;
print hostname(), "\n";
I think system("uname -a");, is what I needed.
Thank you cabhan.
Eyxaristw Nautilus!!
- 01-25-2008 #6
If we did confuse you a bit and not A LOT, then that's good!

I think both cabhan and myself are leading you to the same direction: There is no right or wrong way, especially when you want to experiment (I guess this is what you want to do, right?)
Anyway, you are very welcome! Parakalw!
- 01-25-2008 #7Linux User
- Join Date
- Jul 2007
- Location
- Greece
- Posts
- 277
To be honest, you confused me a lot...

And that's because of you nautilus
(just kidding!!)
I was wondering if you can help me with one more thing.
If I type:
print "Kernel Version:\n";
system("uname -r");
I get
Kernel Version:
2.6.18-1.2798.fc6
Can you please tell me how can I make the code display the information in one sentence?
Kernel Version: 2.6.18-1.2798.fc6
Thank you very much!!!!
- 01-25-2008 #8Same as your code but remove the '\n'. The '\n' means "new line" which is exactly what happens when you press the Enter key.Code:
print "Kernel Version: "; system("uname -r");
I hope you are less confused now!


Reply With Quote