What's the best way to get Epoch milliseconds in Perl? - perl

It's easy to get Epoch-Seconds (timestamp) in perl:
time
But what's with milliseconds? The most effective way seems to be time*1000, but that's not as accurate as I want it to be. Any good hints except for the long terms documented #perldoc?

The Time::HiRes module has a drop-in replacement for time
$ perl -E 'say time'
1298827929
$ perl -MTime::HiRes=time -E 'say time'
1298827932.67446
You can read more in the perl FAQ
perldoc -q "How can I measure time under a second"

A real world example would be:
use Time::HiRes qw(gettimeofday);
print gettimeofday;

perl -MTime::HiRes=time -e 'print time;'
For Perl: v5.8.4 built for SunOS (sun4-solaris-64int), oylenshpeegul's answer needs to be modified.

Related

Perl: Replace text parameter by current timestamp

I have an utility to generate code documentation every night. I would like to add a timestamp in order to be aware how old the generated documentation is. I would like to use perl.
I've seen that with the following sentence I can change a joker (%1) by any value I want
perl -pi.bak -e 's/%1/date/g' footer.html
And with this other one I can get the system timestamp:
perl -MPOSIX -we "print POSIX::strftime('%d/%m/%Y %H:%M:%S', localtime)"
My question is whether there is any way to merge both instructions in just one sentence.
Thank you very much
Try doing this :
perl -MPOSIX -pi.bak -e 'BEGIN{$date = strftime("%d/%m/%Y %H:%M:%S", localtime);} s/%1/$date/g' file.html
sh command:
perl -i.bak -MPOSIX -pe's/%1/strftime("%d/%m/%Y %H:%M:%S", localtime)/eg'
cmd command:
perl -i.bak -MPOSIX -pe"s/%1/strftime('%d/%m/%Y %H:%M:%S', localtime)/eg"
/e cause the replacement expression to be treated as Perl code to execute, the result of which is the replacement text.

How do I pass Shell parameters to Perl script?

I am trying to find dates with in a certain format, I have done it before with perl(strftime), but that time I mentioned a static time, this time i need a variable $CURRENT_DAY here.
Below is the issue, when equate the CURRENT_DAY by using DAYHOUR=86400 and an integer, the variable is giving the right time, but once I put $CURRENT_DAY variable in the statement, the date would not decrease.
$ DAYHOUR=86400
$ DAY=1
$ CURRENT_DAY=`echo $(($DAYHOUR*$DAY))`
$ DD=`perl -e 'use POSIX; print strftime "%d", localtime time - $CURRENT_DAY;'`
$ echo $DD
20
$ DAY=`echo $(($DAY+1))`
$ CURRENT_DAY=`echo $(($DAYHOUR*$DAY))`
$ DD=`perl -e 'use POSIX; print strftime "%d", localtime time - $CURRENT_DAY;'`
$ echo $DD
20
$ DAY=`echo $(($DAY+1))`
$ echo $DAY
3
$ CURRENT_DAY=`echo $(($DAYHOUR*$DAY))`
$ echo $CURRENT_DAY
259200
$ echo `perl -e 'use POSIX; print strftime "%d", localtime time - 259200;'`
17
Your principal problem is that $CURRENT_DAY is a Perl script variable. By enclosing your Perl script in single quotes, there is no visibility to the shell's variable of the same name. Had you enabled warnings (e.g. with -w) you would have seen this.
There are a couple of ways to circumvent your problem. One is to use double quotes to encapsulate your Perl thus allowing the shell to first expand its variable's value before the Perl is run:
CURRENT_DAY=3
perl -MPOSIX -wle "print strftime qq(%d),localtime time-(86400*$CURRENT_DAY);print $CURRENT_DAY"
17
Another is:
export CURRENT_DAY=3
perl -MPOSIX -wle 'print strftime qq(%d),localtime time-(86400*$ENV{CURRENT_DAY})'
Be advised that adding or subtracting 24-hours from a time to calculate yesterday or tomorrow will not correctly account for daylight saving changes. See this faq
You may pass them as arguments in #ARGV:
$ dd_seconds_ago () { perl -MPOSIX -e 'print strftime q(%d), localtime(time - shift)' "$#"; }
$ DD=$(dd_seconds_ago 86400)
Without an argument and in the above context, shift shifts #ARGV, which is handy for shell one-liners like this
Like Perl, sh doesn't interpolate in single quoted strings, so Perl sees $CURRENT_DAY instead of the actual number, and you've never assigned anything to that Perl variable. You could switch to a double-quoted string.
perl -MPOSIX -e"print strftime '%d', localtime time-$CURRENT_DAY;"
That's fine since $CURRENT_DAY is a number, but if you wanted to pass an arbitrary string, you'd use an env var or an argument.
export CURRENT_DAY
perl -MPOSIX -e'print strftime "%d", localtime time-$ENV{CURRENT_DAY};'
or
perl -MPOSIX -e'print strftime "%d", localtime time-$ARGV[0];' -- "$CURRENT_DAY"
Note that your code is buggy, though. There are two hours every year for which your code will give the wrong answer because not all days have 86400 seconds. Some have 82800, and others have 90000. (And that's assuming leap seconds don't factor in.) A Perl solution that doesn't suffer from that problem follows:
perl -MDateTime -e'print
DateTime->today(time_zone=>"local")
->subtract(days=>$ARGV[0])
->strftime("%d")' -- "$DAY"
Or you could use date.
date -d "$DAY days ago" +%d
I am assuming you want to pass the number of days in the past in the shell variable DAY and that you want the answer in the shell variable DD
So if it is the 20th of the month and the DAY is 1, then DD should be set to 19
You could modify your Perl command as follows:
DD=`perl -e 'use POSIX; print strftime "%d", localtime( time - ($ENV{DAY}* 86400))';
Alternatively, you could use the GNU date command that is widely available
DD=`date -d "$DAY days ago" +%d`
Using date is probably better at dealing with leap days, etc

grep regex to perl or awk

I have been using Linux env and recently migrated to solaris. Unfortunately one of my bash scripts requires the use of grep with the P switch [ pcre support ] .As Solaris doesnt support the pcre option for grep , I am obliged to find another solution to the problem.And pcregrep seems to have an obvious loop bug and sed -r option is unsupported !
I hope that using perl or nawk will solve the problem on solaris.
I have not yet used perl in my script and am unware neither of its syntax nor the flags.
Since it is pcre , I beleive that a perl scripter can help me out in a matter of minutes. They should match over multiple lines .
Which one would be a better solution in terms of efficiency the awk or the perl solution ?
Thanks for the replies .
These are some grep to perl conversions you might need:
grep -P PATTERN FILE(s) ---> perl -nle 'print if m/PATTERN/' FILE(s)
grep -Po PATTERN FILE(s) ---> perl -nle 'print "$1\n" while m/(PATTERN)/g' FILE(s)
That's my guess as to what you're looking for, if grep -P is out of the question.
Here's a shorty:
grep -P /regex/ ====> perl -ne 'print if /regex/;'
The -n takes each line of the file as input. Each line is put into a special perl variable called $_ as Perl loops through the whole file.
The -e says the Perl program is on the command line instead of passing it a file.
The Perl print command automatically prints out whatever is in $_ if you don't specify for it to print out anything else.
The if /regex/ matches the regular expression against whatever line of your file is in the $_ variable.

How can I execute Perl code stored inside a shell script variable?

I've got a script that calls Perl's Time::HiRes module to calculate elapsed time. Basically the script gets the time by passing the following one-liner:
use Time::HiRes qw(time); print time
to the Perl interpreter via backticks and gets back the results.
#/bin/sh
START_TIME=`perl -e 'use Time::HiRes qw(time); print time'`
END_TIME=`perl -e 'use Time::HiRes qw(time); print time'`
ELAPSED_TIME=$(echo "($END_TIME - $START_TIME)" | bc)
echo $ELAPSED_TIME
I tried to rewrite it in a more modular way, but I'm stumped by the quoting rules of the Bash shell.
#/bin/sh
CALCULATE='bc'
NOW="perl -e 'use Time::HiRes qw(time); print time'"
START_TIME=`$NOW`
[Some long running task ...]
ELAPSED_TIME=$(echo "($NOW - $START_TIME)" | $CALCULATE)
echo $ELAPSED_TIME
Bash complains that something is not quoted properly. Why doesn't Bash just expand the command in $NOW and pass it to the backtick to be executed?
I tried various ways to embed Perl code in a shell script variable, but I can't seem to get it right.
How can I quote Perl code inside a shell script correctly?
Using a function is the most straightforward way to do this, I think:
#! /bin/bash
now() {
perl -e 'use Time::HiRes qw(time); print time';
}
calc=bc
time1=$(now)
time2=$(now)
elapsed=$(echo $time2 - $time1 | $calc)
echo $elapsed $time1 $time2
Essentially no quoting is required.
Your problem is that $NOW is just a string with some Perl code in it. You need to tell Bash to execute it, with backticks or $():
ELAPSED_TIME=$(echo "($($NOW) - $START_TIME)" | $CALCULATE)
Also, Bash can do arithmetic natively:
ELAPSED_TIME=$(( $($NOW) - $START_TIME))
There isn't any need to invoke bc.
Finally, starting and stopping perl is likely to take a lot of time, which will add noise to your results. I'd recommend running perl only once, and having perl itself execute the long-running task. You'd then do all the computation within Perl itself as well:
#!/usr/bin/perl
use Time::HiRes qw(time);
my $start = time;
system(#ARGV);
my $end = time;
print "Elapsed: ", ($end - $start), "\n"
Or you could just use the Bash builtin time (or /usr/bin/time) to just do all the timing directly.
If $NOW is outside of quotes, it gets split on whitespace.
$ perl -E'say 0+#ARGV; say for #ARGV' $NOW
7
perl
-e
'use
Time::HiRes
qw(time);
print
time'
You can surround the variable by double-quotes to avoid this:
$ perl -E'say 0+#ARGV; say for #ARGV' "$NOW"
1
perl -e 'use Time::HiRes qw(time); print time'
But you want to execute that string as a shell command. For that, use eval.
$ eval "$NOW"
1335602750.57325
Finally, to assign it, we use the backticks (or equivalent $( ... )).
$ START_TIME=$(eval "$NOW")
$ echo $START_TIME
1335602898.78472
The previously posted function is obviously cleaner, but you said you wanted help with quoting.
By the way,
perl -e 'use Time::HiRes qw(time); print time'
can be shortened to
perl -MTime::HiRes=time -e'print time'
and even to the following (since the trailing new line is perfectly fine):
perl -MTime::HiRes=time -E'say time'
Or if you really wanted to golf:
perl -MTime::HiRes=time -Esay+time
Below is a modified version of your script. You basically need to understand that some applications have their standard output towards standard error (stderr), so when you don't see their output put in a variable, you just need to redirect it to standard output (stdout):
#/bin/sh
CALCULATE='bc'
echo 'starting'
NOW=$(perl -e 'use Time::HiRes qw(time); print time' 2>&1)
sleep 3
echo 'ending'
END_TIME=$(perl -e 'use Time::HiRes qw(time); print time' 2>&1)
ELAPSED_TIME=$(echo "($NOW - $START_TIME)")
echo $ELAPSED_TIME
I think the benefit of HiRes time is negated by the fact that perl is a relatively heavy external process and it is separately invoked two times.
If you don't need that many decimal places for the value. you can use the time builtin in Bash like
task() {
[Some long running task ...]
}
TIMEFORMAT=%R
elapse=$({ time task > task.out 2>&1; } 2>&1)
echo $elapse

How to get the default encoding of current OS in perl script?

How can I get the default encoding used by current platform?
Is there any available module in CPAN or with the distribution of Perl itself?
I can't find the solution in perl.org
See I18N::Langinfo.
$ LANG=en_US.UTF-8 perl -MI18N::Langinfo=langinfo,CODESET -E 'say langinfo(CODESET())'
UTF-8
$ LANG=C perl -MI18N::Langinfo=langinfo,CODESET -E 'say langinfo(CODESET())'
ANSI_X3.4-1968
$ LANG=ja_JP.eucjp perl -MI18N::Langinfo=langinfo,CODESET -E 'say langinfo(CODESET())'
EUC-JP
This is probably what you're looking for. If you follow the code in I18N::Langinfo, you can see how it discovers what locale to use for returning this.