Modules

  • ABCDE
  • FGHIL
  • MNOPS
  • TUX

Tools

Scalar::Util

Perl 5 version 8.8 documentation
Recently read

Scalar::Util

NAME

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

SYNOPSIS

  1. use Scalar::Util qw(blessed dualvar isweak readonly refaddr reftype tainted
  2. weaken isvstring looks_like_number set_prototype);

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
  • 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);
  • 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($ref); # false
  • 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.

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

COPYRIGHT

Copyright (c) 1997-2005 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.

BLATANT PLUG

The weaken and isweak subroutines in this module and the patch to the core Perl were written in connection with the APress book `Tuomas J. Lukka's Definitive Guide to Object-Oriented Programming in Perl', to avoid explaining why certain things would have to be done in cumbersome ways.