Fauxtobox - fake autoboxing (call methods on plain scalars)
use Fauxtobox; my $n = "zomg"->$_length; # $n = 4 print $n->$_sqrt; # "2" # ... and many methods more
This module provides fake autoboxing support. Autoboxing means being able to call methods on non-objects like 42
or 'hello'
because the language will automatically wrap them in objects. If you want that, see autobox.
What this module does is much simpler: It exports a bunch of variables that can be used like methods. These method variables can be used on any value, not just objects.
By default everything listed below is exported. If you don't want that, you can explicitly list the variables you want:
use Fauxtobox qw($_defined $_length $_apply);
For convenience you can omit the leading $_
in the import list:
use Fauxtobox qw(defined length apply);
Several functions in Perl take or return lists. In general, the method equivalents of these take and return array references instead, to make method chaining possible. Exceptions are noted below.
$X->$_apply($F)
is equivalent to $F->($X)
, so e.g. "abc"->$_apply(\&display)
is equivalent to display("abc")
.
$X->$_list
returns %{$X}
if $X
is a hash reference and @{$X}
otherwise. This is useful if you have an array reference and want to turn it into a list of its values.
$X->$_qr
is equivalent to qr/$X/
. $X->$_qr($FLAGS)
is equivalent to qr/$X/$FLAGS
except that's a syntax error, but e.g. '^hello\s+world'->$_qr('i')
is equivalent to qr/^hello\s+world/i
.
See "qr" in perlfunc.
$X->$_m($REGEX)
is equivalent to $X =~ m/$REGEX/
.
See "m" in perlfunc.
$X->$_m_g($REGEX)
is equivalent to $X =~ m/$REGEX/g
.
See "m" in perlfunc.
$X->$_m_gc($REGEX)
is equivalent to $X =~ m/$REGEX/gc
.
See "m" in perlfunc.
$X->$_s($REGEX, $REPLACEMENT)
is equivalent to $X =~ s/$REGEX/$REPLACEMENT->()/e
if $REPLACEMENT
is a subroutine reference and $X =~ s/$REGEX/$REPLACEMENT/
otherwise.
See "s" in perlfunc.
$X->$_s_g($REGEX, $REPLACEMENT)
is equivalent to $X =~ s/$REGEX/$REPLACEMENT->()/ge
if $REPLACEMENT
is a subroutine reference and $X =~ s/$REGEX/$REPLACEMENT/g
otherwise.
See "s" in perlfunc.
$X->$_s_r($REGEX, $REPLACEMENT)
is equivalent to $X =~ s/$REGEX/$REPLACEMENT->()/re
if $REPLACEMENT
is a subroutine reference and $X =~ s/$REGEX/$REPLACEMENT/r
otherwise.
See "s" in perlfunc.
$X->$_s($REGEX, $REPLACEMENT)
is equivalent to $X =~ s/$REGEX/$REPLACEMENT->()/gre
if $REPLACEMENT
is a subroutine reference and $X =~ s/$REGEX/$REPLACEMENT/gr
otherwise.
See "s" in perlfunc.
These are file test operators. $X->$_test_X
is equivalent to -X $X
for all letters X listed above.
See "-X" in perlfunc.
$X->$_abs
is equivalent to abs $X
.
See "abs" in perlfunc.
$X->$_alarm
is equivalent to alarm $X
.
See "alarm" in perlfunc.
$X->atan2($Y)
is equivalent to atan2 $X, $Y
.
See "atan2" in perlfunc.
$X->$_bless($CLASS)
is equivalent to bless $X, $CLASS
. $X->$_bless
is equivalent to bless $X
.
See "bless" in perlfunc.
$X->$_chdir
is equivalent to chdir $X
.
See "chdir" in perlfunc.
$X->$_chmod($MODE)
is equivalent to chmod $MODE, @{$X}
if $X
is an array reference and chmod $MODE, $X
otherwise.
See "chmod" in perlfunc.
$X->$_chomp
is equivalent to chomp @{$X}
if $X
is an array reference and chomp $X
otherwise.
See "chomp" in perlfunc.
$X->$_chop
is equivalent to chop @{$X}
if $X
is an array reference and chop $X
otherwise.
See "chop" in perlfunc.
$X->$_chown($UID, $GID)
is equivalent to chown $UID, $GID, @{$X}
if $X
is an array reference and chown $UID, $GID, $X
otherwise.
See "chown" in perlfunc.
$X->$_chr
is equivalent to chr $X
.
See "chr" in perlfunc.
$X->$_chroot
is equivalent to chroot $X
.
See "chroot" in perlfunc.
$X->$_cos
is equivalent to cos $X
.
See "cos" in perlfunc.
$X->$_crypt($SALT)
is equivalent to crypt $X, $SALT
.
See "crypt" in perlfunc.
$X->$_defined
is equivalent to defined $X
.
$X->$_delete($KEY)
is equivalent to delete $X->[$KEY]
if $X is an array reference and delete $X->{$KEY}
otherwise.
See "delete" in perlfunc.
$X->$_die
is equivalent to die $X
.
See "die" in perlfunc.
$X->$_each
is equivalent to each @{$X}
if $X
is an array reference and each %{$X}
otherwise.
See "each" in perlfunc.
$X->$_eval
is equivalent to eval $X
.
See "eval" in perlfunc.
$X->$_exec(@ARGS)
is equivalent to exec { $X } @ARGS
. $X->$_exec
is equivalent to exec @{$X}
if $X
is an array reference and exec $X
otherwise.
See "exec" in perlfunc.
$X->$_exists($KEY)
is equivalent to exists $X->[$KEY]
if $X
is an array reference and exists $X->{$KEY}
otherwise.
See "exists" in perlfunc.
$X->$_exit
is equivalent to exit $X
.
See "exit" in perlfunc.
$X->$_exp
is equivalent to exp $X
.
See "exp" in perlfunc.
$X->$_fc
is equivalent to fc $X
.
See "fc" in perlfunc.
$X->$_getpgrp
is equivalent to getpgrp $X
.
$X->$_getpwnam
is equivalent to getpwnam $X
.
$X->$_getgrnam
is equivalent to getgrnam $X
.
$X->$_gethostbyname
is equivalent to gethostbyname $X
.
$X->$_getnetbyname
is equivalent to getnetbyname $X
.
$X->$_getprotobyname
is equivalent to getprotobyname $X
.
$X->$_getpwuid
is equivalent to getpwuid $X
.
$X->$_getgrgid
is equivalent to getgrgid $X
.
$X->$_getservbyname($Y)
is equivalent to getservbyname $X, $Y
.
$X->$_gethostbyaddr($Y)
is equivalent to gethostbyaddr $X, $Y
.
$X->$_getnetbyaddr($Y)
is equivalent to getnetbyaddr $X, $Y
.
$X->$_getprotobynumber
is equivalent to getprotobynumber $X
.
$X->$_getservbyport($Y)
is equivalent to getservbyport $X, $Y
.
$X->$_glob
is equivalent to [ glob $X ]
, i.e. it returns an array reference of results.
See "glob" in perlfunc.
$X->$_gmtime
is equivalent to gmtime $X
.
See "gmtime" in perlfunc.
$X->$_grep($F)
is equivalent to [ grep $F->($_), @{$X} ]
, i.e. it takes and returns an array reference. The function $F
is passed the current element as an argument.
See "grep" in perlfunc.
$X->$_hex
is equivalent to hex $X
.
See "hex" in perlfunc.
$X->$_index($NEEDLE)
is equivalent to index $X, $NEEDLE
. $X->$_index($NEEDLE, $OFFSET)
is equivalent to index $X, $NEEDLE, $OFFSET
.
See "index" in perlfunc.
$X->$_int
is equivalent to int $X
.
See "int" in perlfunc.
$X->$_join($SEP)
is equivalent to join $SEP, @{$X}
.
See "join" in perlfunc.
$X->$_keys
is equivalent to [ keys @{$X} ]
if $X
is an array reference and [ keys %{$X} ]
otherwise.
See "keys" in perlfunc.
$X->$_kill($SIGNAL)
is equivalent to kill $SIGNAL, @{$X}
if $X
is an array reference and kill $SIGNAL, $X
otherwise.
See "kill" in perlfunc.
$X->$_lc
is equivalent to lc $X
.
See "lc" in perlfunc.
$X->$_lcfirst
is equivalent to lcfirst $X
.
$X->$_length
is equivalent to length $X
.
See "length" in perlfunc.
$X->$_link($Y)
is equivalent to link $X, $Y
.
See "link" in perlfunc.
$X->$_localtime
is equivalent to localtime $X
.
$X->$_log
is equivalent to log $X
.
See "log" in perlfunc.
$X->$_lstat
is equivalent to lstat $X
.
See "lstat" in perlfunc.
$X->$_map($F)
is equivalent to [ map $F->($_), @{$X} ]
, i.e. it takes and returns an array reference. The function $F
is passed the current element as an argument.
See "map" in perlfunc.
$X->$_mkdir
is equivalent to mkdir $X
.
See "mkdir" in perlfunc.
$X->$_oct
is equivalent to oct $X
.
See "oct" in perlfunc.
$X->$_ord
is equivalent to ord $X
.
See "ord" in perlfunc.
$X->$_pack($FORMAT)
is equivalent to pack $FORMAT, @{$X}
if $X
is an array reference and pack $FORMAT, $X
otherwise.
See "pack" in perlfunc.
$X->$_pop
is equivalent to pop @{$X}
.
See "pop" in perlfunc.
$X->$_pos
is equivalent to pos $X
. $X->$_pos($Y)
is equivalent to pos($X) = $Y
.
See "pos" in perlfunc.
$X->$_prototype
is equivalent to prototype $X
.
$X->$_push(@VALUES)
is equivalent to push @{$X}, @VALUES
.
See "push" in perlfunc.
$X->$_quotemeta
is equivalent to quotemeta $X
.
$X->$_rand
is equivalent to rand $X
.
See "rand" in perlfunc.
$X->$_readlink
is equivalent to readlink $X
.
$X->$_ref
is equivalent to ref $X
.
See "ref" in perlfunc.
$X->$_rename($Y)
is equivalent to rename $X, $Y
.
See "rename" in perlfunc.
$X->$_require
is equivalent to require $X
.
$X->$_reverse
is equivalent to [ reverse @{$X} ]
if $X
is an array reference and scalar reverse $X
otherwise.
$X->$_rindex($NEEDLE)
is equivalent to rindex $X, $NEEDLE
. $X->$_rindex($NEEDLE, $OFFSET)
is equivalent to rindex $X, $NEEDLE, $OFFSET
.
See "rindex" in perlfunc.
$X->$_rmdir
is equivalent to rmdir $X
.
See "rmdir" in perlfunc.
$X->$_shift
is equivalent to shift @{$X}
.
See "shift" in perlfunc.
$X->$_sin
is equivalent to sin $X
.
See "sin" in perlfunc.
$X->$_sleep
is equivalent to sleep $X
.
See "sleep" in perlfunc.
$X->$_sort
is equivalent to [ sort @{$X} ]
. $X->$_sort($CMP)
is equivalent to [ sort { $CMP->($a, $b) } @{$X} ]
.
See "sort" in perlfunc.
$X->$_splice
is equivalent to splice @{$X}
. $X->$_splice($OFFSET)
is equivalent to splice @{$X}, $OFFSET
. $X->$_splice($OFFSET, $LENGTH)
is equivalent to splice @{$X}, $OFFSET, $LENGTH
. $X->$_splice($OFFSET, $LENGTH, @VALUES)
is equivalent to splice @{$X}, $OFFSET, $LENGTH, @VALUES
.
See "splice" in perlfunc.
$X->$_split
is equivalent to [ split ' ', $X ]
. $X->$_split($REGEX)
is equivalent to [ split $REGEX, $X ]
. $X->$_split($REGEX, $LIMIT)
is equivalent to [ split $REGEX, $X, $LIMIT ]
.
See "split" in perlfunc.
$X->$_sprintf($FORMAT)
is equivalent to sprintf $FORMAT, @{$X}
if $X
is an array reference and sprintf $FORMAT, $X
otherwise.
See "sprintf" in perldoc.
$X->$_sqrt
is equivalent to sqrt $X
.
See "sqrt" in perlfunc.
$X->$_srand
is equivalent to srand $X
.
See "srand" in perlfunc.
$X->$_stat
is equivalent to stat $X
.
See "stat" in perlfunc.
$X->$_substr($OFFSET)
is equivalent to substr $X, $OFFSET
. $X->$_substr($OFFSET, $LENGTH)
is equivalent to substr $X, $OFFSET, $LENGTH
. $X->$_substr($OFFSET, $LENGTH, $REPLACEMENT)
is equivalent to substr $X, $OFFSET, $LENGTH, $REPLACEMENT
.
See "substr" in perlfunc.
$X->$_symlink($Y)
is equivalent to symlink $X, $Y
.
$X->$_syscall(@ARGS)
is equivalent to syscall $X, @ARGS
.
$X->$_system(@ARGS)
is equivalent to system { $X } @ARGS
. $X->$_system
is equivalent to system @{$X}
if $X
is an array reference and system $X
otherwise.
See "system" in perlfunc.
$X->$_truncate($Y)
is equivalent to truncate $X, $Y
.
$X->$_uc
is equivalent to uc $X
.
See "uc" in perlfunc.
$X->$_ucfirst
is equivalent to ucfirst $X
.
$X->$_umask
is equivalent to umask $X
.
See "umask" in perlfunc.
$X->$_unlink
is equivalent to unlink $X
.
See "unlink" in perlfunc.
$X->$_unpack($FORMAT)
is equivalent to unpack $FORMAT, $X
.
See "unpack" in perlfunc.
$X->$_unshift(@VALUES)
is equivalent to unshift @{$X}, @VALUES
.
$X->$_utime($ATIME, $MTIME)
is equivalent to utime $ATIME, $MTIME, @{$X}
if $X
is an array reference and utime $ATIME, $MTIME, $X
otherwise.
See "utime" in perlfunc.
$X->$_values
is equivalent to [ values @{$X} ]
if $X
is an array reference and [ values %{$X} ]
otherwise.
See "values" in perlfunc.
$X->$_vec($OFFSET, $BITS)
is equivalent to vec $X, $OFFSET, $BITS
. $X->$_vec($OFFSET, $BITS, $REPLACEMENT) is equivalent to c<< vec($X, $OFFSET, $BITS) = $REPLACEMENT
.
See "vec" in perlfunc.
$X->$_waitpid
is equivalent to waitpid $X, 0
. $X->$_waitpid($FLAGS)
is equivalent to waitpid $X, $FLAGS
.
$X->$_warn
is equivalent to warn $X
.
See "warn" in perlfunc.
After installing, you can find documentation for this module with the perldoc command.
perldoc Fauxtobox
You can also look for information at:
Lukas Mai, <l.mai at web.de>
Copyright 2013 Lukas Mai.
This program is free software; you can redistribute it and/or modify it under the terms of either: the GNU General Public License as published by the Free Software Foundation; or the Artistic License.
See http://dev.perl.org/licenses/ for more information.