perl

 view release on metacpan or  search on metacpan

cpan/perlfaq/lib/perlfaq4.pod  view on Meta::CPAN

     my ($d, $m, $y) = ( localtime $today-86400 )[3..5]; # WRONG
     printf "Yesterday: %d-%02d-%02d\n", $y+1900, $m+1, $d;

=head2 Does Perl have a Year 2000 or 2038 problem? Is Perl Y2K compliant?

(contributed by brian d foy)

Perl itself never had a Y2K problem, although that never stopped people
from creating Y2K problems on their own. See the documentation for
C<localtime> for its proper use.

Starting with Perl 5.12, C<localtime> and C<gmtime> can handle dates past
03:14:08 January 19, 2038, when a 32-bit based time would overflow. You
still might get a warning on a 32-bit C<perl>:

    % perl5.12 -E 'say scalar localtime( 0x9FFF_FFFFFFFF )'
    Integer overflow in hexadecimal number at -e line 1.
    Wed Nov  1 19:42:39 5576711

On a 64-bit C<perl>, you can get even larger dates for those really long
running projects:

    % perl5.12 -E 'say scalar gmtime( 0x9FFF_FFFFFFFF )'
    Thu Nov  2 00:42:39 5576711

You're still out of luck if you need to keep track of decaying protons
though.

=head1 Data: Strings

=head2 How do I validate input?

(contributed by brian d foy)

There are many ways to ensure that values are what you expect or
want to accept. Besides the specific examples that we cover in the
perlfaq, you can also look at the modules with "Assert" and "Validate"
in their names, along with other modules such as L<Regexp::Common>.

Some modules have validation for particular types of input, such
as L<Business::ISBN>, L<Business::CreditCard>, L<Email::Valid>,
and L<Data::Validate::IP>.

=head2 How do I unescape a string?

It depends just what you mean by "escape". URL escapes are dealt
with in L<perlfaq9>. Shell escapes with the backslash (C<\>)
character are removed with

    s/\\(.)/$1/g;

This won't expand C<"\n"> or C<"\t"> or any other special escapes.

=head2 How do I remove consecutive pairs of characters?

(contributed by brian d foy)

You can use the substitution operator to find pairs of characters (or
runs of characters) and replace them with a single instance. In this
substitution, we find a character in C<(.)>. The memory parentheses
store the matched character in the back-reference C<\g1> and we use
that to require that the same thing immediately follow it. We replace
that part of the string with the character in C<$1>.

    s/(.)\g1/$1/g;

We can also use the transliteration operator, C<tr///>. In this
example, the search list side of our C<tr///> contains nothing, but
the C<c> option complements that so it contains everything. The
replacement list also contains nothing, so the transliteration is
almost a no-op since it won't do any replacements (or more exactly,
replace the character with itself). However, the C<s> option squashes
duplicated and consecutive characters in the string so a character
does not show up next to itself

    my $str = 'Haarlem';   # in the Netherlands
    $str =~ tr///cs;       # Now Harlem, like in New York

=head2 How do I expand function calls in a string?

(contributed by brian d foy)

This is documented in L<perlref>, and although it's not the easiest
thing to read, it does work. In each of these examples, we call the
function inside the braces used to dereference a reference. If we
have more than one return value, we can construct and dereference an
anonymous array. In this case, we call the function in list context.

    print "The time values are @{ [localtime] }.\n";

If we want to call the function in scalar context, we have to do a bit
more work. We can really have any code we like inside the braces, so
we simply have to end with the scalar reference, although how you do
that is up to you, and you can use code inside the braces. Note that
the use of parens creates a list context, so we need C<scalar> to
force the scalar context on the function:

    print "The time is ${\(scalar localtime)}.\n"

    print "The time is ${ my $x = localtime; \$x }.\n";

If your function already returns a reference, you don't need to create
the reference yourself.

    sub timestamp { my $t = localtime; \$t }

    print "The time is ${ timestamp() }.\n";

The C<Interpolation> module can also do a lot of magic for you. You can
specify a variable name, in this case C<E>, to set up a tied hash that
does the interpolation for you. It has several other methods to do this
as well.

    use Interpolation E => 'eval';
    print "The time values are $E{localtime()}.\n";

In most cases, it is probably easier to simply use string concatenation,
which also forces scalar context.

    print "The time is " . localtime() . ".\n";

=head2 How do I find matching/nesting anything?

To find something between two single
characters, a pattern like C</x([^x]*)x/> will get the intervening

 view all matches for this distribution
 view release on metacpan -  search on metacpan

( run in 0.456 second using v1.00-cache-2.02-grep-82fe00e-cpan-1925d2aa809 )