{
    "mode": "perldoc",
    "parameter": "Scalar::Util",
    "section": "",
    "url": "https://www.chedong.com/phpMan.php/perldoc/Scalar%3A%3AUtil/json",
    "generated": "2026-05-30T08:19:53Z",
    "synopsis": "use Scalar::Util qw(blessed dualvar isdual readonly refaddr reftype\ntainted weaken isweak isvstring lookslikenumber\nsetprototype);\n# and other useful utils appearing below",
    "sections": {
        "NAME": {
            "content": "Scalar::Util - A selection of general-utility scalar subroutines\n",
            "subsections": []
        },
        "SYNOPSIS": {
            "content": "use Scalar::Util qw(blessed dualvar isdual readonly refaddr reftype\ntainted weaken isweak isvstring lookslikenumber\nsetprototype);\n# and other useful utils appearing below\n",
            "subsections": []
        },
        "DESCRIPTION": {
            "content": "\"Scalar::Util\" contains a selection of subroutines that people have\nexpressed would be nice to have in the perl core, but the usage would\nnot really be high enough to warrant the use of a keyword, and the size\nwould be so small that being individual extensions would be wasteful.\n\nBy default \"Scalar::Util\" does not export any subroutines.\n",
            "subsections": []
        },
        "FUNCTIONS FOR REFERENCES": {
            "content": "The following functions all perform some useful activity on reference\nvalues.\n\nblessed\nmy $pkg = blessed( $ref );\n\nIf $ref is a blessed reference, the name of the package that it is\nblessed into is returned. Otherwise \"undef\" is returned.\n\n$scalar = \"foo\";\n$class  = blessed $scalar;           # undef\n\n$ref    = [];\n$class  = blessed $ref;              # undef\n\n$obj    = bless [], \"Foo\";\n$class  = blessed $obj;              # \"Foo\"\n\nTake care when using this function simply as a truth test (such as in\n\"if(blessed $ref)...\") because the package name \"0\" is defined yet\nfalse.\n\nrefaddr\nmy $addr = refaddr( $ref );\n\nIf $ref is reference, the internal memory address of the referenced\nvalue is returned as a plain integer. Otherwise \"undef\" is returned.\n\n$addr = refaddr \"string\";           # undef\n$addr = refaddr \\$var;              # eg 12345678\n$addr = refaddr [];                 # eg 23456784\n\n$obj  = bless {}, \"Foo\";\n$addr = refaddr $obj;               # eg 88123488\n\nreftype\nmy $type = reftype( $ref );\n\nIf $ref is a reference, the basic Perl type of the variable referenced\nis returned as a plain string (such as \"ARRAY\" or \"HASH\"). Otherwise\n\"undef\" is returned.\n\n$type = reftype \"string\";           # undef\n$type = reftype \\$var;              # SCALAR\n$type = reftype [];                 # ARRAY\n\n$obj  = bless {}, \"Foo\";\n$type = reftype $obj;               # HASH\n\nNote that for internal reasons, all precompiled regexps (\"qr/.../\") are\nblessed references; thus \"ref()\" returns the package name string\n\"Regexp\" on these but \"reftype()\" will return the underlying C structure\ntype of \"REGEXP\" in all capitals.\n\nweaken\nweaken( $ref );\n\nThe lvalue $ref will be turned into a weak reference. This means that it\nwill not hold a reference count on the object it references. Also, when\nthe reference count on that object reaches zero, the reference will be\nset to undef. This function mutates the lvalue passed as its argument\nand returns no value.\n\nThis is useful for keeping copies of references, but you don't want to\nprevent the object being DESTROY-ed at its usual time.\n\n{\nmy $var;\n$ref = \\$var;\nweaken($ref);                     # Make $ref a weak reference\n}\n# $ref is now undef\n\nNote that if you take a copy of a scalar with a weakened reference, the\ncopy will be a strong reference.\n\nmy $var;\nmy $foo = \\$var;\nweaken($foo);                       # Make $foo a weak reference\nmy $bar = $foo;                     # $bar is now a strong reference\n\nThis may be less obvious in other situations, such as \"grep()\", for\ninstance when grepping through a list of weakened references to objects\nthat may have been destroyed already:\n\n@object = grep { defined } @object;\n\nThis will indeed remove all references to destroyed objects, but the\nremaining references to objects will be strong, causing the remaining\nobjects to never be destroyed because there is now always a strong\nreference to them in the @object array.\n\nunweaken\nunweaken( $ref );\n\n*Since version 1.36.*\n\nThe lvalue \"REF\" will be turned from a weak reference back into a normal\n(strong) reference again. This function mutates the lvalue passed as its\nargument and returns no value. This undoes the action performed by\n\"weaken\".\n\nThis function is slightly neater and more convenient than the\notherwise-equivalent code\n\nmy $tmp = $REF;\nundef $REF;\n$REF = $tmp;\n\n(because in particular, simply assigning a weak reference back to itself\ndoes not work to unweaken it; \"$REF = $REF\" does not work).\n\nisweak\nmy $weak = isweak( $ref );\n\nReturns true if $ref is a weak reference.\n\n$ref  = \\$foo;\n$weak = isweak($ref);               # false\nweaken($ref);\n$weak = isweak($ref);               # true\n\nNOTE: Copying a weak reference creates a normal, strong, reference.\n\n$copy = $ref;\n$weak = isweak($copy);              # false\n",
            "subsections": []
        },
        "OTHER FUNCTIONS": {
            "content": "dualvar\nmy $var = dualvar( $num, $string );\n\nReturns a scalar that has the value $num in a numeric context and the\nvalue $string in a string context.\n\n$foo = dualvar 10, \"Hello\";\n$num = $foo + 2;                    # 12\n$str = $foo . \" world\";             # Hello world\n\nisdual\nmy $dual = isdual( $var );\n\n*Since version 1.26.*\n\nIf $var is a scalar that has both numeric and string values, the result\nis true.\n\n$foo = dualvar 86, \"Nix\";\n$dual = isdual($foo);               # true\n\nNote that a scalar can be made to have both string and numeric content\nthrough standard operations:\n\n$foo = \"10\";\n$dual = isdual($foo);               # false\n$bar = $foo + 0;\n$dual = isdual($foo);               # true\n\nThe $! variable is commonly dual-valued, though it is also magical in\nother ways:\n\n$! = 1;\n$dual = isdual($!);                 # true\nprint(\"$!\\n\");                      # \"Operation not permitted\"\n\nCAUTION: This function is not as useful as it may seem. Dualvars are not\na distinct concept in Perl, but a standard internal construct of all\nscalar values. Almost any value could be considered as a dualvar by this\nfunction through the course of normal operations.\n\nisvstring\nmy $vstring = isvstring( $var );\n\nIf $var is a scalar which was coded as a vstring, the result is true.\n\n$vs   = v49.46.48;\n$fmt  = isvstring($vs) ? \"%vd\" : \"%s\"; #true\nprintf($fmt,$vs);\n\nlookslikenumber\nmy $isnum = lookslikenumber( $var );\n\nReturns true if perl thinks $var is a number. See \"lookslikenumber\" in\nperlapi.\n\nopenhandle\nmy $fh = openhandle( $fh );\n\nReturns $fh itself, if $fh may be used as a filehandle and is open, or\nif it is a tied handle. Otherwise \"undef\" is returned.\n\n$fh = openhandle(*STDIN);           # \\*STDIN\n$fh = openhandle(\\*STDIN);          # \\*STDIN\n$fh = openhandle(*NOTOPEN);         # undef\n$fh = openhandle(\"scalar\");         # undef\n\nreadonly\nmy $ro = readonly( $var );\n\nReturns true if $var is readonly.\n\nsub foo { readonly($[0]) }\n\n$readonly = foo($bar);              # false\n$readonly = foo(0);                 # true\n\nsetprototype\nmy $code = setprototype( $code, $prototype );\n\nSets the prototype of the function given by the $code reference, or\ndeletes it if $prototype is \"undef\". Returns the $code reference itself.\n\nsetprototype \\&foo, '$$';\n\ntainted\nmy $t = tainted( $var );\n\nReturn true if $var is tainted.\n\n$taint = tainted(\"constant\");       # false\n$taint = tainted($ENV{PWD});        # true if running under -T\n",
            "subsections": []
        },
        "DIAGNOSTICS": {
            "content": "Module use may give one of the following errors during import.\n\nVstrings are not implemented in this version of perl\nThe version of perl that you are using does not implement Vstrings,\nto use \"isvstring\" you will need to use a newer release of perl.\n",
            "subsections": []
        },
        "KNOWN BUGS": {
            "content": "There is a bug in perl5.6.0 with UV's that are >= 1<<31. This will show\nup as tests 8 and 9 of dualvar.t failing\n",
            "subsections": []
        },
        "SEE ALSO": {
            "content": "List::Util\n",
            "subsections": []
        },
        "COPYRIGHT": {
            "content": "Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights\nreserved. This program is free software; you can redistribute it and/or\nmodify it under the same terms as Perl itself.\n\nAdditionally \"weaken\" and \"isweak\" which are\n\nCopyright (c) 1999 Tuomas J. Lukka <lukka@iki.fi>. All rights reserved.\nThis program is free software; you can redistribute it and/or modify it\nunder the same terms as perl itself.\n\nCopyright (C) 2004, 2008 Matthijs van Duin. All rights reserved.\nCopyright (C) 2014 cPanel Inc. All rights reserved. This program is free\nsoftware; you can redistribute it and/or modify it under the same terms\nas Perl itself.\n",
            "subsections": []
        }
    },
    "summary": "Scalar::Util - A selection of general-utility scalar subroutines",
    "flags": [],
    "examples": [],
    "see_also": []
}