casiano has asked for the wisdom of the Perl Monks concerning the following question:
Is there any way to know if my sub was called in a "numerical" context or a "string" context?
A partial solution is to use dualvar from Scalar::Util but then I have to compute both the string and the number on each call.use strict; sub sensible { return "one" if stringcontext(); return 1 if numericalcontext(); return; } print "Result = ".sensible()."\n"; # Result = one print "Result = ".0+sensible()."\n"; # Result = 1
Many thanks
Casiano
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Finding the context of a call: numerical or string
by kyle (Abbot) on Feb 29, 2008 at 16:35 UTC | |
by casiano (Pilgrim) on Feb 29, 2008 at 16:58 UTC | |
by sgt (Deacon) on Mar 02, 2008 at 12:52 UTC | |
|
Re: Finding the context of a call: numerical or string
by ikegami (Patriarch) on Feb 29, 2008 at 17:19 UTC | |
|
Re: Finding the context of a call: numerical or string
by leocharre (Priest) on Feb 29, 2008 at 16:56 UTC | |
by TGI (Parson) on Feb 29, 2008 at 20:03 UTC |