LinuxSir.Org  
| 网站首页 | 论坛帮助 |

欢迎来到LinuxSir.Org!
您还未登录,请登录后查看论坛,或者点击论坛上方的注册链接注册新账号。


发表新主题 回复
置顶的主题 精华主题  
主题工具
旧 03-10-25, 11:16 第 61 帖
devel
 
devel 的头像
 
 
已封禁  
  注册日期: Sep 2003
  我的住址: 自由的世界 !
  帖子: 1,472
  精华: 6
 

对不起,那个脚本严重不行,今天再试多几次,发现漏了一次检验了。发表了这样的脚本,向大家道歉,:o:o现在我改了,此脚本只适用于正整数的转换。不知道那里还有漏洞的,请大家指出。

#!/usr/bin/perl
#author:devel
print "input a 2 format number :\n";
chomp($a=<STDIN>);
if ( $a >=10) {
while ( $a >= 10)
{
$A=&yu ($a);
$result += 2**$A;
$a -=10**$A;
}
$result += $a % 10 ;
print "$result\n";
}
else {
print "$a\n";
}
sub yu { #这个子函数的作用是计算出有多少位的十进制正整数。
$k=$a,$i=0;
while ( $k >= 10 )
{
$e= $k % 10;
exit if ( $k !~ /[01]/ );
$k -= $e;
$k/=10;
++$i;
}
exit if ($k !~ /[01]/ ); #加了这里
return "$i"; #返回值就是位数了。
}

此帖于 03-10-25 16:08 被 devel 编辑.
  devel 当前离线   回复时引用此帖
旧 03-10-25, 19:08 第 62 帖
devel
 
devel 的头像
 
 
已封禁  
  注册日期: Sep 2003
  我的住址: 自由的世界 !
  帖子: 1,472
  精华: 6
 

请问new()这个函数有什么用:ask
  devel 当前离线   回复时引用此帖
旧 03-11-06, 16:22 第 63 帖
home
 
 
 
已封禁  
  注册日期: Nov 2003
  帖子: 1,150
  精华: 5
 

想写一个类似于web爬虫一类的程序,定期检查站点url的可连接性,但是对于那些要求登陆后才可见的web页,我怎样才能让它自动登陆,进行检查呢?
  home 当前离线   回复时引用此帖
旧 03-11-07, 11:39 第 64 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

7楼和8楼的问题,我看还是让咱们的perl高手来解决吧~~
  KornLee 当前离线   回复时引用此帖
旧 03-11-07, 21:00 第 65 帖
大水缸
 
 
 
注册会员  
  注册日期: Jul 2003
  帖子: 12
  精华: 0
 

标题: 我不是高手...


引用:
请问new()这个函数有什么用
29.2.100. new

new CLASSNAME LIST
new CLASSNAME

There is no built-in new function. It is merely an ordinary constructor method (that is, a user-defined subroutine) that is defined or inherited by the CLASSNAME class (that is, package) to let you construct objects of type CLASSNAME. Many constructors are named "new", but only by convention, just to trick C++ programmers into thinking they know what's going on. Always read the documentation of the class in question so you know how to call its constructors; for example, the constructor that creates a list box in the Tk widget set is just called Listbox().

另:Perl提供内置的制式转换函数
引用:
一个二进制转为十进制的脚本
29.2.103. oct

oct EXPR
oct
This function interprets EXPR as an octal string and returns the equivalent decimal value. If EXPR happens to start with "0x", it is interpreted as a hexadecimal string instead. If EXPR starts off with "0b", it is interpreted as a string of binary digits. The following will properly convert to numbers any input strings in decimal, binary, octal, and hex bases written in standard C or C++ notation:
$val = oct $val if $val =~ /^0/;
To perform the inverse function, use sprintf with an appropriate format:


$perms = (stat("filename"))[2] & 07777;
$oct_perms = sprintf "%lo", $perms;
The oct function is commonly used when a data string such as "644" needs to be converted into a file mode, for example. Although Perl will automatically convert strings into numbers as needed, this automatic conversion assumes base 10.

引用:
想写一个类似于web爬虫一类的程序,定期检查站点url的可连接性,但是对于那些要求登陆后才可见的web页,我怎样才能让它自动登陆,进行检查呢?
你可以看看perl-CGI类参考书中有关HTTP协议的章节。
  大水缸 当前离线   回复时引用此帖
旧 03-11-08, 20:54 第 66 帖
home
 
 
 
已封禁  
  注册日期: Nov 2003
  帖子: 1,150
  精华: 5
 

我想通过perl来控制串口,从交换机中读取信息,但是怎样从串口中读入信息呢?有没有资料,或者图书什么的?
  home 当前离线   回复时引用此帖
旧 03-11-09, 10:51 第 67 帖
大水缸
 
 
 
注册会员  
  注册日期: Jul 2003
  帖子: 12
  精华: 0
 

如果你有C语言进行这类操作的经验,应该知道基本的思路,用Perl的思路是一样的,可以查阅有关系统IO调用的Perl文档,还可以去CPAN参考一下IO::Stty、Device::ParallelPort 、Device::SerialPort 等文档。
  大水缸 当前离线   回复时引用此帖
旧 03-11-11, 16:40 第 68 帖
devel
 
devel 的头像
 
 
已封禁  
  注册日期: Sep 2003
  我的住址: 自由的世界 !
  帖子: 1,472
  精华: 6
 

一个从正整数的10进制转到正整数的8进制的perl脚本,有什么漏洞请大家指出


#!/usr/bin/perl
$i=0;
print "input a 10 format num: \n";
chomp($n=<>);
if ( $n > 7 )
{
$c=0;
while ( $n >= 8 )
{
$i=$n % 8;
$r += (10 ** $c) * $i;
$c++;
$n=( $n - $n % 8 ) /8 ;
}
$r += (10 ** $c) * $n;
print "0$r\n";
}
else
{
print "0$n\n";
}

改一下,也可以变成从正整数的10进制转为2进制的,或者3.4.5.6.7,8,9进制都行但计算机并不需要这些。呵呵~~~~


#!/usr/bin/perl
print "input a 10 format num: \n";
chomp($n=<>);
if ( $n > 1 )
{
$c=0;
while ( $n >= 2 )
{
$i=$n % 2;
$r += (10 ** $c) * $i;
$c++;
$n=( $n - $n % 2 ) /2 ;
}
$r += (10 ** $c) * $n;
print "$r\n";
}
else
{
print "$n\n";
}
  devel 当前离线   回复时引用此帖
旧 03-11-12, 16:43 第 69 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

devle一露面就出手不凡
  KornLee 当前离线   回复时引用此帖
旧 03-11-12, 23:33 第 70 帖
KornLee
 
 
 
★☆★☆★☆★  
  注册日期: Nov 2002
  我的住址: LinuxWorld
  帖子: 6,960
  精华: 61
 

引用:
最初由 home 发表
who is devle ?

大水缸兄,我推荐你作版主,
,支持~~~,我9号给他发了短消息~~,希望他能来~~
  KornLee 当前离线   回复时引用此帖
旧 03-11-19, 13:50 第 71 帖
home
 
 
 
已封禁  
  注册日期: Nov 2003
  帖子: 1,150
  精华: 5
 

This script come from internet:

一个简单的FTP镜像脚本,它递归地将一个本地目录和远程目录做比较,,并将新地或更新过的文件拷贝到本地机器,保持目录的结构,脚本在本地拷贝中保持文件的模式,也尽力保持符号链接。

为了从远程服务器镜像文件和目录,以命令行参数调用这个脚本,命令行参数的组成为远程服务器的DNS名字,一个冒号,以及要镜像的文件或目录的路径。下面的例子镜像文件RECENT,只在从最后一次镜像该文件之后且文件又被改变的情况下才将它拷贝到本地目录:
$ftp_mirror.pl ftp.perl.org:/pub/CPAN/RECENT
下一个例子镜像CPAN模块目录的全部内容,递归地将远程目录结构拷贝到当前本地工作目录中(不要逐字地用这条命令,除非你地网络连接非常快速并且你具又很多地FREE磁盘空间);

$ftp_mirror.pl ftp.perl.org:/pub/CPAN

这个脚本地命令行选项包括--user和--pass,为非匿名FTP提供用户和密码;还包括--verbose,获取详细地状态报告;以及--hash,在文件传输过程中打印出散列标记。
----------------start

#!/usr/bin/perl -w
# file: ftp_mirror.pl
# Figure 6.2: Recursively mirroring an FTP directory

use strict;
use Net::FTP;
use File::Path;
use Getopt::Long;

use constant USAGEMSG => <<USAGE;
Usage: ftp_mirror.pl [options] host:/path/to/directory
Options:
--user <user> Login name
--pass <pass> Password
--hash Progress reports
--verbose Verbose messages
USAGE

my ($USERNAME,$PASS,$VERBOSE,$HASH);

die USAGEMSG unless GetOptions('user=s' => \$USERNAME,
'pass=s' => \$PASS,
'hash' => \$HASH,
'verbose' => \$VERBOSE);
die USAGEMSG unless my ($HOST,$PATH) = $ARGV[0]=~/(.+):(.+)/;

my $ftp = Net::FTP->new($HOST) or die "Can't connect: $@\n";
$ftp->login($USERNAME,$PASS) or die "Can't login: ",$ftp->message;
$ftp->binary;
$ftp->hash(1) if $HASH;

do_mirror($PATH);

$ftp->quit;
exit 0;

# top-level entry point for mirroring.
sub do_mirror {
my $path = shift;

return unless my $type = find_type($path);

my ($prefix,$leaf) = $path =~ m!^(.*?)([^/]+)/?$!;
$ftp->cwd($prefix) if $prefix;

return get_file($leaf) if $type eq '-'; # ordinary file
return get_dir($leaf) if $type eq 'd'; # directory

warn "Don't know what to do with a file of type $type. Skipping.";
}

# mirror a file
sub get_file {
my ($path,$mode) = @_;
my $rtime = $ftp->mdtm($path);
my $rsize = $ftp->size($path);
$mode = (parse_listing($ftp->dir($path)))[2] unless defined $mode;

my ($lsize,$ltime) = stat($path) ? (stat(_))[7,9] : (0,0);
if ( defined($rtime) and defined($rsize)
and ($ltime >= $rtime)
and ($lsize == $rsize) ) {
warn "Getting file $path: not newer than local copy.\n" if $VERBOSE;
return;
}

warn "Getting file $path\n" if $VERBOSE;
$ftp->get($path) or (warn $ftp->message,"\n" and return);
chmod $mode,$path if $mode;
}

# mirror a directory, recursively
sub get_dir {
my ($path,$mode) = @_;
my $localpath = $path;
-d $localpath or mkpath $localpath or die "mkpath failed: $!";
chdir $localpath or die "can't chdir to $localpath: $!";
chmod $mode,'.' if $mode;

my $cwd = $ftp->pwd or die "can't pwd: ",$ftp->message;
$ftp->cwd($path) or die "can't cwd: ",$ftp->message;

warn "Getting directory $path/\n" if $VERBOSE;

foreach ($ftp->dir) {
next unless my ($type,$name,$mode) = parse_listing($_);
next if $name =~ /^(\.|\.\.)$/; # skip . and ..
get_dir ($name,$mode) if $type eq 'd';
get_file($name,$mode) if $type eq '-';
make_link($name) if $type eq 'l';
}

$ftp->cwd($cwd) or die "can't cwd: ",$ftp->message;
chdir '..';
}

# subroutine to determine whether a path is a directory or a file
sub find_type {
my $path = shift;
my $pwd = $ftp->pwd;
my $type = '-'; # assume plain file
if ($ftp->cwd($path)) {
$ftp->cwd($pwd);
$type = 'd';
}
return $type;
}

# Attempt to mirror a link. Only works on relative targets.
sub make_link {
my $entry = shift;
my ($link,$target) = split /\s+->\s+/,$entry;
return if $target =~ m!^/!;
warn "Symlinking $link -> $target\n" if $VERBOSE;
return symlink $target,$link;
}

# parse directory listings
# -rw-r--r-- 1 root root 312 Aug 1 1994 welcome.msg
sub parse_listing {
my $listing = shift;
return unless my ($type,$mode,$name) =
$listing =~ /^([a-z-])([a-z-]{9}) # -rw-r--r--
\s+\d* # 1
(?:\s+\w+){2} # root root
\s+\d+ # 312
\s+\w+\s+\d+\s+[\d:]+ # Aug 1 1994
\s+(.+) # welcome.msg
$/x;
return ($type,$name,filemode($mode));
}

# turn symbolic modes into octal
sub filemode {
my $symbolic = shift;
my (@modes) = $symbolic =~ /(...)(...)(...)$/g;
my $result;
my $multiplier = 1;
while (my $mode = pop @modes) {
my $m = 0;
$m += 1 if $mode =~ /[xsS]/;
$m += 2 if $mode =~ /w/;
$m += 4 if $mode =~ /r/;
$result += $m * $multiplier if $m > 0;
$multiplier *= 8;
}
$result;
}
  home 当前离线   回复时引用此帖
旧 03-11-19, 13:55 第 72 帖
home
 
 
 
已封禁  
  注册日期: Nov 2003
  帖子: 1,150
  精华: 5
 

一个简单的下载单个文件的脚本。来自internet.
#!/usr/bin/perl -w
# file: ftp_recent.pl
# Figure 6.1: Downloading a single file with Net::FTP

use Net::FTP;

use constant HOST => 'ftp.perl.org';
use constant DIR => '/pub/CPAN';
use constant FILE => 'RECENT';

my $ftp = Net::FTP->new(HOST) or die "Couldn't connect: $@\n";
$ftp->login('anonymous') or die $ftp->message;
$ftp->cwd(DIR) or die $ftp->message;
$ftp->get(FILE) or die $ftp->message;
$ftp->quit;

warn "File retrieved successfully.\n";
  home 当前离线   回复时引用此帖
旧 03-11-19, 14:32 第 73 帖
home
 
 
 
已封禁  
  注册日期: Nov 2003
  帖子: 1,150
  精华: 5
 

来自internet

用FTP下载一个文件。
#!/usr/bin/perl
# File: FTP_download.pl
use CGI ':standard';
use Win32::Internet;

$inet = new Win32::Internet();
$inet->FTP($FTP,'192.69.150.1','mlavery','jukilo0');

$FTP->Ascii(); #set the transfer to ASCII mode;
$FTP->Cd('/nfsexport/phone');
$FTP->Get('phone.txt');
$inet->close;
  home 当前离线   回复时引用此帖
旧 03-11-21, 14:26 第 74 帖
home
 
 
 
已封禁  
  注册日期: Nov 2003
  帖子: 1,150
  精华: 5
 

(转)安装一个perl模块的方法

安装一个模块(如:Crypt::Blowfish 加密功能的)
先在系统上用perldoc Crypt::Blowfish 看模块是否有
如默认没有被安装则去(http://www.perl.com/CPAN/、http://s...块(用wget http://search.cpan.org/CPAN/...........)
解压
进入模块目录,看README查是否需要其他模块的支持(本模块需要先安装 Crypt::Rijndael、 Crypt::CBC两个)
安装
1. perl Makefile.PL
2. make
3. make test
4. make install
即可
(可在用perldoc 模块 来验证啊!)
  home 当前离线   回复时引用此帖
旧 03-11-22, 14:44 第 75 帖
home
 
 
 
已封禁  
  注册日期: Nov 2003
  帖子: 1,150
  精华: 5
 

请大家帮看看。。我这程序错在那里?

#!/usr/bin/perl -w
$d=10;
print "\$d\=10\;\n$d\n";
@b=(1,2,3,9);
print "@b\n";
foreach $d (@b)
{
print "$d\n";
}
print "@b\n";

原来是打错单词了。。。。:o

此帖于 03-11-22 15:04 被 home 编辑.
  home 当前离线   回复时引用此帖
发表新主题 回复


主题工具

发帖规则
您 [不可以] 发表新主题
您 [不可以] 回复主题
您 [不可以] 上传附件
您 [不可以] 编辑您的帖子

已 [启用] BB 代码
已 [启用] 表情符号
已 [启用] IMG 代码
已 [禁用] HTML 代码
[论坛跳转…]


所有时间均为[北京时间]。现在的时间是 11:55


Powered by vBulletin 版本 3.6.8
版权所有 ©2000 - 2012, Jelsoft Enterprises Ltd.
官方中文技术支持: vBulletin 中文
版权所有 ©2002 - 2011, LinuxSir.Org