Is there a way to pass "Time::Piece" object from "localtime" directly as function parameter without using variables $t or $x?
#!/usr/bin/perl
use strict;
use Time::Piece;
sub time_repr {
$_[0]->strftime("%F %T");
}
my $t = localtime;
print
"ref in var OK: ", (time_repr($t)), "\n\n",
"not elegant: ", (time_repr(do { my $x = localtime() })), "\n\n",
"error 1: ", eval { (time_repr(localtime)) }, $@ , "\n",
"error 2: ", eval { (time_repr(\localtime)) }, $@ , "\n",
"error 3: ", eval { (time_repr(\(localtime))) }, $@ , "\n",
;
Output
ref in var OK: 2022-10-22 11:46:34
not elegant: 2022-10-22 11:46:34
error 1: Can't call method "strftime" on unblessed reference at ./p1 l
+ine 7.
error 2: Can't call method "strftime" on unblessed reference at ./p1 l
+ine 7.
error 3: Can't call method "strftime" on unblessed reference at ./p1 l
+ine 7.
Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
Read Where should I post X? if you're not absolutely sure you're posting in the right place.
Please read these before you post! —
Posts may use any of the Perl Monks Approved HTML tags:
- a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
| |
For: |
|
Use: |
| & | | & |
| < | | < |
| > | | > |
| [ | | [ |
| ] | | ] |
Link using PerlMonks shortcuts! What shortcuts can I use for linking?
See Writeup Formatting Tips and other pages linked from there for more info.