Re: what is bad about this?
by syphilis (Archbishop) on Feb 06, 2006 at 22:35 UTC
|
See "What is the difference between $array[1] and @array[1]?" in perlfaq 4.
Cheers, Rob | [reply] [d/l] [select] |
Re: what is bad about this?
by kutsu (Priest) on Feb 06, 2006 at 21:42 UTC
|
Because your program won't run (actually it will run but will also fill up your logs with a lot of unnessary warnings) under warnings and strict (if you want to know why it's a good idea to have them on Use strict warnings and diagnostics or die). But as to why it's a bad idea in general check out this quote from diagnostics:
Scalar value @a[1] better written as $a[1] at -e line 1 (#1) (W syntax) You've used an array slice (indicated by @) to select a single element of an array. Generally it's better to ask for a scalar value (indicated by $). The difference is that $foo[&bar] always behaves like a scalar, both when assigning to it and when evaluating its argument, while @foo[1] behaves like a list when you assign to it, and provides a list context to its subscript, which can do weird things if you're expecting only one subscript.
On the other hand, if you were actually hoping to treat the array element as a list, you need to look into how references work, because Perl will not magically convert between scalars and lists for you. See perlref.
| [reply] |
Re: what is bad about this?
by InfiniteSilence (Curate) on Feb 06, 2006 at 21:27 UTC
|
Let's see:
- The title of your question needs work
- You need to use <code> tags to encapsulate your code
- When you use brackets in PerlMonks, you are referring to a Node
Celebrate Intellectual Diversity
| [reply] |
Re: what is bad about this?
by runrig (Abbot) on Feb 06, 2006 at 21:36 UTC
|
Because if you're following good practices and using strict and warnings, changing that line is the easiest way to "fix" the warning? (But then you'll probably have to fix a few "Global symbol" errors also). | [reply] |
|
What are you talking about?
Neither
$IP_ADDRESS = @REALM[1];
nor
$IP_ADDRESS = $REALM[1];
generates a warning.
And strict is not relevant. If one generates a strict fault, so will the other. If one doesn't generate a strict fault, neither will the other.
| [reply] [d/l] [select] |
|
Maybe it's a new warning? (From 5.8.4):
Scalar value @REALM[1] better written as $REALM[1] at ...
Also, I'm just guessing the OP is using neither strict nor warnings. If he was using warnings, he'd be getting a warning and probably fixing it, and he'd probably already be using strict. If he does start using strict and warnings, then I bet there will be a bunch of errors to fix (and a good chance that either version of that line will need to be fixed). | [reply] [d/l] |
|
Re: what is bad about this?
by jkeenan1 (Deacon) on Feb 06, 2006 at 21:43 UTC
|
use strict;
use warnings;
my ($IP_ADDRESS, @REALM1);
@REALM1 = qw(alpha beta gamma);
$IP_ADDRESS = @REALM1;
print "$IP_ADDRESS\n";
@REALM1 = qw(alpha);
$IP_ADDRESS = @REALM1;
print "$IP_ADDRESS\n";
my $REALM1;
$REALM1 = q{alpha};
$IP_ADDRESS = $REALM1;
print "$IP_ADDRESS\n";
| [reply] [d/l] |
|
| [reply] [d/l] [select] |
Re: what is bad about this?
by ikegami (Patriarch) on Feb 06, 2006 at 21:38 UTC
|
| [reply] [d/l] |