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

Remove references to ref($proto)||$proto from perl*.pod #7418

Closed
p5pRT opened this issue Jul 9, 2004 · 11 comments
Closed

Remove references to ref($proto)||$proto from perl*.pod #7418

p5pRT opened this issue Jul 9, 2004 · 11 comments

Comments

@p5pRT
Copy link

p5pRT commented Jul 9, 2004

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

Searchable as RT30663$

@p5pRT
Copy link
Author

p5pRT commented Jul 9, 2004

From andy-baby@petdance.com

Created by andy-baby@petdance.com

perlobj (and maybe others) advertise the constructor method of

sub new {
  my $proto = shift;
  my $class = ref($proto) || $proto;

This is 99% of the time unnecessary. People copy & paste it into their constructors without understanding why.

The standard way should be​:

sub new {
  my $class = shift;

and optionally explain why you'd want to do the old way.

Perl Info

Flags:
    category=docs
    severity=medium

Site configuration information for perl v5.8.4:

Configured by andy at Sat Apr 24 08:21:07 CDT 2004.

Summary of my perl5 (revision 5 version 8 subversion 4) configuration:
  Platform:
    osname=linux, osvers=2.4.20-8, archname=i686-linux
    uname='linux baby 2.4.20-8 #1 thu mar 13 17:54:28 est 2003 i686 i686 i386 gnulinux '
    config_args='-des -Dprefix=/usr/local -Dcf_email=andy-baby@petdance.com -Dperladmin=andy-baby@petdance.com'
    hint=recommended, useposix=true, d_sigaction=define
    usethreads=undef use5005threads=undef useithreads=undef usemultiplicity=undef
    useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
    use64bitint=undef use64bitall=undef uselongdouble=undef
    usemymalloc=n, bincompat5005=undef
  Compiler:
    cc='cc', ccflags ='-fno-strict-aliasing -I/usr/local/include -D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -I/usr/include/gdbm',
    optimize='-O2',
    cppflags='-fno-strict-aliasing -I/usr/local/include -I/usr/include/gdbm'
    ccversion='', gccversion='3.2.2 20030222 (Red Hat Linux 3.2.2-5)', gccosandvers=''
    intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=1234
    d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
    ivtype='long', ivsize=4, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8
    alignbytes=4, prototype=define
  Linker and Libraries:
    ld='cc', ldflags =' -L/usr/local/lib'
    libpth=/usr/local/lib /lib /usr/lib
    libs=-lnsl -lgdbm -ldb -ldl -lm -lcrypt -lutil -lc
    perllibs=-lnsl -ldl -lm -lcrypt -lutil -lc
    libc=/lib/libc-2.3.2.so, so=so, useshrplib=false, libperl=libperl.a
    gnulibc_version='2.3.2'
  Dynamic Linking:
    dlsrc=dl_dlopen.xs, dlext=so, d_dlsymun=undef, ccdlflags='-Wl,-E'
    cccdlflags='-fpic', lddlflags='-shared -L/usr/local/lib'

Locally applied patches:
    


@INC for perl v5.8.4:
    /usr/local/lib/perl5/5.8.4/i686-linux
    /usr/local/lib/perl5/5.8.4
    /usr/local/lib/perl5/site_perl/5.8.4/i686-linux
    /usr/local/lib/perl5/site_perl/5.8.4
    /usr/local/lib/perl5/site_perl/5.8.1/i686-linux
    /usr/local/lib/perl5/site_perl/5.8.1
    /usr/local/lib/perl5/site_perl
    .


Environment for perl v5.8.4:
    HOME=/home/andy
    LANG=en_US.UTF-8
    LANGUAGE (unset)
    LD_LIBRARY_PATH (unset)
    LOGDIR (unset)
    PATH=/usr/local/bin:/bin:/usr/bin:/usr/X11R6/bin:/home/andy/bin:/home/andy/play
    PERL_BADLANG (unset)
    SHELL=/bin/bash

@p5pRT
Copy link
Author

p5pRT commented Jul 11, 2004

From @schwern

On Fri, Jul 09, 2004 at 10​:15​:45PM -0000, andy-baby @​ petdance. com wrote​:

perlobj (and maybe others) advertise the constructor method of

sub new {
my $proto = shift;
my $class = ref($proto) || $proto;

This is 99% of the time unnecessary. People copy & paste it into their constructors without understanding why.

The standard way should be​:

sub new {
my $class = shift;

and optionally explain why you'd want to do the old way.

I used to be in the ref $proto || $proto camp because I felt it would be
useful to be able to construct a new instance of an object without having
to first get its class.

But here's how many times I've used $obj->new​: Zero.

Andy's right, its not worth the confusion in the docs and it sidesteps
the whole "what should $obj->new do?" discussion.

--
Michael G Schwern schwern@​pobox.com http​://www.pobox.com/~schwern/
Milk is timeless.

@p5pRT
Copy link
Author

p5pRT commented Jul 11, 2004

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Jul 11, 2004

From @petdance

On Sat, Jul 10, 2004 at 11​:50​:11PM -0400, Michael G Schwern (schwern@​pobox.com) wrote​:

I used to be in the ref $proto || $proto camp because I felt it would be
useful to be able to construct a new instance of an object without having
to first get its class.

But here's how many times I've used $obj->new​: Zero.

If ever there was a case of YAGNI, this is it. :-)

xoa

--
Andy Lester => andy@​petdance.com => www.petdance.com => AIM​:petdance

@p5pRT
Copy link
Author

p5pRT commented Jul 11, 2004

From perl@nevcal.com

On approximately 7/10/2004 8​:57 PM, came the following characters from
the keyboard of Andy Lester​:

If ever there was a case of YAGNI, this is it. :-)

In perl land, shouldn't we call that TOTMWTDI ?? :)

Nope, even Google can't find There's One Too Many Ways To Do It :)

--
Glenn -- http​://nevcal.com/

The best part about procrastination is that you are never bored,
because you have all kinds of things that you should be doing.

@p5pRT
Copy link
Author

p5pRT commented Jul 18, 2007

@petdance - Status changed from 'open' to 'new'

@p5pRT
Copy link
Author

p5pRT commented Nov 26, 2011

From @jkeenan

On Fri Jul 09 15​:15​:45 2004, andy-baby@​petdance.com wrote​:

perlobj (and maybe others) advertise the constructor method of

sub new {
my $proto = shift;
my $class = ref($proto) || $proto;

This is what I currently find in 'pod/perlobj.pod'​:
###
Or if you expect people to call not just "CLASS->new()" but also
"$obj->new()", then use something like the following. ...

  sub new {
  my $this = shift;
  my $class = ref($this) || $this;
  my $self = {};
  bless $self, $class;
  $self->initialize();
  return $self;
  }
###
And this is what I currently find in 'pod/perlmodlib.PL'​:
###
or even this if you'd like it to be used as either a static
or a virtual method.

sub new {
  my $self = shift;
  my $class = ref($self) || $self;
  return bless {}, $class;
}
###
(That 'either a static or a virtual method' doesn't seem very Perlish.)

And this is what I find in 'pod/perldiag.pod'​:
###
=item Attempt to bless into a reference

(F) The CLASSNAME argument to the bless() operator is expected to be
the name of the package to bless the resulting object into. You've
supplied instead a reference to something​: perhaps you wrote

  bless $self, $proto;

when you intended

  bless $self, ref($proto) || $proto;
###

@p5pRT
Copy link
Author

p5pRT commented Nov 26, 2011

The RT System itself - Status changed from 'new' to 'open'

@p5pRT
Copy link
Author

p5pRT commented Nov 26, 2011

From @b2gills

On Fri, Nov 25, 2011 at 8​:45 PM, James E Keenan via RT
<perlbug-followup@​perl.org> wrote​:

On Fri Jul 09 15​:15​:45 2004, andy-baby@​petdance.com wrote​:

perlobj (and maybe others) advertise the constructor method of

sub new {
    my $proto = shift;
    my $class = ref($proto) || $proto;

This is what I currently find in 'pod/perlobj.pod'​:
###
Or if you expect people to call not just "CLASS->new()" but also
"$obj->new()", then use something like the following. ...

          sub new {
              my $this = shift;
              my $class = ref($this) || $this;
              my $self = {};
              bless $self, $class;
              $self->initialize();
              return $self;
          }
###
And this is what I currently find in 'pod/perlmodlib.PL'​:
###
or even this if you'd like it to be used as either a static
or a virtual method.

 sub new {
    my $self  = shift;
    my $class = ref($self) || $self;
    return bless {}, $class;
 }
###
(That 'either a static or a virtual method' doesn't seem very Perlish.)

And this is what I find in 'pod/perldiag.pod'​:
###
=item Attempt to bless into a reference

(F) The CLASSNAME argument to the bless() operator is expected to be
the name of the package to bless the resulting object into. You've
supplied instead a reference to something​: perhaps you wrote

   bless $self, $proto;

when you intended

   bless $self, ref($proto) || $proto;
###

This is the simplest way to use a constructor as a method on an object.

% perl -E'$a=bless[]; say $a; $a=bless[],$a; say $a'
main=ARRAY(0x1c94868)
Attempt to bless into a reference at -e line 1.

@p5pRT
Copy link
Author

p5pRT commented Nov 26, 2011

From @rjbs

I think Jim was looking at 5.14, rather than blead, actually. perlobj
has been entirely rewritten in blead, and does not show the ref($self) form.

I think this request has been addressed.

@p5pRT
Copy link
Author

p5pRT commented Nov 26, 2011

@rjbs - Status changed from 'open' to 'resolved'

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