LanX has asked for the wisdom of the Perl Monks concerning the following question:
I'd like to construct a prototype which checks at compile time for a "real scalar value" instead of forcing scalar context.
Using (\$) excludes literals and there doesn't seem to be any alternative for constants to be included in ((\[$@%&*]))
(more details in docs for Prototypes)
some example code to make it clearer:
DB<1> sub tst ($) { print @_} # primitive approach DB<2> @a=qw/a b c/ DB<3> tst @a # NO! I don't want to force scalar c +ontext on other datatypes 3 DB<4> sub tst2 (\$) { print @_} # Restrict to scalars starting with +sigil "$" DB<5> tst2 @a # YES! at least there is no scalar c +ontext Type of arg 1 to main::tst2 must be scalar (not array dereference) at +(eval 11)[/usr/share/perl/5.10/perl5db.pl:638] line 2, at EOF<P><P> DB<6> tst2 "1" # NO! Constants are not allowed ... +sigh Type of arg 1 to main::tst2 must be scalar (not constant item) at (eva +l 9)[/usr/share/perl/5.10/perl5db.pl:638] line 2, at EOF
Any ideas? Am I missing something?
(....I'm not really confident anyhow ...)
Cheers Rolf
UPDATE: yeah I know that I still need to dereference with (\$), but that's not the point here.
UPDATE2: now that's confusing, isn't it?
DB<9> print ref \"1" SCALAR
UPDATE3: (16:25 CEST) Is this a purely academic discussion? Don't think so, let me give you an example of a problem with the command join:
DB<13> @a=qw/a b c/ DB<14> print join ":",@a # RIGHT a:b:c DB<15> print join @a,":" # NONSENSE (at least in 99.9% of th +e cases) but no error message : DB<16> # What if I REALLY want the length as delimiter? DB<17> print join scalar @a,":" # well, in <0.1% of the usecases, yo +u can afford to be explicit :
Many beginners (and even me sometimes) get confused about when to list the delimiter argument. But the compiler doesn't complain about the most obviously wrong syntax and the user hast to debug the problem...
Now the experienced programmers has to explain why an array in scalar context returns a valid argument ... and the newbie gets frustrated, and asks himself why he ever has chosen perl...
IMHO extending the prototype mechanism such that it's possible to catch these cases would really improve perl's acceptance.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Prototype for constant items?
by moritz (Cardinal) on Sep 25, 2009 at 12:51 UTC | |
by LanX (Saint) on Sep 25, 2009 at 13:13 UTC | |
by moritz (Cardinal) on Sep 25, 2009 at 13:37 UTC | |
by LanX (Saint) on Sep 25, 2009 at 13:52 UTC | |
by moritz (Cardinal) on Sep 25, 2009 at 14:04 UTC | |
|
Re: Prototype for constant items???
by ikegami (Patriarch) on Sep 25, 2009 at 14:35 UTC | |
|
Re: Prototype for constant items???
by Bloodnok (Vicar) on Sep 25, 2009 at 13:22 UTC | |
by LanX (Saint) on Sep 25, 2009 at 13:39 UTC | |
by SuicideJunkie (Vicar) on Sep 25, 2009 at 14:40 UTC | |
by LanX (Saint) on Sep 25, 2009 at 14:55 UTC | |
by SuicideJunkie (Vicar) on Sep 25, 2009 at 16:34 UTC | |
| |
by Porculus (Hermit) on Sep 26, 2009 at 17:37 UTC | |
| |
|
Re: Prototype for constant items???
by cdarke (Prior) on Sep 25, 2009 at 16:42 UTC |