Scalar::Quote - Utility functions to quote Perl strings
use Scalar::Quote ':short'; $_=pack('c',rand 127) for (@a[0..1000]); $s1=join '', @a; $_=pack('c',rand 127) for (@b[0..1000]); $s2=join '', @b; $_=pack('c',rand 127) for (@c[0..40]); $s3=join '', @c; print "Q(\$s1)=",Q($s1),"\n"; print "S(\$a)=",S($a),"\n"; D($s3.$s1, $s3.$s2); print "$a is not the same as $b\n"; print N(0), N(1), N(undef), N("hello"), "\n";
Several subrutines to quote scalars and spot differences between strings.
Mostly useful for debugging purposes.
quote $n as a number.
$n
returns the string conveniently enclosed in single or double quotes, escaping unprintable and quoting chars as required.
quote the beginning of $string.
$string
similar to substr($str, $start, $len) but adds a head or/and a tail to the substring stating how many chars have been left out.
substr($str, $start, $len)
returns the index where the two strings start to differ or -1 if they are equal.
str_diff compares two strings and creates quoted versions of them around the place where they start to differ.
str_diff
D($s1, $s2) and print "$a is not the same as $b\n";
In scalar context the quoted strings are stored in globals $a and $b on the caller package.
$a
$b
In list context the quoted strings are returned ($a and $b are untouched).
When both strings are equal, undef or the empty list is returned.
Optional arguments $start and $len allow to configure the length of the quoting. $start is the location to start the quote *after* the differences begin, so it should be a negative number.
$start
$len
Nothing by default.
exports quote, quote_start, quote_cut and quote_number
quote
quote_start
quote_cut
quote_number
exports str_diffix and str_diff subrutines.
str_diffix
exports Q, S, N and D.
Q
S
N
D
Salvador Fandiño, <sfandino@yahoo.com>
Copyright 2002-2006 by Salvador Fandiño
This library is free software; you can redistribute it and/or modify it under the same terms as Perl itself.
To install Scalar::Quote, copy and paste the appropriate command in to your terminal.
cpanm
cpanm Scalar::Quote
CPAN shell
perl -MCPAN -e shell install Scalar::Quote
For more information on module installation, please visit the detailed CPAN module installation guide.