Beefy Boxes and Bandwidth Generously Provided by pair Networks
Think about Loose Coupling
 
PerlMonks  

comment on

( [id://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;.

Summary

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

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others learning in the Monastery: (3)
As of 2024-04-19 17:11 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found