Unfortunately not all functions will send back the contectula meaning you think it implied. Different list returning functions will return different things when returning scalars. localtime will return a join of it's array; readline ( and likewise <> ) will return the 'next' value in the series ( mainly I think, because it's designed to be iterative ); Others may return the array and default to the scalar value as you had thought about the <> statement

I believe the important thing would be to not make assumptions. Once you get to know which returns which, you might be tempted to make accomodate each. I myself would stick to what I know would work, in this case return an array, then use scalar to get the count
.... my @lines = <$fh>; my $count = scalar @lines; ....
That also tends to allow others reading your code to understand your code, especially if they still do not know the ins and outs of contextual returns.

As far as reading the file, slurp works well and I won't dispute it's usefulness, but I am still old school.
# Unstested my strict; my warnings; my $content = ''; my $filename = 'myfile.txt'; { local $\; open $fh, '<', $filename or die "Cannot open $filename: $!\n"; $content = <$fh>; close $fh; } print $content;
Here we localize the input record seperator, or enable 'slurp' mode, and the call using <> will pull in the whole file.

Don
WHITEPAGES.COM | INC
Everything I've learned in life can be summed up in a small perl script!

In reply to Re: Reading files in scalar context with <> operator by BaldPenguin
in thread Reading files in scalar context with <> operator by senőr_biggles

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.