1 #define NEED_newSVpvn_flags
5 mop_deconstruct_variable_name(pTHX_ SV* const variable, svtype* const type, const char** const type_name) {
8 if(SvROK(variable) && SvTYPE(SvRV(variable)) == SVt_PVHV){
9 /* e.g. variable = { type => "SCALAR", name => "foo" } */
10 HV* const hv = (HV*)SvRV(variable);
15 svp = hv_fetchs(hv, "name", FALSE);
16 if(!(svp && SvOK(*svp))){
17 croak("You must pass a variable name");
20 pv = SvPV_const(name, len);
22 croak("You must pass a variable name");
25 svp = hv_fetchs(hv, "type", FALSE);
26 if(!(svp && SvOK(*svp))) {
27 croak("You must pass a variable type");
29 pv = SvPV_nolen_const(*svp);
30 if(strEQ(pv, "SCALAR")){
31 *type = SVt_PV; /* for all the type of scalars */
33 else if(strEQ(pv, "ARRAY")){
36 else if(strEQ(pv, "HASH")){
39 else if(strEQ(pv, "CODE")){
42 else if(strEQ(pv, "GLOB")){
45 else if(strEQ(pv, "IO")){
49 croak("I do not recognize that type '%s'", pv);
56 /* e.g. variable = '$foo' */
58 croak("You must pass a variable name");
60 pv = SvPV_const(variable, len);
62 croak("You must pass a variable name including a sigil");
67 *type = SVt_PV; /* for all the types of scalars */
68 *type_name = "SCALAR";
87 croak("I do not recognize that sigil '%c'", pv[0]);
90 name = newSVpvn_share(pv+1, len-1, 0U);
97 MODULE = Class::MOP::Package PACKAGE = Class::MOP::Package
102 get_all_package_symbols(self, filter=TYPE_FILTER_NONE)
110 if ( ! SvROK(self) ) {
111 die("Cannot call get_all_package_symbols as a class method");
114 if (GIMME_V == G_VOID) {
120 if ( (he = hv_fetch_ent((HV *)SvRV(self), KEY_FOR(package), 0, HASH_FOR(package))) ) {
121 stash = gv_stashsv(HeVAL(he), 0);
129 symbols = mop_get_all_package_symbols(stash, filter);
130 PUSHs(sv_2mortal(newRV_noinc((SV *)symbols)));
133 INSTALL_SIMPLE_READER_WITH_KEY(Package, name, package);
136 add_package_symbol(SV* self, SV* variable, SV* ref = &PL_sv_undef)
139 const char* type_name;
144 var_name = mop_deconstruct_variable_name(aTHX_ variable, &type, &type_name);
146 package_name = mop_call0(aTHX_ self, KEY_FOR(name));
147 if(!SvOK(package_name)){
148 croak("name() did not return a defined value");
150 fq_name = newSVpvf("%"SVf"::%"SVf, package_name, var_name);
153 if(SvOK(ref)){ /* set */
157 ref = newRV_noinc(newSVsv(ref));
161 else if(!(SvROK(ref) && SvTYPE(SvRV(ref)) == type)){
162 croak("You must pass a reference of %s for the value of %s", type_name, GvNAME(CvGV(cv)));
164 gv = gv_fetchsv(fq_name, GV_ADDMULTI, type);
166 if(type == SVt_PVCV && GvCV(gv)){
167 /* XXX: should introduce an option { redefine => 1 } ? */
168 SvREFCNT_dec(GvCV(gv));
171 sv_setsv_mg((SV*)gv, ref); /* *glob = $ref */
175 GV* const gv = gv_fetchsv(fq_name, GV_ADDMULTI, type);
198 croak("NOT REACHED");
204 RETVAL = sv_2mortal(newRV_inc(sv));
207 RETVAL = &PL_sv_undef;
213 get_package_symbol(SV* self, SV* variable)
215 get_package_symbol = GV_ADDMULTI
216 has_package_symbol = 0
219 const char* type_name;
226 var_name = mop_deconstruct_variable_name(aTHX_ variable, &type, &type_name);
228 package_name = mop_call0(aTHX_ self, KEY_FOR(name));
229 if(!SvOK(package_name)){
230 croak("name() did not return a defined value");
232 fq_name = newSVpvf("%"SVf"::%"SVf, package_name, var_name);
235 gv = gv_fetchsv(fq_name, ix, type);
236 if(!gv){ /* no symbol in has_package_symbol() */
260 croak("NOT REACHED");
265 if(!ix){ /* has_package_symbol */
270 RETVAL = newRV_inc(sv);
273 RETVAL = &PL_sv_undef;