Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

New to me: What does $var = -s 'file' do?

by edwyr (Sexton)
on Sep 05, 2018 at 15:22 UTC ( #1221773=perlquestion: print w/replies, xml ) Need Help??

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

I've started reading _Higher-Order Perl_ and read a command I'm not familiar with. Normally I would use perldoc or perldoc -f, but the regex is "painful" on this one...

The line is: my $total = -s $top; What is the = -s? Mike

Replies are listed 'Best First'.
Re: New to me: What does $var = -s 'file' do?
by choroba (Archbishop) on Sep 05, 2018 at 15:25 UTC
    perldoc -f -x

    returns among other:

    -s File has nonzero size (returns size in bytes).

    = is the normal old assignment operator.

    ($q=q:Sq=~/;[c](.)(.)/;chr(-||-|5+lengthSq)`"S|oS2"`map{chr |+ord }map{substrSq`S_+|`|}3E|-|`7**2-3:)=~y+S|`+$1,++print+eval$q,q,a,
Re: New to me: What does $var = -s 'file' do?
by edwyr (Sexton) on Sep 05, 2018 at 15:32 UTC
    I tried to get back quickly... :) The -s is probably just a test -s testing of the argument is a zero-length file. Thanks.

      Not quite.

      -z is used to check if it's a zero-length file. It returns '' if the file is greater than zero bytes, undef if the file doesn't exist, and 1 if the file is in fact empty.

      -s will return the number of bytes, including zero if file is empty:

      use warnings; use strict; use feature 'say'; say -s 'size.txt'; say -s 'empty.txt'; say -z 'size.txt'; say -z 'empty.txt';


      8 # -s, 8 bytes 0 # -s, empty file # -z, file has bytes 1 # -z, file is empty

      Thanks AnomalousMonk for privately correcting me on the return values from -z.

Re: New to me: What does $var = -s 'file' do?
by Anonymous Monk on Sep 06, 2018 at 17:50 UTC
    FWIW, this is why I made redirect appropriately.

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1221773]
Approved by marto
Front-paged by Corion
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (4)
As of 2020-09-24 14:32 GMT
Find Nodes?
    Voting Booth?
    If at first I donít succeed, I Ö

    Results (133 votes). Check out past polls.