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

Following the tutorial here

gv_fetchpv("<", 0, SVt_PV) ,gv_fetchpv("^X", 0, SVt_PV) always return (GV *) 0x0

Is there any pre-requisitary to make it work?

Replies are listed 'Best First'.
Re: Why I can't fetch any special variables ?
by ikegami (Patriarch) on Sep 15, 2011 at 04:27 UTC

    $< doesn't get created automatically. Just like with any other variable, you need to use the G_ADD flag to create the var if it doesn't exist. The magic will be added automatically when it's created.

    As for $^X, "^X" is a convenient way of writing Ctrl-X. Yes, you can use a real Ctrl-X after the $ in the code, and in fact, that's the real variable name. You'll need to pass "\cX" (or whatever the C equivalent is) to gv_fetchpv.

      It works by using "\030"

      Isn't the second parameter of gv_fetchpv the default value?

      How do you specify the G_ADD flag?

        Isn't the second parameter of gv_fetchpv the default value?

        Say again?

        What does it mean for a parameter to be a default value? C doesn't have optional parameters, so parameters can't even have default values in C.

        How do you specify the G_ADD flag?

        Use it instead of zero for the second argument.

      $< doesn't get created automatically.

      Sure it does  perl -le " print defined $<"

      As for $^X, "^X" is a convenient way of writing Ctrl-X. Yes, you can use a real Ctrl-X after the $ in the code, and in fact, that's the real variable name. You'll need to pass "\cX"c (or whatever the C equivalent is) to gv_fetchpv.

      $^X is the actual name of the variable, so no, you don't need to use \cX, you can use $^X, the actual name of the variable

        Sure it does perl -le " print defined $<"

        No, your use of it created it. Try

        perl -E'say for keys %::'

        $^X is the actual name of the variable

        Why are you contradicting two people without proof or argument?

        >perl -E"say ${ $::{'^X'} }" >perl -E"say ${ $::{qq{\cX}} }" ...\perl.exe
Re: Why I can't fetch any special variables ?
by ikegami (Patriarch) on Sep 15, 2011 at 06:10 UTC

    By the way, get_sv("<", G_ADD) is a convenient shortcut (and better documented). Calls GvSV for you.

Re: Why I can't fetch any special variables ?
by Anonymous Monk on Sep 15, 2011 at 03:33 UTC
      The question is perfectly well phrased. It showed the code he used, the result he expected and the result he got. Runnable demonstrations are always nice, but not necessary.