Register a SA Forums Account here!
JOINING THE SA FORUMS WILL REMOVE THIS BIG AD, THE ANNOYING UNDERLINED ADS, AND STUPID INTERSTITIAL ADS!!!

You can: log in, read the tech support FAQ, or request your lost password. This dumb message (and those ads) will appear on every screen until you register! Get rid of this crap by registering your own SA Forums Account and joining roughly 150,000 Goons, for the one-time price of $9.95! We charge money because it costs us money per month for bills, and since we don't believe in showing ads to our users, we try to make the money back through forum registrations.
 
  • Post
  • Reply
Jabor
Jul 16, 2010

#1 Loser at SpaceChem
If you take a list, and use it in scalar context, you get the number of items in it. Pretty useful in a bunch of places.

But what if you have one of these functions that works differently in scalar context, but you want to call the listy version and then count how many values there are? You could just use a temporary, as described above, but the true Perl master would use the goatse operator =()= (alternatively written =( )=, if you prefer authenticity over compactness).

For example, to count how many times a regex matches a string:

code:

my $count =()= $str =~ /myregex/g

Adbot
ADBOT LOVES YOU

Jabor
Jul 16, 2010

#1 Loser at SpaceChem
That's exactly how it works in every other language though?

Using side-effecting expressions there is weird, but that's on the person writing side-effecting expressions in that context.

  • 1
  • 2
  • 3
  • 4
  • 5
  • Post
  • Reply