Book HomeJava and XSLTSearch this book

8.180. Scalar::Util

Implements a few useful scalar-related subroutines. Like List::Util, Scalar::Util is something useful that doesn't necessarily fit well as a keyword in the Perl core. By default, Scalar::Util does not export any subroutines. As of Perl 5.8, Scalar::Util is shipped with the source kit.

Scalar::Util implements the following methods.

blessed

blessed expr

Evaluates whether expr is a blessed reference. If successful, blessed returns the name of the package. Otherwise, blessed returns undef.

use Scalar::Util qw(blessed);
use CGI;
 
my $cgi = CGI->new();
my $not_cgi = "IamNotCGI";

my $is_blessed = blessed($cgi);
if(defined($is_blessed) { print "$cgi\n;" } # Prints CGI
 
my $is_blessed2 = blessed($not_cgi);
if(defined($is_blessed2) { print "$not_cgi\n"; } # undef
dualvar

dualvar number, string

Returns a string that has the value numberin a numeric context and a value string in a string context. For example:

my $context = dualvar(10, "Nathan");
my $add_nums = $context + 1; # '11'
my $str_add = $context . "Patwardhan"; # Nathan Patwardhan
isweak

isweak expr

Returns true if expr is a scalar, which is a weak reference:

my $weak_ref = \$boo_hoo;
my $i_am_weak = isweak($weak_ref); # false
weaken($weak_ref);
$i_am_weak = isweak($weak_ref); # true
openhandle

openhandle fh

Returns fh if fh is an open filehandle. fh may also be a tied filehandle. Returns undef on failure.

my $fh = openhandle(*STDIN);
$fh = openhandle("sumfin"); # undef, since sumfin isn't 
                            # open
readonly

readonly scalar

Returns true if scalar is read-only.

reftype

reftype expr

Returns the reference type of expr if expr is a reference. Otherwise, returns undef.

my $r_type = reftype "ORA"; # undef, not a reference
my $r_type = reftype [];    # ARRAY
tainted

tainted expr

Returns true if the result of expr is tainted:

my $is_tainted = tainted($ENV{PATH}); 
# Returns true if -T is enabled
weaken

weaken ref

Returns ref into a weak reference. When the reference count on an object reaches 0, ref will be set to undef.



Library Navigation Links

Copyright © 2002 O'Reilly & Associates. All rights reserved.