in reply to how to force scalar context (without scalar())?
The classical trick is to use ~~ which is bitwise negation applied twice. It doesn't work for references or anything slightly magical as it numifies or stringifies its operand.
Personally though, I have no problems what so ever with scalar() and the indent of its use can't be any clearer. I advocate for the use of scalar().
Update:
You can also do (my $whatever = ...) but that's also ugly and not a perfect equivalent to scalar(), which I must emphazise is the perfect function for this with no pitfalls. The scalar assignment operator returns the LHS, i.e. e.g. \(my $whatever = ...) references $whatever and not the expression you wanted in scalar context.
Your sub ss ($) { $_[0] } function isn't either a perfect equivalent to scalar() since it returns a copy of the value.
my $foo; print \$foo eq \scalar($foo); # true print \$foo eq \ss($foo); # false
I can see this as a fun exercise or quiz but I really have to question practicing using anything but scalar() as a general scalar context enforcing operator for anything except obfuscation.
ihb
Read argumentation in its context!
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re^2: how to force scalar context (without scalar())?
by borisz (Canon) on Sep 19, 2004 at 12:16 UTC |