Archive for 'Perl'
While messing around with Perl I ran into the following error: $ perl -e ‘use Time::Local print (localtime)[5] + 1900;’ syntax error at -e line 1, near “)[” Execution of -e aborted due to compilation errors. I asked Perl guru Peter Marschall if he knew what was wrong with this statement, and Peter mentioned that [...]
While updating some Perl code I wrote quite some time back, I came across the following: my ($sec, $min, $hour, $day, $month, $year, $wd, $day, $dst) = localtime(time); print $year + 1900 . “\n”; This code is rather wasteful, especially when the program only needs the year. A much simpler solution is to put the [...]
When a regular expression uses the ‘*’ wild card operator to match text, the regular expression will attempt to match as much as possible when applying the regular expression. Given the the following Perl code with the regular expression “(Some.*text)”: $ cat test.pl #!/usr/bin/perl my $string = “Some chunk of text that has text”; $string [...]
While performing some house cleaning this evening, I came across the following Perl nugget: #!/usr/bin/perl my @alphanumeric = (‘a’..’z', ‘A’..’Z', 0..9); my $randpassword = join ”, map $alphanumeric[rand @alphanumeric], 0..8; print “$randpassword\n” This awesome little 3-line script will produce random 8-character alphanumeric passwords: $ randpasswd.pl ahvtGRE6U $ randpasswd.pl lxVLA7xLv I wish I knew where I [...]
While messing around with Perl, I created a Perl program that displays output similar to the Linux stat utility: $ stat.pl /etc/services /etc/passwd /etc/shadow File: /etc/services Size: 15 Blocks: 2 Block Size: 8192 Device: 22282240 Inode: 7876 Links: 1 Perms: 777 Uid: ( 0 / root ) Gid: ( 0 / root ) Access Time [...]