Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

Scalar::Util

Perl 5 version 18.0 documentation
Recently read

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. The subroutines defined are

  • blessed EXPR

    If EXPR evaluates to 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"
  • 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 EXPR

    If EXPR is a scalar that is a dualvar, 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 EXPR

    If EXPR 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 EXPR

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

  • openhandle FH

    Returns FH if FH may be used as a filehandle and is open, or FH 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 SCALAR

    Returns true if SCALAR is readonly.

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

    If EXPR evaluates to a reference the internal memory address of the referenced value is returned. 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 EXPR

    If EXPR evaluates to a reference the type of the variable referenced is returned. 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
  • set_prototype CODEREF, PROTOTYPE

    Sets the prototype of the given function, or deletes it if PROTOTYPE is undef. Returns the CODEREF.

    1. set_prototype \&foo, '$$';
  • tainted EXPR

    Return true if the result of EXPR is tainted

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

    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, REF will be set to undef.

    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.

  • isweak EXPR

    If EXPR is a scalar which is a weak reference the result is true.

    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

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.

Except 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.