Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Code Ref - or my stupidness #1294

Closed
p5pRT opened this issue Mar 9, 2000 · 18 comments
Closed

Code Ref - or my stupidness #1294

p5pRT opened this issue Mar 9, 2000 · 18 comments

Comments

@p5pRT
Copy link

p5pRT commented Mar 9, 2000

Migrated from rt.perl.org#2291 (status was 'resolved')

Searchable as RT2291$

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From jarausch@numa1.igpm.rwth-aachen.de

Created by jarausch@igpm.rwth-aachen.de

Please explain this to me if it's not a bug

use strict;

my $FRf = \&sin;

print "sin(1)= ",sin(1),"\n"; # OK

print "sin(1)= ",&$FRf(1),"\n"; # Undefined subroutine &main​::sin called at ...

Perl Info


Site configuration information for perl v5.6.0:

Configured by jarausch at Tue Mar  7 08:15:38 CST 2000.

Summary of my perl5 (revision 5.0 version 6 subversion 0) configuration:
  Platform:
    osname=irix, osvers=6.5, archname=IP26-irix
    uname='irix64 numa1 6.5 10181059 ip26 '
    config_args='-Dcc=cc -Dprefix=/usr/LOCAL -Dlocincpth=/usr/LOCAL/include -Dloclibpth=/usr/LOCAL/lib'
    hint=previous, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=undef d_sfio=undef uselargefiles=define 
    use64bitint=undef use64bitall=undef uselongdouble=undef usesocks=undef
  Compiler:
    cc='cc', optimize='-g3 -O3 -OPT:Olimit=0:space=on -mips4', gccversion=
    cppflags='-D_BSD_TYPES -D_BSD_TIME -I/usr/LOCAL/include -DLANGUAGE_C'
    ccflags ='-D_BSD_TYPES -D_BSD_TIME -I/usr/LOCAL/include -DLANGUAGE_C'
    stdchar='unsigned char', d_stdstdio=define, usevfork=false
    intsize=4, longsize=4, ptrsize=4, doublesize=8
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=8, usemymalloc=n, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -Wl,-woff,84 -L/usr/LOCAL/lib'
    libpth=/usr/LOCAL/lib /usr/lib32 /lib32
    libs=-lgdbm -ldb -lm -lc
    libc=/usr/lib32/libc.so, so=so, useshrplib=true, libperl=libperl.so
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='  -Wl,-rpath,/usr/LOCAL/lib/perl5/5.6.0/IP26-irix/CORE'
    cccdlflags='-KPIC', lddlflags='-shared -L/usr/LOCAL/lib'

Locally applied patches:
    v5.6.0-RC1


@INC for perl v5.6.0:
    /usr/LOCAL/lib/perl5/5.6.0/IP26-irix
    /usr/LOCAL/lib/perl5/5.6.0
    /usr/LOCAL/lib/perl5/site_perl/5.6.0/IP26-irix
    /usr/LOCAL/lib/perl5/site_perl/5.6.0
    /usr/LOCAL/lib/perl5/site_perl
    .


Environment for perl v5.6.0:
    HOME=/usr/people/jarausch
    LANG=C
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=.:/usr/people/jarausch/bin:/usr/PRIVATE/bin:/usr/LOCAL/bin:/usr/LOCAL/teTeX/bin:/usr/bin:/usr/java/bin:/usr/bsd:/bin:/usr/bin/X11:/usr/local/bin:/usr/local/bin/X11:/usr/sbin:/usr/demos/bin
    PERL_BADLANG (unset)
    SHELL=/bin/zsh


@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

Quoting jarausch@​numa1.igpm.rwth-aachen.de​:
:Please explain this to me if it's not a bug
:
:use strict;
:my $FRf = \&sin;
:print "sin(1)= ",sin(1),"\n"; # OK
:print "sin(1)= ",&$FRf(1),"\n"; # Undefined subroutine &main​::sin called at ...

You're referencing "sub sin", not the builtin sin().
There's no way to reference the builtin sin() that I know of.

Raphael

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

"Functions" like sin() and other builtins don't exist in symbol tables.
So you can't have references to them.

Solution​:

  $RF = sub { sin(@​_) };

Nat

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

On Mar 9, Raphael Manfredi said​:

You're referencing "sub sin", not the builtin sin().
There's no way to reference the builtin sin() that I know of.

While you can EXPLICITLY call builtin sin() via CORE​::sin(), you can't
make a reference to it, not even via \&CORE​::sin.

--
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve japhy@​pobox.com
http​://www.pobox.com/~japhy/ http​://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine http​://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc. http​://www.perlarchive.com/

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

Helmut Jarausch writes​:
| print "sin(1)= ",&$FRf(1),"\n"; # Undefined subroutine &main​::sin called at ...

Not a bug; this is documented. From man perlsub​:


  To unambiguously refer to the built-in form, precede the built-in
  name with the special package qualifier `CORE​::'. For example,
  saying `CORE​::open()' always refers to the built-in `open()',
  even if the current package has imported some other subroutine
  called `&open()' from elsewhere. Even though it looks like a
  regular function call, it isn't​: you can't take a reference to
  it, such as the incorrect `\&CORE​::open' might appear to produce.


However, a note to this effect in perlref may be appropriate. Suggested
patch below.

Mx.


*** perlref.pod.orig Thu Mar 9 14​:36​:06 2000
--- perlref.pod Thu Mar 9 14​:40​:15 2000
***************
*** 68,78 ****
  $coderef = \&handler;
  $globref = \*foo;
 
! It isn't possible to create a true reference to an IO handle (filehandle
! or dirhandle) using the backslash operator. The most you can get is a
! reference to a typeglob, which is actually a complete symbol table entry.
! But see the explanation of the C<*foo{THING}> syntax below. However,
! you can still use type globs and globrefs as though they were IO handles.
 
  =item 2.
 
--- 68,81 ----
  $coderef = \&handler;
  $globref = \*foo;
 
! It isn't possible to take a reference to a core function in the above
! (C<\&>) fashion, since core functions aren't quite the same as user
! code. Neither is it possible to create a true reference to an IO handle
! (filehandle or dirhandle) using the backslash operator. The most you
! can get is a reference to a typeglob, which is actually a complete
! symbol table entry. But see the explanation of the C<*foo{THING}>
! syntax below. However, you can still use type globs and globrefs as
! though they were IO handles.
 
  =item 2.
 

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

On Mar 9, Nathan Torkington said​:

Solution​:

$RF = sub { sin(@​_) };

That's not universally safe, because of the prototypes of some builtin
functions. splice(@​array, @​ARGS) won't work, for instance (if you were
expecting @​ARGS to be expanded to a list of its elements, that is).

--
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve japhy@​pobox.com
http​://www.pobox.com/~japhy/ http​://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine http​://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc. http​://www.perlarchive.com/

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

While you can EXPLICITLY call builtin sin() via CORE​::sin(), you can't
make a reference to it, not even via \&CORE​::sin.

sub CORE​::AUTOLOAD { ... }

--tom

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

On Mar 9, Nathan Torkington said​:

Solution​:

$RF = sub { sin(@​_) };

That's not universally safe, because of the prototypes of some builtin
functions. splice(@​array, @​ARGS) won't work, for instance (if you were
expecting @​ARGS to be expanded to a list of its elements, that is).

And, in that case, the answer is virtually always 0.841470984807897 :-)

  DB<1> $RF = sub { sin(@​_) };
  DB<2> print $RF->(1)
  0.841470984807897
  DB<3> print $RF->(2)
  0.841470984807897
  DB<4> print $RF->(-234234234)
  0.841470984807897

--tom

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

On Mar 9, Tom Christiansen said​:

$RF = sub { sin(@​_) };

That's not universally safe, because of the prototypes of some builtin
functions. splice(@​array, @​ARGS) won't work, for instance (if you were
expecting @​ARGS to be expanded to a list of its elements, that is).

And, in that case, the answer is virtually always 0.841470984807897 :-)

My point exactly. I did the test you did below.

 DB\<1> $RF = sub \{ sin\(@&#8203;\_\) \};
 DB\<2> print $RF\->\(1\)

0.841470984807897
DB<3> print $RF->(2)
0.841470984807897
DB<4> print $RF->(-234234234)
0.841470984807897

--
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve japhy@​pobox.com
http​://www.pobox.com/~japhy/ http​://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine http​://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc. http​://www.perlarchive.com/

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

Tom Christiansen writes​:

And, in that case, the answer is virtually always 0.841470984807897 :-)

D'oh. Well, it worked for his test case :-)

Ah well, time to go learn Python.

Nat
(the "it compiled" of the 90s)

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

Quoting tchrist@​chthon.perl.com​:
:>While you can EXPLICITLY call builtin sin() via CORE​::sin(), you can't
:>make a reference to it, not even via \&CORE​::sin.
:
:sub CORE​::AUTOLOAD { ... }

You lost me. What does autoloading have to do with builtin referencing?

Raphael

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

On Mar 9, Nathan Torkington said​:

Ah well, time to go learn Python.

I'm wanting to give a talk on an intro to Python at YAPC. :) And I also
plan to give a talk on an ordered hash data structure, and how to write
and use one in Perl, and its equiv. in Python.

Python is nice. I wish I used it more often.

--
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve japhy@​pobox.com
http​://www.pobox.com/~japhy/ http​://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine http​://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc. http​://www.perlarchive.com/

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

Quoting tchrist@​chthon.perl.com​:
​:>While you can EXPLICITLY call builtin sin() via CORE​::sin(), you can't
​:>make a reference to it, not even via \&CORE​::sin.
​:
​:sub CORE​::AUTOLOAD { ... }

You lost me. What does autoloading have to do with builtin referencing?

Hee.

  {
  package CORE;
  sub AUTOLOAD {
  our $AUTOLOAD;
  my $name = $AUTOLOAD;
  $name =~ s/^CORE​:://s || die "don't inherit me!";
  print "I see you want to call the $name function\n";
  }
  }

  my $rf = \&CORE​::sin;
  print &$rf(2);

But you need to call the proto and figure that. See Fatal.pm

--tom

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

Tom Christiansen <tchrist@​chthon.perl.com> wrote

And, in that case, the answer is virtually always 0.841470984807897 :-)

Except of course when it isn't. :-)

  DB<1> $RF = sub { sin(@​_) };
  DB<2> print $RF->(1..2)
0.909297426825682
  DB<3> print $RF->(1..3)
0.141120008059867
  DB<4>

Mike Guy

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

Tom Christiansen <tchrist@​chthon.perl.com> wrote

And, in that case, the answer is virtually always 0.841470984807897 :-)

Except of course when it isn't. :-)

Common use indicates that "virtually" never means "really" or
"truly". Instead, it means "not" (although they want you to think
of it as meaning "so nearly so as to disregard exceptions"). Try
the substitution of s/virtually/not/ for a less euphemistic reading
of derned near anything.

--tom

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

Except of course when it isn't. :-)

You little bugger. :)

DB<1> $RF = sub { sin(@​_) };
DB<2> print $RF->(1..2)
0.909297426825682
DB<3> print $RF->(1..3)
0.141120008059867

Damned scalar context.

--
MIDN 4/C PINYAN, NROTCURPI, US Naval Reserve japhy@​pobox.com
http​://www.pobox.com/~japhy/ http​://pinyaj.stu.rpi.edu/
PerlMonth - An Online Perl Magazine http​://www.perlmonth.com/
The Perl Archive - Articles, Forums, etc. http​://www.perlarchive.com/

@p5pRT
Copy link
Author

p5pRT commented Mar 9, 2000

From [Unknown Contact. See original ticket]

Tom Christiansen <tchrist@​chthon.perl.com> wrote

Tom Christiansen <tchrist@​chthon.perl.com> wrote

And, in that case, the answer is virtually always 0.841470984807897
:-)

Except of course when it isn't. :-)

Common use indicates that "virtually" never means "really" or
"truly". Instead, it means "not" (although they want you to think
of it as meaning "so nearly so as to disregard exceptions"). Try
the substitution of s/virtually/not/ for a less euphemistic reading
of derned near anything.

--tom

Ah, those multi-sibylline utter-stances of wistom ;-)

Worth reading again now that the fur stopped flying...

Wolfgang

@p5pRT
Copy link
Author

p5pRT commented Mar 10, 2000

From [Unknown Contact. See original ticket]

Quoting tchrist@​chthon.perl.com​:
: {
: package CORE;
: sub AUTOLOAD {
: our $AUTOLOAD;
: my $name = $AUTOLOAD;
: $name =~ s/^CORE​:://s || die "don't inherit me!";
: print "I see you want to call the $name function\n";
: }
: }
:
: my $rf = \&CORE​::sin;
: print &$rf(2);
:
:But you need to call the proto and figure that. See Fatal.pm

Brilliantly obscure! It frightens me. :-)

It does not work on my perl5.005_03 though (I replaced "our" with "use vars")​:

  I see you want to call the sin function
  rf = CODE(0xe8c68)
  sin(2) = 1

I perfectly understand the part up to the call of CORE​::AUTOLOAD, since
we're looking at CORE​::sin. Since AUTOLOAD does not do anything, the
reference returned is what exactly? It's not \&CORE​::AUTOLOAD.

Besides, de-referencing the CODE ref returned yields the results of a
function that cannot be the sin() we all know.

Raphael

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant