Beefy Boxes and Bandwidth Generously Provided by pair Networks
Perl-Sensitive Sunglasses

what is bad about this?

by edwardt_tril (Sexton)
on Feb 06, 2006 at 21:19 UTC ( #528335=perlquestion: print w/replies, xml ) Need Help??

edwardt_tril has asked for the wisdom of the Perl Monks concerning the following question:

Can anyone tell me how the following is bad?:
$IP_ADDRESS     = @REALM[1];
I know it should be $REALM[1];
but I want a strong case for
this change, because it works in run time

Replies are listed 'Best First'.
Re: what is bad about this?
by syphilis (Bishop) on Feb 06, 2006 at 22:35 UTC
    See "What is the difference between $array[1] and @array[1]?" in perlfaq 4.

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

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

      What are you talking about?

      $IP_ADDRESS = @REALM[1];
      $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.

        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).
Re: what is bad about this?
by jkeenan1 (Deacon) on Feb 06, 2006 at 21:43 UTC
    Define "works".

    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";

      Not that you're not correct, you've misunderstood the OP's question. Because they didn't use <code> tags their []s got eaten. They're actually trying to ask what's wrong with using a one-element array slice @array[0] versus accessing the element directly $array[0].

Re: what is bad about this?
by ikegami (Pope) on Feb 06, 2006 at 21:38 UTC

    Are you planning on using slices (@hash{key}) for hashes too?

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://528335]
Approved by jkeenan1
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others having an uproarious good time at the Monastery: (5)
As of 2020-08-07 12:17 GMT
Find Nodes?
    Voting Booth?
    Which rocket would you take to Mars?

    Results (44 votes). Check out past polls.