leszekdubiel has asked for the wisdom of the Perl Monks concerning the following question:
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.
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Prevent unstructuring reference when passed as a funciton parameter
by Corion (Patriarch) on Oct 22, 2022 at 09:59 UTC |