Perl 5 version 22.4 documentation

Scalar::Util

NAME

Scalar::Util - A selection of general-utility scalar subroutines

SYNOPSIS

  1. use Scalar::Util qw(blessed dualvar isdual readonly refaddr reftype
  2. tainted weaken isweak isvstring looks_like_number
  3. set_prototype);
  4. # and other useful utils appearing below

DESCRIPTION

Scalar::Util contains a selection of subroutines that people have expressed would be nice to have in the perl core, but the usage would not really be high enough to warrant the use of a keyword, and the size so small such that being individual extensions would be wasteful.

By default Scalar::Util does not export any subroutines.

FUNCTIONS FOR REFERENCES

The following functions all perform some useful activity on reference values.

blessed

  1. my $pkg = blessed( $ref );

If $ref is a blessed reference the name of the package that it is blessed into is returned. Otherwise undef is returned.

  1. $scalar = "foo";
  2. $class = blessed $scalar; # undef
  3. $ref = [];
  4. $class = blessed $ref; # undef
  5. $obj = bless [], "Foo";
  6. $class = blessed $obj; # "Foo"

Take care when using this function simply as a truth test (such as in if(blessed $ref)... ) because the package name "0" is defined yet false.

refaddr

  1. my $addr = refaddr( $ref );

If $ref is reference the internal memory address of the referenced value is returned as a plain integer. Otherwise undef is returned.

  1. $addr = refaddr "string"; # undef
  2. $addr = refaddr \$var; # eg 12345678
  3. $addr = refaddr []; # eg 23456784
  4. $obj = bless {}, "Foo";
  5. $addr = refaddr $obj; # eg 88123488

reftype

  1. my $type = reftype( $ref );

If $ref is a reference the basic Perl type of the variable referenced is returned as a plain string (such as ARRAY or HASH ). Otherwise undef is returned.

  1. $type = reftype "string"; # undef
  2. $type = reftype \$var; # SCALAR
  3. $type = reftype []; # ARRAY
  4. $obj = bless {}, "Foo";
  5. $type = reftype $obj; # HASH

weaken

  1. weaken( $ref );

The lvalue $ref will be turned into a weak reference. This means that it will not hold a reference count on the object it references. Also when the reference count on that object reaches zero, the reference will be set to undef. This function mutates the lvalue passed as its argument and returns no value.

This is useful for keeping copies of references, but you don't want to prevent the object being DESTROY-ed at its usual time.

  1. {
  2. my $var;
  3. $ref = \$var;
  4. weaken($ref); # Make $ref a weak reference
  5. }
  6. # $ref is now undef

Note that if you take a copy of a scalar with a weakened reference, the copy will be a strong reference.

  1. my $var;
  2. my $foo = \$var;
  3. weaken($foo); # Make $foo a weak reference
  4. my $bar = $foo; # $bar is now a strong reference

This may be less obvious in other situations, such as grep(), for instance when grepping through a list of weakened references to objects that may have been destroyed already:

  1. @object = grep { defined } @object;

This will indeed remove all references to destroyed objects, but the remaining references to objects will be strong, causing the remaining objects to never be destroyed because there is now always a strong reference to them in the @object array.

unweaken

  1. unweaken( $ref );

Since version 1.36.

The lvalue REF will be turned from a weak reference back into a normal (strong) reference again. This function mutates the lvalue passed as its argument and returns no value. This undoes the action performed by weaken.

This function is slightly neater and more convenient than the otherwise-equivalent code

  1. my $tmp = $REF;
  2. undef $REF;
  3. $REF = $tmp;

(because in particular, simply assigning a weak reference back to itself does not work to unweaken it; $REF = $REF does not work).

isweak

  1. my $weak = isweak( $ref );

Returns true if $ref is a weak reference.

  1. $ref = \$foo;
  2. $weak = isweak($ref); # false
  3. weaken($ref);
  4. $weak = isweak($ref); # true

NOTE: Copying a weak reference creates a normal, strong, reference.

  1. $copy = $ref;
  2. $weak = isweak($copy); # false

OTHER FUNCTIONS

dualvar

  1. my $var = dualvar( $num, $string );

Returns a scalar that has the value $num in a numeric context and the value $string in a string context.

  1. $foo = dualvar 10, "Hello";
  2. $num = $foo + 2; # 12
  3. $str = $foo . " world"; # Hello world

isdual

  1. my $dual = isdual( $var );

Since version 1.26.

If $var is a scalar that has both numeric and string values, the result is true.

  1. $foo = dualvar 86, "Nix";
  2. $dual = isdual($foo); # true

Note that a scalar can be made to have both string and numeric content through numeric operations:

  1. $foo = "10";
  2. $dual = isdual($foo); # false
  3. $bar = $foo + 0;
  4. $dual = isdual($foo); # true

Note that although $! appears to be dual-valued variable, it is actually implemented using a tied scalar:

  1. $! = 1;
  2. print("$!\n"); # "Operation not permitted"
  3. $dual = isdual($!); # false

You can capture its numeric and string content using:

  1. $err = dualvar $!, $!;
  2. $dual = isdual($err); # true

isvstring

  1. my $vstring = isvstring( $var );

If $var is a scalar which was coded as a vstring the result is true.

  1. $vs = v49.46.48;
  2. $fmt = isvstring($vs) ? "%vd" : "%s"; #true
  3. printf($fmt,$vs);

looks_like_number

  1. my $isnum = looks_like_number( $var );

Returns true if perl thinks $var is a number. See looks_like_number in perlapi.

openhandle

  1. my $fh = openhandle( $fh );

Returns $fh itself if $fh may be used as a filehandle and is open, or is is a tied handle. Otherwise undef is returned.

  1. $fh = openhandle(*STDIN); # \*STDIN
  2. $fh = openhandle(\*STDIN); # \*STDIN
  3. $fh = openhandle(*NOTOPEN); # undef
  4. $fh = openhandle("scalar"); # undef

readonly

  1. my $ro = readonly( $var );

Returns true if $var is readonly.

  1. sub foo { readonly($_[0]) }
  2. $readonly = foo($bar); # false
  3. $readonly = foo(0); # true

set_prototype

  1. my $code = set_prototype( $code, $prototype );

Sets the prototype of the function given by the $code reference, or deletes it if $prototype is undef. Returns the $code reference itself.

  1. set_prototype \&foo, '$$';

tainted

  1. my $t = tainted( $var );

Return true if $var is tainted.

  1. $taint = tainted("constant"); # false
  2. $taint = tainted($ENV{PWD}); # true if running under -T

DIAGNOSTICS

Module use may give one of the following errors during import.

  • Weak references are not implemented in the version of perl

    The version of perl that you are using does not implement weak references, to use isweak or weaken you will need to use a newer release of perl.

  • Vstrings are not implemented in the version of perl

    The version of perl that you are using does not implement Vstrings, to use isvstring you will need to use a newer release of perl.

  • NAME is only available with the XS version of Scalar::Util

    Scalar::Util contains both perl and C implementations of many of its functions so that those without access to a C compiler may still use it. However some of the functions are only available when a C compiler was available to compile the XS version of the extension.

    At present that list is: weaken, isweak, dualvar, isvstring, set_prototype

KNOWN BUGS

There is a bug in perl5.6.0 with UV's that are >= 1<<31. This will show up as tests 8 and 9 of dualvar.t failing

SEE ALSO

List::Util

COPYRIGHT

Copyright (c) 1997-2007 Graham Barr <gbarr@pobox.com>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.

Additionally weaken and isweak which are

Copyright (c) 1999 Tuomas J. Lukka <lukka@iki.fi>. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as perl itself.

Copyright (C) 2004, 2008 Matthijs van Duin. All rights reserved. Copyright (C) 2014 cPanel Inc. All rights reserved. This program is free software; you can redistribute it and/or modify it under the same terms as Perl itself.