@_ deprecated?

B

broeisito

Hello,

I wrote the following code to read what processor is in a linux box.
But I get the following error:

Use of implicit split to @_ is deprecated at cpu.pl line 17.
Can someone possibly explain to me what I'm doing wrong here?

Here is the code.

================== begin ======================
#!/usr/bin/perl

use strict;
use warnings;

my $CPU_FILE = "/proc/cpuinfo";

if(-e $CPU_FILE && -r $CPU_FILE && -f $CPU_FILE)
{
open CPU, $CPU_FILE;
}

while(<CPU>)
{
if(/model.name/)
{
split;
print "The processor is an $_[3] $_[4] $_[5] running at $_[7]\n";
}
}

close CPU;

================== end ======================

BTW... this is my first perl code... so don;t flame my programming
style yet!!

Cheers,

Broeisi
 
X

xhoster

Hello,

I wrote the following code to read what processor is in a linux box.
But I get the following error:

Use of implicit split to @_ is deprecated at cpu.pl line 17.
Can someone possibly explain to me what I'm doing wrong here?

Yes, you are not using the documentation properly.

from perldoc perldiag:

Use of implicit split to @_ is deprecated
(D deprecated) It makes a lot of work for the compiler
when you clobber a subroutine's argument list, so it's
better if you assign the results of a split() explic-
itly to an array (or list).

Cheers,

Xho
 
J

John Bokma


Don't scare us with a wrong subject :)
I wrote the following code to read what processor is in a linux box.
But I get the following error:

Use of implicit split to @_ is deprecated at cpu.pl line 17.

So the implicit split to @_ is deprecated.
^^^^^^^^^^^^^^

Can someone possibly explain to me what I'm doing wrong here?

Here is the code.

================== begin ======================
#!/usr/bin/perl

use strict;
use warnings;

my $CPU_FILE = "/proc/cpuinfo";

if(-e $CPU_FILE && -r $CPU_FILE && -f $CPU_FILE)
{
open CPU, $CPU_FILE;
}

You *don't* check what open returns. Moreover, de rest of your code
assumes it always works.

replace the if {} with:

-f $CPU_FILE or die "'$CPU_FILE' is not a regular file";
open my $fh, $CPU_FILE or die "Can't open '$CPU_FILE' for reading: $!";
while(<CPU>)
{
if(/model.name/)
{
split;

The problem is here, you split to @_, which is deprecated, see:
perldoc -f split
print "The processor is an $_[3] $_[4] $_[5] running at
$_[7]\n";
}
}

I would do the while loop like:

while ( my $line = <$fh> ) {

$line =~ /model\.name/ or next;

my @data = split /\s+/, $line; # not sure about the pattern
print "The processor .. $data[ 0 ] $data[ 1 ] ...\n";
}

close $fh or die "Can't close '$CPU_FILE' after reading: $!";
 
B

broeisito

Guys,

Thanks a lot for your answers.
I will make better use of the documentation.

I'm just new at perl.

I will make the changes to my script.

Thanks a lot.

Cheers,

Broeisi

John said:

Don't scare us with a wrong subject :)
I wrote the following code to read what processor is in a linux box.
But I get the following error:

Use of implicit split to @_ is deprecated at cpu.pl line 17.

So the implicit split to @_ is deprecated.
^^^^^^^^^^^^^^

Can someone possibly explain to me what I'm doing wrong here?

Here is the code.

================== begin ======================
#!/usr/bin/perl

use strict;
use warnings;

my $CPU_FILE = "/proc/cpuinfo";

if(-e $CPU_FILE && -r $CPU_FILE && -f $CPU_FILE)
{
open CPU, $CPU_FILE;
}

You *don't* check what open returns. Moreover, de rest of your code
assumes it always works.

replace the if {} with:

-f $CPU_FILE or die "'$CPU_FILE' is not a regular file";
open my $fh, $CPU_FILE or die "Can't open '$CPU_FILE' for reading: $!";
while(<CPU>)
{
if(/model.name/)
{
split;

The problem is here, you split to @_, which is deprecated, see:
perldoc -f split
print "The processor is an $_[3] $_[4] $_[5] running at
$_[7]\n";
}
}

I would do the while loop like:

while ( my $line = <$fh> ) {

$line =~ /model\.name/ or next;

my @data = split /\s+/, $line; # not sure about the pattern
print "The processor .. $data[ 0 ] $data[ 1 ] ...\n";
}

close $fh or die "Can't close '$CPU_FILE' after reading: $!";
 
B

Ben Morrow

Quoth John Bokma said:
You *don't* check what open returns. Moreover, de rest of your code
assumes it always works.

replace the if {} with:

-f $CPU_FILE or die "'$CPU_FILE' is not a regular file";
open my $fh, $CPU_FILE or die "Can't open '$CPU_FILE' for reading: $!";

It's better in general to open the file first and then filetest the
filehandle. Then you can be *sure* of what you've got.

In this case, of course, it doesn't matter: all you need is something
you can read. If Linux 3.18 should replace /proc/cpuinfo with a named
pipe, or some other type of magic file, your script should continue to
work. So skip the filetest altogether.

Ben
 
J

Justin C

Hello,

I wrote the following code to read what processor is in a linux box.
But I get the following error:

Use of implicit split to @_ is deprecated at cpu.pl line 17.
Can someone possibly explain to me what I'm doing wrong here?

You're not doing anything wrong, it'll work, but in some situations
using split like this will break your code. From perldoc -f split:

In scalar context, returns the number of fields found and
splits into the @_ array. Use of split in scalar context is
deprecated, however, because it clobbers your subroutine arguâ€
ments.

You may want to read "perldoc perlsub" in conjunction with the above
if it's not immediately clear to you why you are getting the warning.

Justin.
 
J

John Bokma

Guys,

Thanks a lot for your answers.

When replying, write your reply *under* the part you're replying to and
remove everything else that is no longer needed in such a way that the
message you are posting can be read stand alone without too much effort
(top down, left to right, and it's clear who wrote what).
 
J

John Bokma

Ben Morrow said:
It's better in general to open the file first and then filetest the
filehandle. Then you can be *sure* of what you've got.

Thanks, you're right, my order has the risk of a race condition. (I guess
that's why you suggest to swap them).
In this case, of course, it doesn't matter: all you need is something
you can read.

Yeah, I would not do the -f test if this was my own script :)
 
A

anno4000

John Bokma said:
[...]
while(<CPU>)
{
if(/model.name/)
{
split;

The problem is here, you split to @_, which is deprecated, see:
perldoc -f split
print "The processor is an $_[3] $_[4] $_[5] running at
$_[7]\n";
}
}

I would do the while loop like:

while ( my $line = <$fh> ) {

$line =~ /model\.name/ or next;

my @data = split /\s+/, $line; # not sure about the pattern
print "The processor .. $data[ 0 ] $data[ 1 ] ...\n";
}

close $fh or die "Can't close '$CPU_FILE' after reading: $!";

I'd prefer to use significant names instead of the data array
(untested):

my ( $make, $model, $type, $speed) = (split)[ 3, 4, 5, 7];
print "The processor is a $make $model $type running at $speed\n";

Anno
 
J

John Bokma

I'd prefer to use significant names instead of the data array

For the record, me too. I couldn't be bothered though to look the names up
(SSH into a GNU/Linux box etc.). Should have written this, though.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,483
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top