存档

‘Perl’ 分类的存档

[ZT]Perl Command-Line Options

2009年10月21日 Galaxy 没有评论

http://www.perl.com/lpt/a/857
Perl Command-Line Options

By Dave Cross
August 10, 2004

Perl has a large number of command-line options that can help to make your programs more concise and open up many new possibilities for one-off command-line scripts using Perl. In this article we’ll look at some of the most useful of these.
阅读全文…

分类: Perl 标签: , ,

[ZT]How can I find out where a Perl module is installed?

2009年10月14日 Galaxy 没有评论

http://stackoverflow.com/questions/1557959/how-can-i-find-out-where-a-perl-module-is-installed/

How do get the path of a installed Perl module by name, e.g. Time::HiRes?
阅读全文…

分类: Perl 标签: , , ,

安装DBIx::Threaded

2009年7月25日 Galaxy 2 条评论

关键:DBIX_THRD_DSN='dbi:SQLite:dbname=t.sqlite' perl -MCPAN -eshell
剩下的,就是Mystory了……
阅读全文…

分类: Perl 标签: ,

PostgreSQL与标准SQL数据类型对应表

2008年9月28日 Galaxy 没有评论

__DATA__
## Format: for each type, there are 6 items, space separated:
## 1. The given name, from the Postgres source code (pg_type.h)
## 2. The function name we use to do the quoting, or 0 if we do not bind it
## 3. The function name we use for DE-quoting
## 4. The closest SQL_ datatype, or 0 if there is none. May be multiple, separated by |
## 5. Whether this is the one to use for reverse SQL_ type mapping
## 6. What type of SV we can put this in: 1: IV 2: NV

## Simple quoting (e.g. text) – wrap in single quotes, escape backslashes and apostrophes
## This is also the default action for types not specified here
varchar quote_string dequote_string SQL_VARCHAR 1 0
text quote_string dequote_string SQL_LONGVARCHAR 1 0
char quote_string dequote_char SQL_CHAR 0 0
bpchar quote_string dequote_char SQL_CHAR 1 0
cid quote_string dequote_string 0 0 0

## Things that get no quoting at all (e.g. numbers)
int2 null_quote null_dequote SQL_SMALLINT|SQL_TINYINT 1 1
int4 null_quote null_dequote SQL_INTEGER 1 1
int8 null_quote null_dequote SQL_BIGINT 1 0
float4 null_quote null_dequote 0 1 2
float8 null_quote null_dequote SQL_FLOAT|SQL_DOUBLE|SQL_NUMERIC|SQL_REAL 1 2
numeric null_quote null_dequote SQL_DECIMAL 1 2
oid null_quote null_dequote 0 0 1
name null_quote null_dequote SQL_VARCHAR 0 0 ## XXX Wrong

## Boolean
bool quote_bool dequote_bool SQL_BOOLEAN 1 0

## Geometric types
point quote_geom dequote_string 0 0 0
line quote_geom dequote_string 0 0 0
lseg quote_geom dequote_string 0 0 0
box quote_geom dequote_string 0 0 0
path quote_path dequote_string 0 0 0
polygon quote_geom dequote_string 0 0 0
circle quote_circle dequote_string 0 0 0

## Similar enough to geometric types that we use the same quoting rules
tid quote_geom dequote_string 0 0 0

## Binary – very different quoting rules
bytea quote_bytea dequote_bytea SQL_VARBINARY 1 0

## Time and date
date quote_string dequote_string SQL_TYPE_DATE 1 0
time quote_string dequote_string SQL_TYPE_TIME 1 0
timestamp quote_string dequote_string SQL_TIMESTAMP|SQL_TYPE_TIMESTAMP 1 0
timestamptz quote_string dequote_string SQL_TYPE_TIMESTAMP_WITH_TIMEZONE|SQL_TYPE_TIME_WITH_TIMEZONE 1 0

__END__

分类: Perl, Programming 标签: ,

[JS]Determining Your Visitor’s Time Zone

2008年9月20日 Galaxy 没有评论

http://www.willmaster.com/library/javascript/determining-your-visitors-time-zone.php

Determining Your Visitor’s Time Zone

You can capture your visitor’s time and time zone for your own information. And you can let your visitor know the current time on your business office clocks.

But why would you want to?

  • You can determine what time of day (their time) your visitors are most active (for your feedback forms, your page statistics, information for your marketing strategies, etc.) Do they visit your page soon after they get to the office? Maybe most do so on their lunch break? Or, possibly, your site is visited most often after their kids are put to bed?
  • If your visitors email you in the middle of your sleep period, they may understand not receiving an answer right away.
  • Your visitors can determine your office hours translated to their own time zone.
  • You can provide your visitor with that extra tidbit of information which you, yourself, have probably wanted to know when viewing someone’s web site: “Gee, I wonder what time it is where they’re from.”

The idea of time zones is a wonderful solution for coordinating time worldwide. However, when governments got their paws on it, it became confusing to the logical brain.

Explanations of time zones can be found at http://tycho.usno.navy.mil/tzones.html and http://times.clari.net.au/Australia.html

You can look up the Universal and local time for most regions of the world at http://www.cstv.to.cnr.it/toi/uk/timezone.html [Link no longer valid. Google may be able to find others.]

The JavaScript code in this article will put order into the confusions:

  • Your visitors’ time is the correct time. For them. Which probably is different than yours. And your web site could be on a server in a different time zone altogether. And, any could be on either side of the International Date Line.
  • (It took over a century for all countries to adopt the Gregorian Calendar. Thus, it may be a bit much to expect every political power to agree on what time it is.)

The answer is to base these time calculations on the clock and time zone information contained within the visitor’s own computer.

You know what time it is for you. Your visitors know what time it is for them. Now, both of you can know both.

There are two separate, independent, sets of code presented in this article. (1) How to gather your visitor’s time zone information. (2) How to present your visitor with your own local time.

Note: This article contains both JavaScript and HTML code. If you are reading this on an HTML-enabled email program, you may need to “view source” for all the code to be visible. As an alternative, you may view this article in the archives (which will present the code correctly in your browser). The archives are at /library/
(1) How to gather your visitor’s time zone information

Use your existing web site forms — feedback, subscription, recommend, etc. — and place a hidden form field into them. The hidden field gathers the information and transmits it to your CGI program whenever the submit button is clicked. The hidden field’s name can be anything your program can use.

The following works just fine with MasterFeedback. See the documentation of your CGI programs to determine whether they will accept additional hidden form fields and what the fields should be called; and modify the name=”…” portion as needed.

Insert the code anywhere between your <form> and </form> tags:

<script language="JavaScript">
<!-- 
var visitortime = new Date();
document.write('<input type="hidden" name="x-VisitorTimeZoneOffset" ');
if(visitortime) {
document.write('value="' + visitortime.getTimezoneOffset()/60 + '">');
}
else {
document.write('value="JavaScript not Date() enabled">');
}// -->
</script>
<noscript>
<input type="hidden" name="x-VisitorTimeZoneOffset" value="Browser not JavaScript enabled">
</noscript>

The above is 15 lines of code. JavaScript is line break sensitive. If your email program shows more than 15 lines of code, then please obtain your code from this issue’s archive, linked from /library/

If your visitor’s browser is JavaScript enabled and can handle the Date() function, then the code will provide a hidden field containing the number of hours your visitor’s location is from Greenwich Mean Time. Otherwise, the hidden field contains a descriptive message.

The number of hours contained in the hidden field is either positive or negative. If positive, it represents the number of hours earlier than Greenwich Mean Time. If negative, later. (Note: This is opposite of most time zone maps; an idiosyncrasy of JavaScript.)

http://swissinfo.net/cgi/worldtime/ and http://www.worldtimezone.com/ provide time zone maps from which you can view your visitor’s geographical time zone location.

FYI, MasterFeedback is free and can be obtained from /software/feedback

(2) How to present your visitor with your own local time

This requires placing JavaScript in two places on your HTML page: (i) between your <head> and </head> tags, and (ii) between your <body> and </body> tags.

It also requires you to customize one line. To do that, you need to know how many minutes you are from Greenwich Mean Time. The number could be either positive or negative. If your time is earlier than Greenwich Mean Time then the number is positive. If your time is later, the number is negative. (Again, this is opposite of most time zone maps; an idiosyncrasy of JavaScript.)

Get the number quick and easy! Place the following 15 lines of code into a web page and load the page into your browser:

<HTML>
<HEAD>
</HEAD>
<BODY>
The line should read:
<pre>
<script language="JavaScript">
<!-- 
var offset = new Date();
document.write('var officeoffset = ' + offset.getTimezoneOffset() + ';');
// -->
</script>
</ pre>
</BODY>
</HTML>

Either way, you will have obtained the number you need to customize the one line.

The JavaScript code presented below will display timezone information on your page.

Here is the first part, which belongs between your <head> and </head> tags (the third line will need to be customized):

<script language="JavaScript">
<!-- 
var officeoffset = 300;
browser = (parseInt(navigator.appVersion) >= 4)
var weekdays=new Array("Sunday","Monday","Tuesday","Wednesday","Thursday","Friday","Saturday");
if(! weekdays) { browser = 0; }
if(browser) {
var visitortime = new Date();
var visitoroffset = visitortime.getTimezoneOffset();
if(visitoroffset < 0) { visitoroffset = Math.abs(visitoroffset) + 720; }
else { visitoroffset = 720 - visitoroffset; }
if(officeoffset < 0) { officeoffset = Math.abs(officeoffset) + 720; }
else { officeoffset = 720 - officeoffset; }
var diff = officeoffset - visitoroffset;
var officetime = new Date();
officetime.setTime(Number(visitortime) + (diff * 60000));
} // -->
</script>

On the third line of the above code, you see the number 300. That is the number which must be changed to reflect the number of minutes you are from Greenwich Mean Time.

The next 18 lines of code belong between your <body> and </body> tags:

<script language="JavaScript">
<!-- 
if(browser) {
document.write('<table><tr><td align="right>');"
document.write("Your Local Time:</td><td><b>");
document.write(visitortime.getHours() + ':');
if(visitortime.getMinutes() < 10) { document.write('0'); }
document.write(visitortime.getMinutes()); 
document.write('</b> on ' + weekdays[visitortime.getDay()]);
document.write('</td></tr><tr><td align="right>');"
document.write("Web site Business Office:</td><td><b>");
document.write(officetime.getHours() + ':');
if(officetime.getMinutes() < 10) { document.write('0'); }
document.write(officetime.getMinutes()); 
document.write('</b> on ' + weekdays[officetime.getDay()]);
document.write('</td></tr></table>');
} // -->
</script>

Place the above code wherever you want to display the data.

Will Bontrager
©1999 Bontrager Connection, LLC

分类: Perl, Programming 标签:

[Note]Perl表头处理

2008年7月16日 Galaxy 没有评论

http://bbs.chinaunix.net/thread-1200880-1-1.html

root@lynch /cgi/ajax/>mpstat
Linux 2.6.18 (lynch)    2008年07月16日
 
1218分00秒  CPU   %user   %nice    %sys %iowait    %irq   %soft  %steal   %idle    intr/s
1218分00秒   all      14.49     0.00      1.06    0.49       0.05     0.02     0.00     83.89    287.70

要把每一个带%的项的值取出来,比如说要取出%idle的值83.89
最开始用数组的元素来取, $array[9];
但有的系统mpstat的输出会少一项,数组的元素就变了,
要取出%idle的值就要用$array[8]了。

现在用哈希表来取数据,将有CPU的行作为key,将有all的行作为value,
下面是代码

#!/usr/local/bin/perl
use strict;
use warnings;
 
my $mpstat = '/usr/bin/mpstat';
my ($hash, @keys, @values);
 
open(PIPE, "$mpstat |") or die $!;
   while (
 ) {
      chomp($_);
      if ( /CPU/ ) { @keys   = split(/\s+/, $_) }
      if ( /all/ ) { @values = split(/\s+/, $_) }
   }
close(PIPE) or die $!;
 
my $i = 0;
 
foreach ( @keys ) {
   $hash->{ $keys[$i] } = $values[$i];
   ++$i;
}
 
print "CPU IDLE: $hash->{'%idle'}%\n";

不知道有没有更好的方法,还请各位赐教!

[ 本帖最后由 lin_wang 于 2008-7-16 12:56 编辑 ]

@h{@k} = @v;


my (%hash, @keys, @values);
@hash{@keys} = @values;
print "CPU IDLE: $hash{'%idle'}%\n";


D:\MoChou>cat ttt.pl

use Data::Dumper;
 
while(){
    next unless /CPU/;
    my @field = split;
    $_ = ;
    my @value = split;
 
    my @index = grep { $field[$_] =~ /^%/ and $field[$_] = $' } 0..$#field;
    my %hash;
    @hash{ @field[@index] } = @value[@index];
 
    print Dumper( \%hash );
}
__END__
Linux 2.6.18 (lynch)    2008年07月16日
 
1218分00秒  CPU   %user   %nice    %sys %iowait    %irq   %soft  %steal   %idle    intr/s
1218分00秒   all      14.49     0.00      1.06    0.49       0.05     0.02 0.00     83.89    287.70
D:\MoChou>ttt

$VAR1 = {
          'sys' => '1.06',
          'soft' => '0.02',
          'idle' => '83.89',
          'nice' => '0.00',
          'irq' => '0.05',
          'steal' => '0.00',
          'user' => '14.49',
          'iowait' => '0.49'
        };
分类: Perl, Stories 标签: , , ,

如何从文件中每次读取固定数目的行

2008年7月16日 Galaxy 没有评论

如何从文件中每次读取固定数目的行
http://bbs.chinaunix.net/viewthread.php?tid=1213772&pid=8842204&page=1&extra=page%3D1#pid8842204


package MyFileIO;
use base 'IO::File';
sub get_n_lines {
        my $self = shift;
        my $lines = shift || 1;  #default for 1 line.
        if (wantarray) {
                my @t;
                for (1 .. $lines) {
                        push @t, $self->getline;
                }
                return @t;
        }
        my $t;
        for (1 .. $lines) {
                $t .= $self->getline;
        }
        return $t;
}
1;

——

#! /usr/bin/env perl
use strict;
use warnings;
use MyFileIO;
my $file = MyFileIO->new('test.pl') or die;
my $test = $file->get_n_lines(10);
print $file->get_n_lines(10);
print $test;

分类: Perl 标签: ,

怎样用Perl实现CLI下光标旋转

2008年7月6日 Galaxy 没有评论

http://bbs.chinaunix.net/thread-1187443-1-1.html
怎样用perl实现dos提示符下,让那个光标旋转的程序

#!/usr/bin/perl
$|++;
my @c = qw! - \ | / !;
#
my $i = 0;
my $j = 0;
print ". ";
while (1) {
select(undef, undef, undef, 0.2);
print "\b", $c[$i];
$i++;
$j++;
print "\b. " unless ($j % 10);
$i = 0 if ($i > 3);
}

我把 dajun高手的代码改了一小下,这回符合我的要求了,看起来很好玩的样子,感谢大家的帮助,尤其感谢dajun!

#!/usr/bin/perl
$|++;
my @c = qw! - \ | / !;
#
my $i = 0;
my $j = 0;
#
$aa = qx(cd);
chomp $aa;
#
print $aa;
print ">";
print " ";
#
while (1) {
select(undef, undef, undef, 0.2);
#
print "\b", $c[$i];
#print $c[$i];
$i++;
#$j++;
#print "\b. " unless ($j % 10);
$i = 0 if ($i > 3);
}

http://perldoc.perl.org/functions/select.html
Note: not all implementations are capable of returning the $timeleft. If not, they always return $timeleft equal to the supplied $timeout.
You can effect a sleep of 250 milliseconds this way:

select(undef, undef, undef, 0.25);

分类: Perl 标签: , , ,
Locations of visitors to this page