Beefy Boxes and Bandwidth Generously Provided by pair Networks
good chemistry is complicated,
and a little bit messy -LW
 
PerlMonks  

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 (Bishop) 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';

      Output:

      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 https://perldoc.pl/search?q=-s redirect appropriately.

Log In?
Username:
Password:

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

How do I use this? | Other CB clients
Other Users?
Others browsing the Monastery: (5)
As of 2019-10-16 21:49 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Notices?