Mailing List Archive

prototype docs
I have added the notes from Larry on prototyping to perlsub.pod;
they're somewhat succinct.

I did NOT add this:

Writers of C modules conversant with L<perlxs> might wish to consider
something like:

sv_setpv((SV*)newXS("POSIX::setlocale", XS_POSIX_setlocale, file), "$$");
sv_setpv((SV*)newXS("POSIX::sigaction", XS_POSIX_sigaction, file), "$$;$");

Because I'm not sure where it goes. (First patch may be dup)

--tom

*** pod/perlsub.pod Sun Mar 12 20:42:58 1995
--- npod/perlsub.pod Thu Dec 14 07:10:32 1995
***************
*** 112,118 ****
no outside module can see the subroutine, since its name is not in any
package's symbol table.

! =head2 Passing Symbol Table Entries

[.Note: The mechanism described in this section works fine in Perl 5, but
the new reference mechanism is generally easier to work with. See L<perlref>.]
--- 112,118 ----
no outside module can see the subroutine, since its name is not in any
package's symbol table.

! =head2 Passing Symbol Table Entries (typeglobs)

[.Note: The mechanism described in this section works fine in Perl 5, but
the new reference mechanism is generally easier to work with. See L<perlref>.]
***************
*** 205,210 ****
can treat undefined subroutine calls as calls to Unix programs.

There are mechanisms available for modules to help them split themselves
! up into autoloadable files to be used with the standard AutoLoader module.
! See the document on extensions.

--- 205,303 ----
can treat undefined subroutine calls as calls to Unix programs.

There are mechanisms available for modules to help them split themselves
! up into autoloadable files. See the standard AutoLoader module described
! in L<Autoloader>, the standard SelfLoader modules in L<SelfLoader>, and
! the document on adding C functions to perl code in L<perlxs>.

+ =head2 Prototypes
+
+ As of the 5.002 release of perl, if you declare
+
+ sub mypush (\@@)
+
+ then mypush takes arguments exactly like push does. Here are the
+ prototypes for some other functions that parse almost exactly like
+ the corresponding builtins.
+
+ Declared as Called as
+
+ sub mylink ($$) mylink $old, $new
+ sub myvec ($$$) myvec $var, $offset, 1
+ sub myindex ($$;$) myindex &getstring, "substr"
+ sub mysyswrite ($$$;$) mysyswrite $buf, 0, length($buf) - $off, $off
+ sub myreverse (@) myreverse $a,$b,$c
+ sub myjoin ($@) myjoin ":",$a,$b,$c
+ sub mypop (\@) mypop @array
+ sub mysplice (\@$$@) mysplice @array,@array,0,@pushme
+ sub mykeys (\%) mykeys %{$hashref}
+ sub myopen (*;$) myopen HANDLE, $name
+ sub mypipe (**) mypipe READHANDLE, WRITEHANDLE
+ sub mygrep (&@) mygrep { /foo/ } $a,$b,$c
+ sub myrand ($) myrand 42
+ sub mytime () mytime
+
+ Any backslashed prototype character must be passed something starting
+ with that character. Any unbackslashed @ or % eats all the rest of the
+ arguments, and forces list context. An argument represented by $
+ forces scalar context. An & requires an anonymous subroutine, and *
+ does whatever it has to to turn the argument into a reference to a
+ symbol table entry. A semicolon separates mandatory arguments from
+ optional arguments.
+
+ Note that the last three are syntactically distinguished by the lexer.
+ mygrep() is parsed as a true list operator, myrand() is parsed as a
+ true unary operator with unary precedence the same as rand(), and
+ mytime() is truly argumentless, just like time(). That is, if you
+ say
+
+ mytime +2;
+
+ you'll get mytime() + 2, not mytime(2), which is how it would be parsed
+ without the prototype.
+
+ The interesting thing about & is that you can generate new syntax with it:
+
+ sub try (&$) {
+ my($try,$catch) = @_;
+ eval { &$try };
+ if ($@) {
+ local $_ = $@;
+ &$catch;
+ }
+ }
+ sub catch (&) { @_ }
+
+ try {
+ die "phooey";
+ } catch {
+ /phooey/ and print "unphooey\n";
+ };
+
+ That prints "unphooey". (Yes, there are still unresolved
+ issues having to do with the visibility of @_. I'm ignoring that
+ question for the moment. (But note that if we make @_ lexically
+ scoped, those anonymous subroutines can act like closures... (Gee,
+ is this sounding a little Lispish? (Nevermind.))))
+
+ And here's a reimplementation of grep:
+
+ sub mygrep (&@) {
+ my $code = shift;
+ my @result;
+ foreach $_ (@_) {
+ push(@result, $_) if &$ref;
+ }
+ @result;
+ }
+
+ Some folks would prefer full alphanumeric prototypes. Alphanumerics have
+ been intentionally left out of prototypes for the express purpose of
+ adding named, formal parameters. The current mechanism's main goal is to
+ let module writers provide better diagnostics for module users. I fee the
+ notation quite understandable to Perl programmers, and that it will not
+ intrude greatly upon the meat of the module, nor make it harder to read.
+ The line noise is visually encapsulated into a small pill that's easy to
+ swallow.
+
+ This is all very powerful, of course, and should only be used in moderation
+ to make the world a better place.
Re: prototype docs [ In reply to ]
>From: Tom Christiansen <tchrist@mox.perl.com>

>I have added the notes from Larry on prototyping to perlsub.pod;
>they're somewhat succinct.
>
>I did NOT add this:
>
> Writers of C modules conversant with L<perlxs> might wish to consider
> something like:
>
> sv_setpv((SV*)newXS("POSIX::setlocale", XS_POSIX_setlocale, file), "$$");
> sv_setpv((SV*)newXS("POSIX::sigaction", XS_POSIX_sigaction, file), "$$;$");

xsubpp is your friend :)

newXSproto("POSIX::setlocale", XS_POSIX_setlocale, file, "$$");
newXSproto("POSIX::sigaction", XS_POSIX_sigaction, file, "$$;$");

As of xsubpp 1.925 this is a not something the programmer has to worry about.

Dean
Re: prototype docs [ In reply to ]
>Here are the relevant lines from POSIX.c in 5.002b1f

> newXSproto("POSIX::setlocale", XS_POSIX_setlocale, file, "$$");
> newXSproto("POSIX::sigaction", XS_POSIX_sigaction, file, "$$;$");

>xsubpp will now automatically add prototypes to xsub's unless it has
>been explicitly commanded not to.

Cool.

Lamentably, this isn't installed as a proper man page. It just
sits alone in lib/ExtUtils/xsubpp and no one knows about it. It
isn't even installed as a program.

pod2man *is* installed as a program, but its docs don't get
installed. Drat. The current installman doesn't know
to do this properly.

I'm on the verge of advocating a default set up like this.

programs $prefix/man/man1/foo.1
perl.1
xsubpp.1
pod2man.1
libs, modules $prefix/man/man3/foo.3
less.3
Carp.3
FileHandle.3
POSIX.3
aux doc $prefix/man/man7/foo.7
modules.7
syntax.7
forms.7

etc.

--tom
Re: prototype docs [ In reply to ]
> From: Tom Christiansen <tchrist@mox.perl.com>

> Lamentably, this isn't installed as a proper man page. It just
> sits alone in lib/ExtUtils/xsubpp and no one knows about it. It
> isn't even installed as a program.

How to run xsubpp:

make Foo.c

Really, this should not be installed as a command along with pod2man. MM
takes care of xsubpp for you.

And as for the manpage:

perldoc ExtUtils::xsubpp

which is as it should be, for this particular tool, I think.

Dean
Re: prototype docs [ In reply to ]
>And as for the manpage:

> perldoc ExtUtils::xsubpp

>which is as it should be, for this particular tool, I think.

I think not.

apropos xsubpp

Better give me the correct answer.

--tom
Re: prototype docs [ In reply to ]
>From: Tom Christiansen <tchrist@mox.perl.com>
>List-Name: perl5-porters
>
>>And as for the manpage:
>
>> perldoc ExtUtils::xsubpp
>
>>which is as it should be, for this particular tool, I think.
>
>I think not.
>
> apropos xsubpp
>
>Better give me the correct answer.

Is man(1) working for us or are we working for man(1) ?

Dean
Re: prototype docs [ In reply to ]
>>>And as for the manpage:
>>> perldoc ExtUtils::xsubpp
>>>which is as it should be, for this particular tool, I think.
>>
>>I think not.
>> apropos xsubpp
>>Better give me the correct answer.

>Is man(1) working for us or are we working for man(1) ?

That's a reasonable question; let me attempt to give it a reasonable
answer.

Here's my premise:

The command to look up documentation on Unix is man, along with man -k
(apropos). This should suffice for ALL perl documentation.

Now you're telling me it won't. That's just not something I'm simpy
prepared to live with.

If I have to start all the way from ground-zero and rewrite my man
program so it works on silly systems then I'll do that. As far as I'm
concerned, "perldoc" (whose doc isn't even installed) is a temporary
workaround for primitive systems, but should be unneeded on any system
running a reasonable man, including BSD or SysV versions -- or mine.

--tom
Re: prototype docs [ In reply to ]
On Thu, 14 Dec 1995, Tom Christiansen wrote:

> >Here are the relevant lines from POSIX.c in 5.002b1f
>
> > newXSproto("POSIX::setlocale", XS_POSIX_setlocale, file, "$$");
> > newXSproto("POSIX::sigaction", XS_POSIX_sigaction, file, "$$;$");
>
> >xsubpp will now automatically add prototypes to xsub's unless it has
> >been explicitly commanded not to.
>
> Cool.
>
> Lamentably, this isn't installed as a proper man page. It just
> sits alone in lib/ExtUtils/xsubpp and no one knows about it. It
> isn't even installed as a program.
>
> pod2man *is* installed as a program, but its docs don't get
> installed. Drat. The current installman doesn't know
> to do this properly.
>
> I'm on the verge of advocating a default set up like this.
>
> programs $prefix/man/man1/foo.1
> perl.1
> xsubpp.1
> pod2man.1
> libs, modules $prefix/man/man3/foo.3
> less.3
> Carp.3
> FileHandle.3
> POSIX.3
> aux doc $prefix/man/man7/foo.7
> modules.7
> syntax.7
> forms.7

Of course those should be
$man1dir/foo.$man1ext
$man3dir/foo.$man3ext

I can easily add $man7dir and $man7ext to Configure.

However, note that miscellaneus stuff is under man5 in the SVR4 world,
-- Section 7 is for special files and device drivers. I think.
I have no idea what other systems do. I also don't think it's critical,
but It'd be a shame for the man page structure to be driven by some
out-of-date unsupported non-POSIX operating system :-)

Andy Dougherty doughera@lafcol.lafayette.edu
Re: prototype docs [ In reply to ]
From: Tom Christiansen <tchrist@mox.perl.com>
>
> I have added the notes from Larry on prototyping to perlsub.pod;
> they're somewhat succinct.
>
> I did NOT add this:
>
> Writers of C modules conversant with L<perlxs> might wish to consider
> something like:
>
> sv_setpv((SV*)newXS("POSIX::setlocale", XS_POSIX_setlocale, file), "$$");
> sv_setpv((SV*)newXS("POSIX::sigaction", XS_POSIX_sigaction, file), "$$;$");
>
> Because I'm not sure where it goes. (First patch may be dup)

Here are the relevant lines from POSIX.c in 5.002b1f

newXSproto("POSIX::setlocale", XS_POSIX_setlocale, file, "$$");
newXSproto("POSIX::sigaction", XS_POSIX_sigaction, file, "$$;$");

xsubpp will now automatically add prototypes to xsub's unless it has
been explicitly commanded not to.

Paul