Beefy Boxes and Bandwidth Generously Provided by pair Networks
"be consistent"
 
PerlMonks  

Question about Perl refs and basic types

by cheekuperl (Monk)
on Jul 18, 2012 at 15:08 UTC ( [id://982460]=perlquestion: print w/replies, xml ) Need Help??

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

I was wondering about the possible return values of the ref function until I came across the list in ref. I also verified that the 11 return values are printed using the following code.
format = . sub fun { print "\nThis is fun"; } $packRef={}; bless $packRef,"NONSENSE"; $scalarRef=\$_; #1 $arrRef=\@ARGV; #2 $hashRef=\%ENV; #3 $codeRef=\&fun; #4 $refRef=\$scalarRef; #5 $globRef=\*STDIN; #6 $lvalueRef=\ substr ("learning perl",0,4); #7 $regexpRef= qr/something/; #8 $formatRef= *STDOUT{FORMAT};#9 $ioRef= *STDOUT{IO}; #10 $vstringRef= \v5.10.10; #11 print "\n1. scalarref is ". ref($scalarRef); print "\n2. arrRef is ". ref($arrRef); print "\n3. hashRef is ". ref($hashRef); print "\n4. codeRef is ". ref($codeRef); print "\n5. refRef is ". ref($refRef); print "\n6. globRef is ". ref($globRef); print "\n7. lvalueRef is ". ref($lvalueRef); print "\n8. regexpRef is ". ref($regexpRef); print "\n9. formatRef is ". ref($formatRef); print "\n10. ioRef is ". ref($ioRef); print "\n11. vstringRef is ". ref($vstringRef); #prints SCALAR print "\n12. packRef is ". ref($packRef);
However, I could not get the vstringRef to print VSTRING. Any clues why #11 above shows SCALAR?

Secondly, I have always come across the statement
Perl has three basic types : scalars, arrays, hashes
However, I have not found a definitive for this in perldocs (I was trying to find a list like there was one for 11 possible return types of ref function).
Could anyone elaborate why the return values of ref can't be called as data types in Perl?

Replies are listed 'Best First'.
Re: Question about Perl refs and basic types
by tobyink (Canon) on Jul 18, 2012 at 15:29 UTC

    This works for me:

    $ perl -E'say ref do { \(my $o = v1.2) }' VSTRING
    perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'
Re: Question about Perl refs and basic types
by RedElk (Hermit) on Jul 18, 2012 at 18:20 UTC

    Not sure about the vstringRef issue, but I think this could be the perldocs section you want (first paragraph).

      Thanks a lot!
      All ye, repeat after me : Perl has only three data types - no more, no less :) :)
Re: Question about Perl refs and basic types
by Anonymous Monk on Jul 19, 2012 at 00:35 UTC

    However, I could not get the vstringRef to print VSTRING. Any clues why #11 above shows SCALAR?

    Your version of perl is too old -- reading perldocs is great, but match them to your version of perl

      You are spot on!
      I did
      printf "version is v%vd\n", $^V; # Perl's version
      and got v5.8.9. Then I checked the perldoc ref for 5.8.9 and VSTRING is not present.
Re: Question about Perl refs and basic types
by sundialsvc4 (Abbot) on Jul 18, 2012 at 15:24 UTC

    Well, because a reference can “refer to” more things than just “data.”   References refer to things, if you will:   to hashes, to blocks of code, to regular expressions, and so on.

    Technically, if you want to get to the perlguts of the matter, within the interpreter there are various kinds of memory-blocks and each of these has (among other things) a reference-count.   A “reference,” then, is one of those kinds of memory-blocks, whose purpose is to act as an indirect reference to something else.   Each memory block has a type-indicator which says what kind of block it is, and that’s basically what drives the output of ref().

    Why does it say SCALAR?   Dunno.   But, 11 out of 12 ain’t bad ...

      That is fine. But I wish to see some sort of "proof" in perldocs that says there are only three types-no more no less. :)

        That is fine. But I wish to see some sort of "proof" in perldocs that says there are only three types-no more no less. :)

        The proof is in the pudding :) UTSL

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others romping around the Monastery: (2)
As of 2024-04-26 03:53 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found