Hi

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.


In reply to Prototype for constant items??? by LanX

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.