Skip Menu |
Report information
Id: 124239
Status: open
Priority: 0/
Queue: perl5

Owner: Nobody
Requestors: yoanlin93 [at] gmail.com
Cc:
AdminCc:

Operating System: (no value)
PatchStatus: HasPatch
Severity: low
Type: unknown
Perl Version: (no value)
Fixed In: (no value)



Date: Sat, 4 Apr 2015 02:09:58 +0800 (CST)
CC: yoanlin93 [...] gmail.com
From: yoanlin93 [...] gmail.com
Subject: [PATCH] 0f58732 Add a common code snippet of blessing an object into a package
To: perlbug [...] perl.org
Download (untitled) / with headers
text/plain 5.4k
This is a bug report for perl from yoanlin93@gmail.com, generated with the help of perlbug 1.40 running under perl 5.21.10. From 0f58732dc162f40820e3d9e9754d18b1653c46e3 Mon Sep 17 00:00:00 2001 From: c9s <yoanlin93@gmail.com> Date: Sat, 4 Apr 2015 02:08:31 +0800 Subject: [PATCH] Add a common code snippet of blessing an object into a package MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="------------2.1.2" This is a multi-part message in MIME format. --------------2.1.2 Content-Type: text/plain; charset=UTF-8; format=fixed Content-Transfer-Encoding: 8bit --- pod/perlguts.pod | 8 ++++++++ 1 file changed, 8 insertions(+) --------------2.1.2 Content-Type: text/x-patch; name="0001-Add-a-common-code-snippet-of-blessing-an-object-into.patch" Content-Transfer-Encoding: 8bit Content-Disposition: attachment; filename="0001-Add-a-common-code-snippet-of-blessing-an-object-into.patch" diff --git a/pod/perlguts.pod b/pod/perlguts.pod index 0fe61a8..f227ade 100644 --- a/pod/perlguts.pod +++ b/pod/perlguts.pod @@ -694,6 +694,14 @@ The C<sv> argument must be a reference value. The C<stash> argument specifies which class the reference will belong to. See L<Stashes and Globs> for information on converting class names into stashes. +The code below is a common snippet to bless an object into a package in the C<new> +method of a package: + + STRLEN classname_len; + char * classname = SvPVbyte(ST(0), classname_len); + HV * stash = gv_stashpvn(classname, classname_len, 0); + sv_bless(obj, stash); // the obj here is usually a HV + /* Still under construction */ The following function upgrades rv to reference if not already one. --------------2.1.2-- --- Flags: category=docs severity=low --- Site configuration information for perl 5.21.10: Configured by c9s at Fri Feb 27 12:59:15 CST 2015. Summary of my perl5 (revision 5 version 21 subversion 10) configuration: Commit id: e2bb786192adfa315ea974b5f630d7040aa6f6ac Platform: osname=darwin, osvers=14.1.0, archname=darwin-2level uname='darwin c9smba.local 14.1.0 darwin kernel version 14.1.0: mon dec 22 23:10:38 pst 2014; root:xnu-2782.10.72~2release_x86_64 x86_64 ' config_args='-des -Dusedevel -Dprefix=/Users/c9s/perl-blead' hint=recommended, useposix=true, d_sigaction=define useithreads=undef, usemultiplicity=undef use64bitint=define, use64bitall=define, uselongdouble=undef usemymalloc=n, bincompat5005=undef Compiler: cc='cc', ccflags ='-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -I/opt/local/include', optimize='-O3', cppflags='-fno-common -DPERL_DARWIN -fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include -I/opt/local/include' ccversion='', gccversion='4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.54)', gccosandvers='' intsize=4, longsize=8, ptrsize=8, doublesize=8, byteorder=12345678, doublekind=3 d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=16, longdblkind=3 ivtype='long', ivsize=8, nvtype='double', nvsize=8, Off_t='off_t', lseeksize=8 alignbytes=8, prototype=define Linker and Libraries: ld='env MACOSX_DEPLOYMENT_TARGET=10.3 cc', ldflags =' -fstack-protector -L/usr/local/lib -L/opt/local/lib' libpth=/usr/local/lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/bin/../lib/clang/6.0/lib /Applications/Xcode.app/Contents/Developer/Toolchains/XcodeDefault.xctoolchain/usr/lib /usr/lib /opt/local/lib libs=-lpthread -lgdbm -ldbm -ldl -lm -lutil -lc perllibs=-lpthread -ldl -lm -lutil -lc libc=, so=dylib, useshrplib=false, libperl=libperl.a gnulibc_version='' Dynamic Linking: dlsrc=dl_dlopen.xs, dlext=bundle, d_dlsymun=undef, ccdlflags=' ' cccdlflags=' ', lddlflags=' -bundle -undefined dynamic_lookup -L/usr/local/lib -L/opt/local/lib -fstack-protector' --- @INC for perl 5.21.10: lib /Users/c9s/perl-blead/lib/site_perl/5.21.10/darwin-2level /Users/c9s/perl-blead/lib/site_perl/5.21.10 /Users/c9s/perl-blead/lib/5.21.10/darwin-2level /Users/c9s/perl-blead/lib/5.21.10 . --- Environment for perl 5.21.10: DYLD_LIBRARY_PATH (unset) HOME=/Users/c9s LANG=en_US.UTF-8 LANGUAGE (unset) LC_ALL=en_US.UTF-8 LC_CTYPE=UTF-8 LD_LIBRARY_PATH (unset) LOGDIR (unset) PATH=/Users/c9s/perl5/perlbrew/bin:/Users/c9s/perl5/perlbrew/perls/perl-5.18.2/bin:/Users/c9s/.phpbrew/php/php-5.5.17/bin:/Users/c9s/.rvm/bin:/Users/c9s/src/google/depot_tools:/usr/local/texlive/2013/bin/x86_64-darwin:/Users/c9s/Library/Haskell/bin:/opt/local/Library/Frameworks/Python.framework/Versions/2.7/bin:/Users/c9s/.phpbrew/bin:/opt/local/lib/postgresql92/bin:/opt/local/bin:/opt/local/sbin:/Users/c9s/bin:/Users/c9s/Library/Haskell/bin:/opt/local/bin:/opt/local/apache2/bin:/Users/c9s/.rvm/bin:/Users/c9s/.rvm/gems/ruby-2.1.1/bin:/Users/c9s/.rvm/gems/ruby-2.1.1@global/bin:/Users/c9s/.rvm/rubies/ruby-2.1.1/bin:/Users/c9s/bin:/usr/local/bin:/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/git/bin:/usr/local/go/bin:/usr/local/MacGPG2/bin:/usr/texbin:/Users/c9s/.rvm/bin PERLBREW_BASHRC_VERSION=0.73 PERLBREW_HOME=/Users/c9s/.perlbrew PERLBREW_MANPATH=/Users/c9s/perl5/perlbrew/perls/perl-5.18.2/man PERLBREW_PATH=/Users/c9s/perl5/perlbrew/bin:/Users/c9s/perl5/perlbrew/perls/perl-5.18.2/bin PERLBREW_PERL=perl-5.18.2 PERLBREW_ROOT=/Users/c9s/perl5/perlbrew PERLBREW_VERSION=0.73 PERLDOC=-otext PERL_BADLANG (unset) PERL_CPANM_OPT=-n --mirror http://cpan.nctu.edu.tw/ PERL_MM_USE_DEFAULT=1 SHELL=/bin/zsh
RT-Send-CC: perl5-porters [...] perl.org
Download (untitled) / with headers
text/plain 2.2k
On Fri Apr 03 11:10:17 2015, yoanlin93@gmail.com wrote: Show quoted text
> > This is a bug report for perl from yoanlin93@gmail.com, > generated with the help of perlbug 1.40 running under perl 5.21.10. > > From 0f58732dc162f40820e3d9e9754d18b1653c46e3 Mon Sep 17 00:00:00 2001 > From: c9s <yoanlin93@gmail.com> > Date: Sat, 4 Apr 2015 02:08:31 +0800 > Subject: [PATCH] Add a common code snippet of blessing an object into > a > package > MIME-Version: 1.0 > Content-Type: multipart/mixed; boundary="------------2.1.2" > > This is a multi-part message in MIME format. > --------------2.1.2 > Content-Type: text/plain; charset=UTF-8; format=fixed > Content-Transfer-Encoding: 8bit > > --- > pod/perlguts.pod | 8 ++++++++ > 1 file changed, 8 insertions(+) > > > --------------2.1.2 > Content-Type: text/x-patch; name="0001-Add-a-common-code-snippet-of- > blessing-an-object-into.patch" > Content-Transfer-Encoding: 8bit > Content-Disposition: attachment; filename="0001-Add-a-common-code- > snippet-of-blessing-an-object-into.patch" > > diff --git a/pod/perlguts.pod b/pod/perlguts.pod > index 0fe61a8..f227ade 100644 > --- a/pod/perlguts.pod > +++ b/pod/perlguts.pod > @@ -694,6 +694,14 @@ The C<sv> argument must be a reference value. > The C<stash> argument > specifies which class the reference will belong to. See > L<Stashes and Globs> for information on converting class names into > stashes. > > +The code below is a common snippet to bless an object into a package > in the C<new> > +method of a package: > + > + STRLEN classname_len; > + char * classname = SvPVbyte(ST(0), classname_len); > + HV * stash = gv_stashpvn(classname, classname_len, 0); > + sv_bless(obj, stash); // the obj here is usually a HV > + > /* Still under construction */ > > The following function upgrades rv to reference if not already one. > > --------------2.1.2-- > >
Since we're past the point in the release cycle where we would be making changes in the Perl 5 core code, my feeling is that we shouldn't be making unnecessary changes in the documentation of the core code. So I've marked this patch for consideration for 5.23.1. Nonetheless, it would be good to get feedback from contributors familiar with perlguts and XS on the merits of this documentation change. Thank you very much. -- James E Keenan (jkeenan@cpan.org)
RT-Send-CC: perl5-porters [...] perl.org
On Fri Apr 03 11:10:17 2015, yoanlin93@gmail.com wrote: Show quoted text
> Subject: [PATCH] Add a common code snippet of blessing an object into
There's a number of problems with this patch. Show quoted text
> + STRLEN classname_len; > + char * classname = SvPVbyte(ST(0), classname_len); > + HV * stash = gv_stashpvn(classname, classname_len, 0); > + sv_bless(obj, stash); // the obj here is usually a HV
First, since you use the combination of SvPVbytes + gv_stashpvn with zero flags function calls, you throw away any UTF8-ness on original argument. Second, while this code is correct for non-utf8 class names, it's not the best way to fetch a stash pointer. That was the way on older perls, but since this is a documentation for a fresh release, advice readers for the fastest API available - gv_stashsv. It takes advantage of argument on the stack being possibly a shared string already, and does all UTF8 stuff for you. And the last is the wording of the comment on the last line. First argument to the sv_bless can't be non-reference, so it's not a HV*, it's a SV* holding reference to, commonly, HV*.


This service is sponsored and maintained by Best Practical Solutions and runs on Perl.org infrastructure.

For issues related to this RT instance (aka "perlbug"), please contact perlbug-admin at perl.org