Re: Issue with references and array slice with one member !



On Fri, Oct 31, 2008 at 05:46, Amit Saxena <learn.tech123@xxxxxxxxx> wrote:
snip
I used "${$row}[0]", "${$row}[1]" etc one by one to get all the columns for
a particular code. On one of the production code which I am working, they
have used "@{$row}[0]" , "@{$row}[0]" instead of earlier. The results for
both of them were same.

When I did a through debugging on this, I found that "@{$row}[0]" or
"@{$row}[1]" etc is taken as array slice by perl with only one member so it
returns the value of the column in scalar and not in list context. With
"${$row}[0]" or "${$row}[1]" etc, the column value is returned in scaler
context.

I want to know whether it's appropriate to use "@{$row}[0]" , "@{$row}[1]"
instead of "${$row}[0]", "${$row}[1]" though both of them gives same result
snip

${$row}[0] is better, but $row->[0] is best.

If you need ammunition to convince others then show them this code and
then run it on a fairly modern Perl interpreter. Perl will throw a
warning like "Scalar value @a[0] better written as $a[0] at z.pl line
8." It is interesting a warning isn't thrown for references. I shall
have to research that.

#!/usr/bin/perl

use strict;
use warnings;

my @a = qw/a b c/;

print @a[0], "\n";

All of that said, @{$row}[0] has its uses:

@{$row}[0] = qw(a b c);

produces difference results from

$row->[0] = qw(a b c);

But it is probably better written as

$row->[0] = (qw(a b c))[0];

to avoid confusing people.

--
Chas. Owens
wonkden.net
The most important skill a programmer can have is the ability to read.
.



Relevant Pages

  • Re: BARRY & THE BROTHERS TO THE RESCUE SHOOTDOWN
    ... [snip portions of Kenny's posting already debunked here, ... It is an "advisory" NOT a warning to depart or not enter. ... warnings prior to the flight. ... >>> Look at the LOGS Danny they state the EXACT times. ...
    (soc.culture.cuba)
  • Re: Identify source of warning
    ... post because I wasn't saying I thought using undef there was the way ... I did that to try to trip the same warning output. ...
    (perl.beginners)
  • Re: Does anyone know what this text means? (related to C4251 warning)
    ... It isn't type derivation, but it is inheritance in the OOP sense of the ... It doesn't just "avoid the warning". ... Every single use of dllimport/dllexport on a type violates the ODR. ...
    (microsoft.public.dotnet.languages.vc)
  • Re: 2 problems parsing output from HTML::TableExtract
    ... characters in the string that, while not a problem in browsers ... Here is the code block the warning relates to: ... foreach $ts { ...
    (comp.lang.perl.misc)
  • lxrun and RH 7.3 or something else
    ... warning: /etc/ld.so.conf created as /etc/ld.so.conf.rpmnew ... GNU gdb 5.0 ... Do I really have to have Sun Linux 5.0 in order to run lxrun? ...
    (comp.unix.solaris)