Tabs-to-space function
sub tabs2space($) { my $str = shift; 1 while $str =~ s/\t+/' ' x (length($&)*8 - length($`)%8)/e; return $str; }
DZone Snippets > Minimiscience > perl
12390 users tagging and storing useful source code snippets
Snippets is a public source code repository. Easily build up your personal collection of code snippets, categorize them with tags / keywords, and share them with the world
sub tabs2space($) { my $str = shift; 1 while $str =~ s/\t+/' ' x (length($&)*8 - length($`)%8)/e; return $str; }
sub basename($) { my $file = shift; $file =~ s!^(?:.*/)?(.+?)(?:\.[^.]*)?$!$1!; return $file; } sub dirname($) {my $file = shift; $file =~ s!/?[^/]*/*$!!; return $file; }
#!/usr/bin/perl -wl use strict; $_ = shift or die 'Invalid argument passed'; s/\s+//g; /^(\d+)[dD](\d+)([+-]\d+)?$/ or die 'Invalid argument passed'; my $sum = 0; $sum += 1 + int rand $2 for 1..$1; $sum += $3 if defined $3; print $sum;
#!/usr/bin/perl -wn use strict; s/\s//g; print chr hex $1 while /([[:xdigit:]]{2})/g; print "\n";
#!/usr/bin/perl -w use strict; my %exten; foreach (@ARGV) { /(\.[^.]+)$/ && $exten{$1}++ foreach glob "$_/*.*" } print "$_: $exten{$_}\n" foreach sort keys %exten;
#!/usr/bin/perl -wn use strict; s/\s//g; print chr oct "0b$1" while /([01]{8})/g; print "\n";
#!/usr/bin/perl -w use strict; use Socket; my $arg = shift; if ($arg =~ /^(\d+\.){3}\d+$/) { print scalar gethostbyaddr(inet_aton($arg), AF_INET), "\n" } else { printf "%vd\n", scalar gethostbyname $arg }
#!/usr/bin/perl -wni # para.pl use strict; /^\s+/ || /^$/ ? print : write; format ARGVOUT = ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<< $_ ^<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<~~ $_ .