@_ deprecated?

Discussion in 'Perl Misc' started by broeisito@gmail.com, Aug 9, 2006.

  1. Guest

    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
     
    , Aug 9, 2006
    #1
    1. Advertising

  2. Guest

    "" <> wrote:
    > 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

    --
    -------------------- http://NewsReader.Com/ --------------------
    Usenet Newsgroup Service $9.95/Month 30GB
     
    , Aug 9, 2006
    #2
    1. Advertising

  3. John Bokma Guest

    "" <> wrote:

    > Hello,


    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: $!";


    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Aug 9, 2006
    #3
  4. Guest

    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 Bokma wrote:
    > "" <> wrote:
    >
    > > Hello,

    >
    > 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: $!";
    >
    >
    > --
    > John Bokma Freelance software developer
    > &
    > Experienced Perl programmer: http://castleamber.com/
     
    , Aug 9, 2006
    #4
  5. Ben Morrow Guest

    Quoth John Bokma <>:
    > "" <> wrote:
    > >
    > > 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: $!";


    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

    --
    I must not fear. Fear is the mind-killer. I will face my fear and
    I will let it pass through me. When the fear is gone there will be
    nothing. Only I will remain.
    Frank Herbert, 'Dune'
     
    Ben Morrow, Aug 9, 2006
    #5
  6. Justin C Guest

    On 2006-08-09, <> wrote:
    > 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.

    --
    Justin C, by the sea.
     
    Justin C, Aug 9, 2006
    #6
  7. John Bokma Guest

    "" <> wrote:

    > 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).
     
    John Bokma, Aug 10, 2006
    #7
  8. John Bokma Guest

    Ben Morrow <> wrote:

    >
    > Quoth John Bokma <>:
    >> "" <> wrote:
    >> >
    >> > 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: $!";

    >
    > 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 :)

    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Aug 10, 2006
    #8
  9. -berlin.de Guest

    John Bokma <> wrote in comp.lang.perl.misc:
    > "" <> wrote:


    [...]

    > > 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
     
    -berlin.de, Aug 10, 2006
    #9
  10. John Bokma Guest

    -berlin.de wrote:

    > John Bokma <> wrote in comp.lang.perl.misc:


    >> my @data = split /\s+/, $line; # not sure about the


    > 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.


    --
    John Bokma Freelance software developer
    &
    Experienced Perl programmer: http://castleamber.com/
     
    John Bokma, Aug 10, 2006
    #10
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Barney Barumba
    Replies:
    0
    Views:
    571
    Barney Barumba
    Jul 23, 2003
  2. david
    Replies:
    4
    Views:
    6,372
    david
    Oct 9, 2003
  3. David McDivitt

    deprecated getResources method

    David McDivitt, May 18, 2004, in forum: Java
    Replies:
    2
    Views:
    1,889
    David McDivitt
    May 19, 2004
  4. Bernd Oninger
    Replies:
    1
    Views:
    416
    GIMME
    Jun 10, 2004
  5. kaeli
    Replies:
    3
    Views:
    4,925
    Eric Sosman
    Aug 24, 2004
Loading...

Share This Page