... you want to declare the variable in as narrow a scope as you can and still be able to access it where you need to.

++dvergin!

Of course, this assumes that the fragment presented was whittled down to some minimum amount needed to ask the question.

In other words, it's probably safe to presume that the if block would be followed by code which does something with the @entries array, such as return a reference to it or its contents, in which case perhaps something like this is more appropriate:

sub openFile { my ($file1, $file2) = @_; # assume these are passed as parameters my $fn = $file1 || $file2; # replaces the 'if' in original open FH, $fn or die "$fn: $!"; my @entries = <FH>; close FH; # in list context, return the entire array, else (scalar context) a +ref return wantarray ? @entries : \@entries; }

dmm


You can give a man a fish and feed him for a day ...
Or, you can teach him to fish and feed him for a lifetime

In reply to Re(2): where to declare a variable... by dmmiller2k
in thread where to declare a variable... by kiat

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.