Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine

Comment on

( #3333=superdoc: print w/replies, xml ) Need Help??
Perl Idioms Explained - my $count = () = /.../g
my $str = "here are some words"; my $count = () = $str =~ /\S+/g;
The "empty parentheses" idiom is one that is used to force list context on an expression, and yet return a scalar: the count of items in that list. We are often told that there is no way to get the size of a list -- it must either be iterated over, or stored in an array, or some other means -- because when you try treating a list as a scalar, you're no longer dealing with a list: you're enforcing scalar context on some expression. Here are some examples:
my $last_operand = ("a", "b", "c"); # "c" my $true_or_false = /\w+/g; # 1 or "" my $dunno = some_function(); # who knows?!
The last example is particularly nasty: the return value of the function will be evaluated in scalar context. If that value is @array, then you'll get the size of the array; if it's @array[0,1], you'll get $array[1].

So how does the empty parentheses idiom work? It uses a little-exercised rule: a list assignment in scalar context returns the number of elements on the right-hand side of the list assignment. That's probably also rarely-spoken, since it's a bit dense. What it means is that an expression like (LIST1) = (LIST2), in scalar context, returns the number of elements in LIST2 (not LIST1). This means (LIST1) could be replaced with () and the scalar value returned would still be the size of LIST2.

This trick is most frequently used in conjunction with a regex with the "global" switch on it. In scalar context, such a regex would only match once, but in list context, it matches exhaustively. To get all such matches, one would do @matches = /.../g. But if we aren't interested in keeping the matches around, and instead just want to know how many there are, we could replace @matches with (), and evaluate it all in scalar context: my $count = (() = /.../g);. Of course, those outer parentheses are unnecessary, so the idiom becomes my $count = () = /.../g;.


It forces an expression to be evaluated in list context, and returns the number of elements returned by that expression.

Jeff japhy Pinyan, P.L., P.M., P.O.D, X.S.: Perl, regex, and perl hacker
How can we ever be the sold short or the cheated, we who for every service have long ago been overpaid? ~~ Meister Eckhart

In reply to Perl Idioms Explained - my $count = () = /.../g by japhy

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, 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, 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.
  • Log In?

    What's my password?
    Create A New User
    [Discipulus]: Corion are you would able to realize such thing? O_O
    [Corion]: In the same vein I have a script that automates Firefox to enter some data into another system. It's not faster than the people using the script if they were to do it manually, but they prefer not having to check the data and not having typos when ...
    [Corion]: ... entering the data
    [Corion]: Discipulus: I don't know whether I could really do that, but the init process itself mostly launches other processes, and the whole startup is just following a path of dependencies and making sure they are all running. Which basically is what ...
    [Discipulus]: when at work my time is (temporarly) owned by the firm, so i do not care (coworkers whatch movies.. I code Perl)
    [Corion]: ... make already does, except for files instead of programs. But maybe with some /proc hackery, that could be eliminated and one could use plain make :-D
    [choroba]: systemd just makes is asynchronous
    [choroba]: so, make -j
    [Corion]: Discipulus: Yeah - but when writing Perl to save time (instead of having fun), it helps to look whether you're actually saving time ;) Why spend 5 minutes doing manually what you can spend three years automating? ;)
    [Corion]: choroba: Oh, yeah :-D

    How do I use this? | Other CB clients
    Other Users?
    Others rifling through the Monastery: (4)
    As of 2017-07-27 09:30 GMT
    Find Nodes?
      Voting Booth?
      I came, I saw, I ...

      Results (408 votes). Check out past polls.