Beefy Boxes and Bandwidth Generously Provided by pair Networks
Do you know where your variables are?
 
PerlMonks  

Re^3: How to test for empty hash?

by haukex (Archbishop)
on Aug 08, 2021 at 08:07 UTC ( [id://11135683]=note: print w/replies, xml ) Need Help??


in reply to Re^2: How to test for empty hash? (Perl Secret Operators)
in thread How to test for empty hash?

With all this talk of scalar, I had to look it up because, despite using Perl heavily for twenty years, I've never actually used it! Which reminded me that I have employed the equivalent "secret" version, namely the infamous inchworm ~~ secret operator

Just a little nitpick: the "inchworm" isn't exactly equivalent, as it doesn't pass through lvalue context, which scalar does as of 5.22.

$ perlbrew exec perl -e 'for(scalar($#foo)) { $_=3 } warn "$] ".@foo." +\n"' >/dev/null 5.034000 4 5.032001 4 5.030003 4 5.028003 4 5.026003 4 5.024004 4 5.022004 4 5.020003 0 5.018004 0 $ perlbrew exec perl -e 'for(~~$#foo) { $_=3 } warn "$] ".@foo."\n"' > +/dev/null 5.034000 0 5.032001 0 5.030003 0 5.028003 0 5.026003 0 5.024004 0 5.022004 0 5.020003 0 5.018004 0

Update: The example above is one of the exceptions for when ~~ is not equivalent to scalar anyway:

$ perl -le 'print scalar($#foo)' -1 $ perl -le 'print ~~$#foo' 18446744073709551615

... but the point still stands (though interestingly, my $foo="x"; for(scalar($foo)) {$_.="y"} worked even before 5.22).

Replies are listed 'Best First'.
Re^4: How to test for empty hash?
by eyepopslikeamosquito (Archbishop) on Aug 08, 2021 at 11:03 UTC

    Impressive analysis!

    It doesn't surprise me there are subtle differences. I only mentioned secret operators for some light relief, and (unlike BooK ;), would never inflict the inchworm secret operator on my workmates in production code ... my rule number one at work being "Correctness, simplicity and clarity come first. Avoid unnecessary cleverness".

    Curiously, your second example seems to be fixed by adding use integer:

    $ perl -le 'print ~~$#foo' 18446744073709551615 $ perl -Minteger -le 'print ~~$#foo' -1

    ... which reminds me of this recent reply by BillKSmith ... while typing -Minteger from the command line just now reminded me of chromatic's comical -Modern::Perl command line hack. :)

    Update: See also: the "Inchworm" and "Inchworm on a stick" sections at perlsecret and The Lighter Side of Perl Culture (Part IV): Golf (search for inchworm).

      ... chromatic's comical -Modern::Perl command line hack. :)

      See also oo and ojo, plus probably a few others out there :-)

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11135683]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others contemplating the Monastery: (6)
As of 2024-04-18 17:20 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found