From: Ilya Zakharevich Date: Thu, 1 Apr 1999 03:29:25 -0500 (EST) Message-Id: <199904010829.DAA16282@monk.mps.ohio-state.edu> To: mjd@plover.com X-Also-Posted-To: comp.lang.perl.moderated Subject: Re: Monthly Post: Infrequently Asked Questions about Perl References: <19990401052410.23923.qmail@plover.com> Organization: Department of Mathematics, The Ohio State University X-Newsreader: trn 4.0-test68 (29 August 1998) [A complimentary Cc of this posting was sent to Mark-Jason Dominus ], who wrote in article <19990401052410.23923.qmail@plover.com>: > How can I find the creation date of a file? > > Use this function: > > sub creation_date { > use FileHandle; > my $filename = shift or die "Usage: &creation_date(filename)\n"; > my $fh1 = new FileHandle; > open $fh1, "< $filename" or return undef; > unlink $filename or return undef; > my $fh2 = new FileHandle; > open $fh2, "> $filename" or return undef; > print $fh2 <$fh1>; > time; > } This is not portable, since unlink(open_file()) will fail on DOSISH machines. Why not use an already cooked solution: rename() and File::Copy? > How can I tell if a string is a number? > > The simplest method is: > > if ($string == "$string") { > # It is a number > } > > > Note the use of the `==' operator to compare the string to its numeric > value. However, this approach is dangerous because the `$string' might > contain arbitrary code such as C<@{[system "rm -rf /"]}> which would be > executed as a result of the interpolation process. For safety, use this > regular expression: > > if ($var =~ > /(?=.)M{0,3}(C[MD]|D?C{0,3})(X[CL]|L?X{0,3})(I[XV]|V?I{0,3})/) { > print "$var contains a number.\b"; > } Why not use ==~ which is created exactly for this purpose? if ($string ==~ "$string") { > How do I find the largest element in an array? > > Write a `foreach' loop to scan the elements one at a time, and stop when > you get to the largest one. This will not work with tied arrays referencing /dev/random, as in the implementation of Net::WWW. > Someone told me that `tr/x//d' would be faster than `s/x//g'. > Is this true? Why? > > `tr///' is faster on multiprocessor machines because it can be > vectorized, so that each `x' is removed by a different processor. > `s///', however, must always be performed sequentially. Beware that future enhancement to REx engine may make this advice wrong. > > How do I get the length of a variable? > > Use > > length('$variable'); This may get you into problems if the variable contains "'". Better to use HERE-docs, and strip the final newline $l = length chomp <<'EOF'; $variable EOF > How do I differentiate my Object Oriented Perl scripts from other perl scripts? > > Just as C programmers use `.cpp' as an extension for their object > oriented C programs, many perl programmers use `.ppp' for their object > oriented Perl programs. This advice is very obsolete. It is a long-standing tradition to use the extension .oops for object oriented Perl scripts. > How do I sort a hash? > > %sorted = sort %hash; This mixes keys and values. The proper way is to repeat the hash, so both the keys and values are seen by sort: (%sorted, %sorted) = sort %hash, %hash; > What's a regular expression to replace `09:23:53 08-OCT-98' with > `Thu Oct 8 09:23:53 1998'? > > s{.*}{Thu Oct 8 09:23:53 1998} Append 'l' to make this locale-conscient. > How do I block warnings? > > The simplest way is to do > > close STDERR; Seasoned Perl programmers know that one can save 1 char: exit STDERR; Hope this helps, Ilya ---------------------------------------------------------------- [A complimentary Cc of this posting was sent to Mark-Jason Dominus ], who wrote in article <19990401052410.23923.qmail@plover.com>: > How do I get tomorrow's date? > > Use this function: > > sub tomorrow_date { > sleep 86_400; > return localtime(); > } In addition to other remarks: this implementation is very pessimal speed-wise. Here is an important optimization: sub tomorrow_date { sleep 86_400; return localtime(); sleep -86_400; } Ilya