Beefy Boxes and Bandwidth Generously Provided by pair Networks
Come for the quick hacks, stay for the epiphanies.
 
PerlMonks  

how to get stash name of SV?

by xiaoyafeng (Deacon)
on Oct 11, 2019 at 07:55 UTC ( [id://11107336]=perlquestion: print w/replies, xml ) Need Help??

xiaoyafeng has asked for the wisdom of the Perl Monks concerning the following question:

As described in perlapi, you can get stash of an SV by SvSTASH, but the following code always crashes!

use strict; use warnings; package dd; our $dd = "hello world"; main_pp(\$dd); use Inline Config => BUILD_NOISY => 1, CLEAN_AFTER_BUILD => 0; use Inline 'C' => Config => ccflagsex => '-g'; use Inline 'C' => <<'CODE'; int main_pp(SV* rx){ SV* x = get_sv("dd::dd", 0); /* below 2 statement both crash! */ // printf("stash is %s \n", HvNAME(SvSTASH((SV*)SvRV(rx)))); // printf("stash is %s \n", HvNAME(SvSTASH(x))); printf("pv is %s \n", (SvPVX(x))); HV* h = gv_stashpv("dd", 0); HE* hash_entry; SV* sv_key; SV* sv_val; int num_keys = hv_iterinit(h); for (int i = 0; i < num_keys; i++) { hash_entry = hv_iternext(h); sv_key = hv_iterkeysv(hash_entry); sv_val = hv_iterval(h, hash_entry); printf("%s => %s\n", SvPV(sv_key, PL_na), SvPV(sv_val, PL_na)); } return 0; } CODE
Please help! Thanks in advance!





I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Replies are listed 'Best First'.
Re: how get stash name of SV?
by dave_the_m (Monsignor) on Oct 11, 2019 at 10:36 UTC
    The SvSTASH() field of an SV typically indicates the class that an SV was blessed into. The field is only physically present if the SV is of type >= SVt_PVMG, and only contains a valid/useful value under certain circumstances (such as SvOBJECT(sv) being true).

    It isn't a general field of an SV indicating what stash a package variable is located in.

    Dave.

      So If I understand correctly, the name of SvSTASH is a little bit misleading. it should be SvCLASS or not SvSTASH?




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

        So If I understand correctly, the name of SvSTASH is a little bit misleading. it should be SvCLASS or not SvSTASH
        In an ideal world, perhaps yes.

        Dave.

        I suspect that the choice of name for that field is related to how Perl 5 objects are implemented internally: an object uses some package (internally represented as a "stash", which is a hash storing typeglobs) as its vtable. Perl does not really have classes as distinct objects, so the name SvSTASH doubtless made perfect sense when the feature was added to what became Perl 5.

Re: how get stash name of SV?
by jcb (Parson) on Oct 11, 2019 at 23:43 UTC

    If you are trying to find the package a global variable came from, I do not think that an SV stores that information. Actually, it cannot store that information, since the same SV can be aliased from multiple packages, as Exporter does. You probably want the GV (corresponding to the typglob *VAR) instead of an SV (corresponding to the scalar $VAR).

    And I see a few English mistakes, so I will point them out to help you learn as you have asked:

    "how get stash name" in subject
    You need the infinitive verb form here: "how to get stash name".
    "a SV"
    If "SV" is pronounced "ess-vee", then you should write "an SV" rather than "a SV" — the rule in English is to use "a" if the following phoneme is a consonant and "an" if the following phoneme is a vowel. (Yes, acronyms and abbreviations can "make hash" of this at times: "an SV" but "a Scalar Value" if you expand the abbreviation as you read it.)
    "my below code always crash"
    The most important part here is the verb number: "code" is singular, so "crash" should be "crashes". (Yes, many English verbs require essentially the same suffix used for plural nouns if the subject of the sentence is a singular noun.) The word order is also "odd", although understandable; in normal English this should be written as "my code below always crashes", "the code below always crashes", or perhaps "the following code always crashes". Note that there is also a certain "distance" in the later examples — you wrote the code, but the code is to some extent independent of its author — especially when it does not work! :-)
    "Thank in advance!"
    Verb number again — this should be "Thanks in advance!"

      "Thank in advance!"
      Verb number again — this should be "Thanks in advance!"

      In this case, it's a noun: "(many/thousand) thanks" as opposed to "(I/we) thank you"
      Thanks for your correction and detailed explanation! I've corrected the mistakes in my post, and hope I hope I will not make such mistakes any more! Thanks!




      I am trying to improve my English skills, if you see a mistake please feel free to reply or /msg me a correction

Re: how get stash name of SV?
by Anonymous Monk on Oct 11, 2019 at 12:00 UTC

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://11107336]
Front-paged by Corion
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others lurking in the Monastery: (4)
As of 2024-03-29 00:35 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found