Update: Changed the foreach push to a simple push. I will now lay down the crack pipe and step away from the keyboard.

Using ref is quite frequently an indication of a design flaw. The higher level the code you're writing, the less likely it is that you'll want to use it. Here's an example that could cause a problem:

my $foo = some_func(); if ('ARRAY' eq ref $foo) { push @data => @$foo; } else { push @data => $foo; }

That's bug prone. Instead, it might be cleaner if some_func() always returned an array ref. If that's the case, the above code becomes:

my $foo = some_func(); push @data => @$foo;

The code becomes easier to read and understand as a result.

If we're talking about what a function might accept, though, I'm a bit less stringent, but it's still easy to write bugs.

sub some_func { my $self = shift; my @data = 'ARRAY' eq ref $_[0] ? @$_[0] : @_; # do stuff }

On the surface, it looks like some_funct() can take an array, a list, or an array reference, and still behaves the same, regardless. However, what happens if some tries to pass in a list where the first element is also an array ref? Program fall down and go boom.

In short, I don't disagree with using ref. Further, it might be appropriate for your program but I can't really say as I don't know what you are doing. However, any time you see someone using ref, you should look very carefully at their program to see if bugs are crawling around in there.

Cheers,
Ovid

New address of my CGI Course.


In reply to Re: Is "ref $date eq 'ARRAY'" wrong? by Ovid
in thread Is "ref $date eq 'ARRAY'" wrong? by bronto

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.