I must to implement a Perl script that makes use inside the Linux command 'date' to use the current date as the reference date for the identification of any events stored in a file. Help please. I know the function Localtime() but i don't need that but explicitly linux command DATE.
Asked
Active
Viewed 1,519 times
-2
-
1Have you tried using the `system` function in perl? – Ruth Franklin Aug 30 '15 at 18:40
-
5http://stackoverflow.com/questions/11020812/todays-date-in-perl-in-mm-dd-yyyy-format - don't run `date` when Perl will tell you what you want without spawning any more commands – chicks Aug 30 '15 at 18:41
-
2Why would you need explicit linux date, perl can do dates just as easily. and you can print it in any formate you like. – Chris Doyle Aug 30 '15 at 19:15
-
What exactly does your assignment want you to do with the date once you have it? – ysth Aug 30 '15 at 19:43
-
Please understand that this site is for asking questions that you come up with yourself, not questions that others have asked you. – reinierpost Aug 30 '15 at 21:04
-
We will help you understand your home work but not do it for you. – Sobrique Aug 30 '15 at 21:52
-
@Sobrique I absolutely don't want to solve my homework from you. You're very nice ... But only understand how to pass a linux command to Perl. You could spare you this answer.... :/ :/ – Agata Aug 31 '15 at 07:08
-
you can also use backticks as in `my $var = \`date\`;` – bytepusher Aug 31 '15 at 08:12
-
@chicks i read how perl could give me current date, by localtime function. but i need explicity how use the LINUX command "date" in perl script. Thank you so much in advance – Agata Aug 31 '15 at 10:53
-
@ysth i need that my script will save current data in a variable that i will use later in the script. the problem is that i need use ONLY the LINUX COMMAND "date" – Agata Aug 31 '15 at 10:55
-
1@Agata You'd do well to explain _why_ you want to do what you want to do. To those of us who know better, your request is strange and indicates that you probably need a little guidance rather than just a code snippet. – Matt Jacob Aug 31 '15 at 18:08
-
@MattJacob It's an exercise for my university. Implement functionality that allows to invoke the script from the command line with no parameters, and makes use inside the Linux command DATE to use the current date as the reference date for the identification of any events stored in file eventi.txt.... What I don't know is how to give 'date' to the script. – Agata Aug 31 '15 at 19:05
2 Answers
2
This is not a good way to get the current time in Perl, but it does answer the narrowly defined question above. Here it is working:
$ perl date-test
<<Mon Aug 31 06:52:08 PDT 2015>>
Here is the code:
$ cat date-test
#!/usr/bin/perl
use strict;
use warnings;
my $static_date = `date`;
chomp($static_date);
print "<<$static_date>>\n";

chicks
- 2,393
- 3
- 24
- 40
-3
use strict;
my $sysdate = system ( date);
print $sysdate;

BanksySan
- 27,362
- 33
- 117
- 216

crustycollins
- 92
- 5
-
1err, no. `system()` does not give you the output of the command and it requires quoting you left out. – chicks Aug 31 '15 at 13:50
-
Even if we assume that there wasn't a syntax error, the [return value](http://perldoc.perl.org/functions/system.html) of `system` would not be what the variable name indicates. – Matt Jacob Aug 31 '15 at 18:02