RE: if Question
Hi Vance, This sounds silly, but Why do you need to know? If your operation is as simple as trying to decide whether or not to insert a comma or other separator, you can do that with the JOIN command without checking the individual values. $result = join(',' ($var1, $var2)); $result will be: or $var1 or $var2 or $var1,$var2 You also have the inline if: $result = ($var1 ne ? ($var2 ne ? Both : Var1Only) : ($var2 ne ? Var2Only : Neither); $result will be one of: Both, Var1Only, Var2Only, Neither David Luke, Application Developer IV Isocorp, Inc. DMS Enterprise Information Technology Services 100 Rhyne Building 2740 Centerview Drive Tallahassee, Florida 32301 (850) 216-3746 -Original Message- From: Vance M. Allen [mailto:[EMAIL PROTECTED] Sent: Sunday, September 25, 2005 2:42 AM To: beginners-cgi@perl.org Subject: if Question Greetings, I have several places in my code where I need to check if two string variables are empty, if one is empty and the other is not, and if they're both populated. Is there any simpler way to write the code than this?: if($var1 ne and $var2 ne ) { # Both are populated... # ... } elsif($var1 ne or $var2 ne ) { # One or the other is populated... # ... } else { # Neither are populated... # ... } The reason I ask is that some of the code gets to be fairly long when I'm checking things this way, and so I just wondered if there was any shorthand/shortcut way to have Perl check if two strings are empty in one expression...regular expressions, maybe? I appreciate any help, suggestions, or answer you can provide. Thanks, Vance -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: how to use perl modules
--- test.pm --- package test; sub ReturnValue() { my $a=10; return ($a); } 1; # as a module usually has a return value --- call.pl --- use test; print test::ReturnValue(); # specify the package name where the function resides #or u could try this also $tmp=test; print $tmp-ReturnValue(); #- jabir --- Abhishek Dave [EMAIL PROTECTED] wrote: hello all, i've wrote a small test subroutine in my file name test.pm and in the same directory i worte a perl file in which i've to call this subroutine in this perl program I dont know how to use perl modules pls let me knwo abt that as it will solve my lots of problems test.pm sub ReturnValue() { my $a=10; return ($a); } call.pl use test; print ReturnValue(); --I don't know abt the moduls in perl Pls help me in this regard asap waiting for reply from perl group Thanks Beginner I do the diffcult immediately,but the impossible take's a little longer!!! Cresent canaopy apartments #19 Davis Road, Thomas town, Bangalore -84 Phone (mobile) : +91-98867 01148 E-Mail: [EMAIL PROTECTED],[EMAIL PROTECTED] __ Yahoo! Mail - PC Magazine Editors' Choice 2005 http://mail.yahoo.com -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: how to make charts using GD
Hello, Aditi Gupta [EMAIL PROTECTED] asked: I have a perl code that generates a sequence of values which i want to plot on y-axis against 1,2,3... on x-axis. I've read that GD::Graph could be used for it. But I've never done modular programming. Also, I don't know how to install modules from CPAN. Are there any other easier methods for drawing charts using perl? Well, you could always use an external program like gnuplot to generate the graph for you. That would still require that you understand how to use the program, though. And don't be afraid to use Perl modules. Many important modules are already part of your Perl distribution, so you would not have to install them manually, anyways. As for the rest, I'm sure people here will be able to give you some pointers if you provide some details like OS and distribution used, and so on. HTH, Thomas -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: how to make charts using GD
On Wed, 2005-09-28 at 08:10 +0200, Thomas Bätzler wrote: Hello, Aditi Gupta [EMAIL PROTECTED] asked: I have a perl code that generates a sequence of values which i want to plot on y-axis against 1,2,3... on x-axis. I've read that GD::Graph could be used for it. But I've never done modular programming. Also, I don't know how to install modules from CPAN. Are there any other easier methods for drawing charts using perl? Well, you could always use an external program like gnuplot to generate the graph for you. That would still require that you understand how to use the program, though. Hello, GD::Graph is very simple to use, just check out the CPAN documents. If you want to use gnuplot, try the following little c shell script. set data = $argv[1] gnuplot EOF set xlable 'x axis' set ylable 'y label' set title 'title' set terminal postscript color plot $data using x:y quit EOF The thing with gnuplot is the vast number of options you have, this can work out to be a good thing, you could dump in any function you like in that script. Another option is XMgrace, which is a nice plotting program which is perhaps easier to use than gnuplot, after that there is matlab and other commercial tools you could use. I use GD as it is quick and simple. Installing the module: If you are on windows, making the assumption you are running Active State perl, use the PPM to install GD::Graph On linux just do perl -MCPAN -e 'shell' follow the instructions and then type: install GD::Graph and it should all be fine. Hope that is of some use. Dan. And don't be afraid to use Perl modules. Many important modules are already part of your Perl distribution, so you would not have to install them manually, anyways. As for the rest, I'm sure people here will be able to give you some pointers if you provide some details like OS and distribution used, and so on. HTH, Thomas -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
File Existance
Hi there! I have a lot of experience in programming, but WHAT command do I use for checking if a file exists... Have searched google, but can't seem to find any good answers... /G http://www.varupiraten.se/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: File Existance
-e filename Regards, Ganesh [EMAIL PROTECTED] wrote: Hi there! I have a lot of experience in programming, but WHAT command do I use for checking if a file exists... Have searched google, but can't seem to find any good answers... /G http://www.varupiraten.se/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: File Existance
(Bringing this back to the list) On Wednesday 28 September 2005 21:09, you wrote: I don't get this to work. What am I doing wrong? Must the path be included? My program is like this: my $program = ls; if (-e $program) { #execute program } yes, it must include the full path: foreach ('ls', '/bin/ls') { if (-e $_) { print $_ exists\n; } else { print $_ doesn't exist\n; } } Prints: ls doesn't exist /bin/ls exists -- Robin [EMAIL PROTECTED] JabberID: [EMAIL PROTECTED] Hostes alienigeni me abduxerunt. Qui annus est? PGP Key 0xA99CEB6D = 5957 6D23 8B16 EFAB FEF8 7175 14D3 6485 A99C EB6D pgp7xC3dEg0Ui.pgp Description: PGP signature
RE: general retry function
Chris Devers wrote: while ( $tries 10 ) { my $result = do_something_risky(); break if ( $result != 0 ); last, not break -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: WELCOME to beginners@perl.org
Any idea how to install Spreadsheet::ParseExcel? I've downed loaded the required modules, but it still does not seem to work. Are there clear instructions anywhere? tia, dk -Original Message- From: [EMAIL PROTECTED] Sent: Sep 28, 2005 11:08 AM To: [EMAIL PROTECTED] Subject: WELCOME to beginners@perl.org Hi! This is the ezmlm program. I'm managing the beginners@perl.org mailing list. I'm working for my owner, who can be reached at [EMAIL PROTECTED] Acknowledgment: I have added the address [EMAIL PROTECTED] to the beginners mailing list. Welcome to [EMAIL PROTECTED] Please save this message so that you know the address you are subscribed under, in case you later want to unsubscribe or change your subscription address. To unsubscribe, send a message to: [EMAIL PROTECTED] --- Administrative commands for the beginners list --- I can handle administrative requests automatically. Please do not send them to the list address! Instead, send your message to the correct command address: For help and a description of available commands, send a message to: [EMAIL PROTECTED] To subscribe to the list, send a message to: [EMAIL PROTECTED] To remove your address from the list, just send a message to the address in the ``List-Unsubscribe'' header of any list message. If you haven't changed addresses since subscribing, you can also send a message to: [EMAIL PROTECTED] or for the digest to: [EMAIL PROTECTED] For addition or removal of addresses, I'll send a confirmation message to that address. When you receive it, simply reply to it to complete the transaction. If you need to get in touch with the human owner of this list, please send a message to: [EMAIL PROTECTED] Please include a FORWARDED list message with ALL HEADERS intact to make it easier to help you. --- Enclosed is a copy of the request I received. Return-Path: [EMAIL PROTECTED] Received: (qmail 20718 invoked from network); 28 Sep 2005 15:08:08 - Received: from x1a.develooper.com (HELO x1.develooper.com) (216.52.237.111) by lists.develooper.com with SMTP; 28 Sep 2005 15:08:08 - Received: (qmail 6797 invoked by uid 225); 28 Sep 2005 15:08:08 - Delivered-To: [EMAIL PROTECTED] Received: (qmail 6792 invoked by alias); 28 Sep 2005 15:08:07 - X-Spam-Status: No, hits=0.6 required=8.0 tests=BAYES_00,DOMAIN_RATIO,NO_REAL_NAME X-Spam-Check-By: la.mx.develooper.com Received-SPF: pass (x1.develooper.com: local policy) Received: from pop05.mail.atl.earthlink.net (HELO pop05.mail.atl.earthlink.net) (207.69.200.58) by la.mx.develooper.com (qpsmtpd/0.28) with ESMTP; Wed, 28 Sep 2005 08:08:04 -0700 Received: from mswamui-bichon.atl.sa.earthlink.net ([209.86.224.26]) by pop05.mail.atl.earthlink.net with esmtp (Exim 3.36 #10) id 1EKdXN-00022E-00 for [EMAIL PROTECTED]; Wed, 28 Sep 2005 11:08:01 -0400 Message-ID: [EMAIL PROTECTED] Date: Wed, 28 Sep 2005 11:08:00 -0400 (GMT-04:00) From: [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: [EMAIL PROTECTED] Subject: Re: confirm subscribe to beginners@perl.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Earthlink Zoo Mail 1.0 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: WELCOME to beginners@perl.org
On Wed, 28 Sep 2005 [EMAIL PROTECTED] wrote: Any idea how to install Spreadsheet::ParseExcel? Yes. I've downed loaded the required modules, but it still does not seem to work. Bummer. Are there clear instructions anywhere? Yes. -- Chris Devers 9wkö¦QC½lD -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: WELCOME to beginners@perl.org
Just something for to start looking for info on the module http://search.cpan.org/~kwitknr/Spreadsheet-ParseExcel-0.2602/ParseExcel.pm Armando Gomez Guajardo Process Engineer Work Ph 956 547 6438 Beeper956 768 4070 -Original Message- From: Chris Devers [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 28, 2005 11:10 AM To: [EMAIL PROTECTED] Cc: Perl Beginners List Subject: Re: WELCOME to beginners@perl.org On Wed, 28 Sep 2005 [EMAIL PROTECTED] wrote: Any idea how to install Spreadsheet::ParseExcel? Yes. I've downed loaded the required modules, but it still does not seem to work. Bummer. Are there clear instructions anywhere? Yes. -- Chris Devers 9(tm)wkö¦QC½lD -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: WELCOME to beginners@perl.org
-Original Message- From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] Sent: Wednesday, September 28, 2005 12:08 PM To: beginners@perl.org Subject: Re: WELCOME to beginners@perl.org Any idea how to install Spreadsheet::ParseExcel? Just for giggles, I'm gonna assume that you're using PPM (you gave nothing else to go on)... I've only installed this particular module using ActiveState's 'ppm' myself; it seems to have problems with the '::' in module names. If ppm can't find what it believes the module name is, it will default to a search and probably return something like this: ppm search spreadsheet::parseexcel Searching in Active Repositories 1. Spreadsheet-ParseExcel [0.2603] Get information from Excel file 2. Spreadsheet-ParseExcel-Sim~ [1.02] A simple interface to Excel data 3. Spreadsheet-ParseExcel_XLH~ [0.02] Parse Excel Spreadsheets using xlhtml Use the 'install' command and substitute '-' for '::' like so: ppm search Spreadsheet-ParseExcel-Simple cpan (on *nix boxen) is normally a breeze to use too. If this doesn't help, consult the documentation for ppm, cpan, or other applicable mechanism through which you install your modules. Most folks on this list are going to require that you provide a little more information in order to help. ry I've downed loaded the required modules, but it still does not seem to work. Are there clear instructions anywhere? tia, dk -Original Message- From: [EMAIL PROTECTED] Sent: Sep 28, 2005 11:08 AM To: [EMAIL PROTECTED] Subject: WELCOME to beginners@perl.org Hi! This is the ezmlm program. I'm managing the beginners@perl.org mailing list. I'm working for my owner, who can be reached at [EMAIL PROTECTED] Acknowledgment: I have added the address [EMAIL PROTECTED] to the beginners mailing list. Welcome to [EMAIL PROTECTED] Please save this message so that you know the address you are subscribed under, in case you later want to unsubscribe or change your subscription address. To unsubscribe, send a message to: [EMAIL PROTECTED] --- Administrative commands for the beginners list --- I can handle administrative requests automatically. Please do not send them to the list address! Instead, send your message to the correct command address: For help and a description of available commands, send a message to: [EMAIL PROTECTED] To subscribe to the list, send a message to: [EMAIL PROTECTED] To remove your address from the list, just send a message to the address in the ``List-Unsubscribe'' header of any list message. If you haven't changed addresses since subscribing, you can also send a message to: [EMAIL PROTECTED] or for the digest to: [EMAIL PROTECTED] For addition or removal of addresses, I'll send a confirmation message to that address. When you receive it, simply reply to it to complete the transaction. If you need to get in touch with the human owner of this list, please send a message to: [EMAIL PROTECTED] Please include a FORWARDED list message with ALL HEADERS intact to make it easier to help you. --- Enclosed is a copy of the request I received. Return-Path: [EMAIL PROTECTED] Received: (qmail 20718 invoked from network); 28 Sep 2005 15:08:08 - Received: from x1a.develooper.com (HELO x1.develooper.com) (216.52.237.111) by lists.develooper.com with SMTP; 28 Sep 2005 15:08:08 - Received: (qmail 6797 invoked by uid 225); 28 Sep 2005 15:08:08 - Delivered-To: beginners-sc.1127919936.lladepcfpodjjfjadnjp- [EMAIL PROTECTED] Received: (qmail 6792 invoked by alias); 28 Sep 2005 15:08:07 - X-Spam-Status: No, hits=0.6 required=8.0 tests=BAYES_00,DOMAIN_RATIO,NO_REAL_NAME X-Spam-Check-By: la.mx.develooper.com Received-SPF: pass (x1.develooper.com: local policy) Received: from pop05.mail.atl.earthlink.net (HELO pop05.mail.atl.earthlink.net) (207.69.200.58) by la.mx.develooper.com (qpsmtpd/0.28) with ESMTP; Wed, 28 Sep 2005 08:08:04 -0700 Received: from mswamui-bichon.atl.sa.earthlink.net ([209.86.224.26]) by pop05.mail.atl.earthlink.net with esmtp (Exim 3.36 #10) id 1EKdXN-00022E-00 for beginners-sc.1127919936.lladepcfpodjjfjadnjp- [EMAIL PROTECTED]; Wed, 28 Sep 2005 11:08:01 -0400 Message-ID: [EMAIL PROTECTED] bichon.atl.sa.earthlink.net Date: Wed, 28 Sep 2005 11:08:00 -0400 (GMT-04:00) From: [EMAIL PROTECTED] Reply-To: [EMAIL PROTECTED] To: beginners-sc.1127919936.lladepcfpodjjfjadnjp- [EMAIL PROTECTED] Subject: Re: confirm subscribe to beginners@perl.org Mime-Version: 1.0 Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit X-Mailer: Earthlink Zoo Mail 1.0 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail:
Re: eval without warnings
On Sep 27, Bryan R Harris said: 2*(3+2) == 10 2*dog == 2*dog mysquarefunction(2) == 4 3*mysquarefunction(2) == 12 some guy == some guy Here's a solution that works for the cases you've provided: sub try_eval { local $@; my $warning; local $SIG{__WARN__} = sub { $warning = 1 }; my $expr = shift; my $val = eval $expr; $val = $expr if $@ or $warning; return $val; } It catches fatal errors (via $@) and non-fatal warnings (via the __WARN__ handler). If there is an error or warning, the expression itself is returned; otherwise, the returned value from that expression is returned. Excellent! Thank you so much, I tried getting this to work for a long time, but I didn't know about $SIG{__WARN__}. Thanks again. - Bryan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: WELCOME to beginners@perl.org
On 9/28/05, Ryan Frantz [EMAIL PROTECTED] wrote: Just for giggles, I'm gonna assume that you're using PPM (you gave nothing else to go on)... I've only installed this particular module using ActiveState's 'ppm' myself; it seems to have problems with the '::' in module names. If ppm can't find what it believes the module name is, it will default to a search and probably return something like this: ppm search spreadsheet::parseexcel Searching in Active Repositories 1. Spreadsheet-ParseExcel [0.2603] Get information from Excel file 2. Spreadsheet-ParseExcel-Sim~ [1.02] A simple interface to Excel data 3. Spreadsheet-ParseExcel_XLH~ [0.02] Parse Excel Spreadsheets using xlhtml Use the 'install' command and substitute '-' for '::' like so: ppm search Spreadsheet-ParseExcel-Simple You can use the numbers instead of typing all that IIRC, so: ppm install 2 -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: general retry function
Wiggins d'Anconia [Wd], on Tuesday, September 27, 2005 at 17:51 (-0600) contributed this to our collective wisdom: Wd You haven't shown us what you have tried, or where it failed, only Wd suggested something about eval and recursive subs, which neither of Wd which should be pertinent here. Show us your attempts... ok, thanks to both for ideas, I sum all of code shown, so I came with this: sub ftpupload { my $config = shift; # defaults $config-{retries} = $cfg-{ftp_retries} || 5; $config-{file} = $Bin/$config-{template}.html; my $try = 0; while ( $try $config-{retries} ) { $try++; print Uploading ($try/$config-{retries})...\n; sleep 1; eval { my $ftp = Net::FTP-new($config-{ftphost}, Debug = 0) or die Cannot connect to $config-{ftphost}: $@; $ftp-login( $config-{ftpuser}, $config-{ftppass} ) or die Cannot login , $ftp-message; $ftp-cwd( $config-{ftprdir} ) or die Cannot change working directory , $ftp-message; $ftp-put( $config-{file}, index.html ) or die put failed , $ftp-message; $ftp-quit; }; $@ ? print $@ : return 1; } print Failed after $try attempt(s)\n; return 0; } -- ...m8s, cu l8r, Brano. [Recent studies show that recent studies are meaningless.] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Anonymous Reference Question
When I do this: #!/usr/bin/perl -w use strict; use Data::Dumper; my $grades = {tom= 50, sally= 60, harry = 70}; print Dumper($grades) ; And perl gives me this: $VAR1 = { 'harry' = 70, 'sally' = 60, 'tom' = 50 }; QUESTION: Does this mean that $grades is an anonymous reference because perl gives is $VAR1? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
a little help...
Hello all. Please let me know if this is a good place for my questions. I am trying to read a log file and get a list of how many times an IP address get blocked each hour by category PO. An example line in the log with a block is: - [2005-09-28 10:05:03 -7:00] 127.0.0.1 71.32.59.249 216.163.137.3 - http://www.playboy.com/ blocked 0 PO - What I have kinda works but I am not sure if it is the best practice. This is the first time programming in perl and this is what I have so far: sub Scanlog { local($ipb) = @_; open my $slog, -|, tail -n 5 $log or die Unable to open $log:$!\n; open (OUTPUT,/etc/squid/iplist.txt); open (OUTPUT2,/etc/squid/SuspendIpList.txt); while ($slog){ # assigns each line in turn to $_ # use an array slice to select the fields we want @data = (split ,$_)[1,4,10,5,7]; $hr = (split /:/ ,$data[0])[0]; $ip = $data[1]; if($flag eq $data[2]){ if($hr eq $hour){ foreach (/$data[2]/){ $matches += 1 ; } if($matches $blocklimit){ $ip1 = $data[1]/32; print OUTPUT $matches,, $hour, ,$ip1, , @data,\n; print OUTPUT2 $ip1\n; $matched = $matches; $matches = 0; close (OUTPUT); close (OUTPUT2); --- I will take and help I can get. Ryan Lamberton
Re: a little help...
On Sep 28, FamiLink Admin said: I am trying to read a log file and get a list of how many times an IP address get blocked each hour by category PO. An example line in the log with a block is: - [2005-09-28 10:05:03 -7:00] 127.0.0.1 71.32.59.249 216.163.137.3 - http://www.playboy.com/ blocked 0 PO - What I have kinda works but I am not sure if it is the best practice. This is the first time programming in perl and this is what I have so far: Your indentation leaves much to be desired, so I've fixed it. sub Scanlog { local($ipb) = @_; No reason to use 'local'; stick with 'my' here. But... what is $ipb? You don't use it anywhere! open my $slog, -|, tail -n 5 $log or die Unable to open $log:$!\n; open (OUTPUT,/etc/squid/iplist.txt); open (OUTPUT2,/etc/squid/SuspendIpList.txt); You should also die if neither of those could be opened: open(OUTPUT, ...) or die can't create /etc/squid/iplist.txt: $!; while ($slog){ # assigns each line in turn to $_ # use an array slice to select the fields we want @data = (split ,$_)[1,4,10,5,7]; $hr = (split /:/ ,$data[0])[0]; $ip = $data[1]; Those three variables should all be declared with 'my'. Your line assigning to @data has a typo that hasn't effected you, but it might eventually. my @data = (split)[1,4,10,5,7]; # why out of order? my $hr = (split /:/, $data[0])[0]; my $ip = $data[1]; # no need to quote $data[1] here if ($flag eq $data[2]) { Where is $flag coming from? if ($hr eq $hour) { Where is $hour coming from? Those two if statements can be combined into one, since you don't do anything if they aren't both true. if ($flag eq $data[2] and $hr eq $hour) { foreach (/$data[2]/) { $matches += 1 ; } I have a feeling this could lead to false positives. How do you know that 'PO' (or whatever else $data[2] might hold) won't appear in the URL, for instance? Perhaps this should just be $matches++; But where is $matches coming from?! if ($matches $blocklimit) { Where does $blocklimit come from?! $ip1 = $data[1]/32; Declare that with 'my'. print OUTPUT $matches,, $hour, ,$ip1, , @data,\n; You could just write that as print OUTPUT $matches, $hour, $data[1]/32 @data\n; print OUTPUT2 $ip1\n; $matched = $matches; $matches = 0; Where did $matched come from? } } } } close (OUTPUT); close (OUTPUT2); } You should not use any variables in a function that you did not pass to it or create IN it. -- Jeff japhy Pinyan% How can we ever be the sold short or RPI Acacia Brother #734% the cheated, we who for every service http://www.perlmonks.org/ % have long ago been overpaid? http://princeton.pm.org/ %-- Meister Eckhart -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
question about # of files in a directory
Hi, first, I want to thank all who viewed my first question days before, especially to those who took time to answer it. It was trenmendous encouragement for a beginner perlee like me. Thanks again. My question: Is there a way to call or maybe get the # of files in a directory? I am trying to build a storage directory for files which automatically empits itself when the # files reaches 50. If there is some commands which do that(like whatever COMMAND(directory) ), the problem would be solved, I then will be OK to program the rest of perl. To be more specific, I have a CGI online page which takes uploaded files(press 'upload' on my website, then upload whatever text files, uaually DNA sequence files) then I need to modify the files a bit then save the files to the STORAGE directory, the directory can't be infinitely increasing, so I need to empty it when the time comes. And I need to keep track of the # of files in that 'damn' directory. Maybe you got better idea about how to store the files to the directory? Let me know then. Thank you all for reading my 'junk'. best, Bing -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: question about # of files in a directory
ZHAO, BING wrote: Hi, first, I want to thank all who viewed my first question days before, especially to those who took time to answer it. It was trenmendous encouragement for a beginner perlee like me. Thanks again. My question: Is there a way to call or maybe get the # of files in a directory? I am trying to build a storage directory for files which automatically empits itself when the # files reaches 50. If there is some commands which do that(like whatever COMMAND(directory) ), the problem would be solved, I then will be OK to program the rest of perl. To be more specific, I have a CGI online page which takes uploaded files(press 'upload' on my website, then upload whatever text files, uaually DNA sequence files) then I need to modify the files a bit then save the files to the STORAGE directory, the directory can't be infinitely increasing, so I need to empty it when the time comes. And I need to keep track of the # of files in that 'damn' directory. Maybe you got better idea about how to store the files to the directory? Let me know then. perldoc -f opendir perldoc -f readdir perldoc -f closedir perldoc -f unlink This should get you started. You can also look into file globs though I have never preferred them, for whatever reason. perldoc -f stat perldoc -f sort Might also come in handy as presumably you want to remove the oldest, highest number, etc. -- UNTESTED -- opendir my $DIRHANDLE, '/path/to/dir' or die Can't get directory handle: $!; my @filelist = grep { $_ ne '.' and $_ ne '..' } readdir $DIRHANDLE; closedir $DIRHANDLE; if (@filelist 50) { for my $index (50 .. @filelist) { unlink $filelist[$index] or die Can't remove file $filelist[$index]: $!; } } Thank you all for reading my 'junk'. HTH, http://danconia.org best, Bing -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: a little help...
Jeff , Thanks for all your help! This is what I have now (below and this time the whole thing): I think I have included all that you talked about plus others: The sub scanlog does write the information to the files but it does not return anything back to the main program and I also get the error: Use of uninitialized value in split at ./test.pl line 9. Also, is there a better way of counting the number of times each IP address gets blocked with category PO? Each time I get to the blocklimit it writes to the file but I really just want the max number of blocks over the limit. It will write the same IP each time it gets over the blocklimit though. -- #!/usr/bin/perl -w require Mail::Send; $|=1; # no buffering use constant IP_LIST_FILE = /etc/squid/iplist.txt; use constant SUSPEND_FILE = /etc/squid/SuspendIpList.txt; use constant LOG_FILE = /opt/n2h2/logs/filter_log; my $sysop = [EMAIL PROTECTED]; my $flag = PO; my $hour = (split, localtime)[2]; my $blocklimit = 5; my $matches = 0; my $matched = 0; { ($matched,$ip,$hour,$time,$category,$url) = Scanlog($flag,$hour,$blocklimit,$matches,); if($matched $blocklimit){ $msg = new Mail::Send Subject='SuspendIpList', To=$sysop; $fh = $msg-open; print $fh Someone has tried to access $matches banned sites today\n; print $fh Their IP address ($ip) has been added to /etc/squid/SuspendIpList.txt\n; print $fh To unblock them, remove their entry from the file and run squid -k reconfigure\n; print $fh $matches, $ip, $hour, $time, $category, $url\n; $fh-close; # complete the message and send it $matched = 0; } else{ open my $output2, , SUSPEND_FILE or die Can't write @{[SUSPEND_FILE]}: $!; print $output2 10.0.0.252/32\n; close $output2; } } sub Scanlog { my ($flag,$hour,$blocklimit,$matches,)[EMAIL PROTECTED]; open my $slog, -|, tail -n 25000 @{[LOG_FILE]} or die Unable to open $log:$!\n; open my $output, , IP_LIST_FILE or die Can't write @{[IP_LIST_FILE]}: $!; open my $output2, , SUSPEND_FILE or die Can't write @{[SUSPEND_FILE]}: $!; while (my $line = $slog){ # assigns each line in turn to $line #use an array slice to select the fields we want my ($time, $ip, $url, $category) = (split , $line)[1,4,7,10]; my ($hr) = split /:/, $time; if($flag eq $category and $hr eq $hour){ $matches += 1 ; } if($matches $blocklimit){ print $output $matches, $ip, $hour, $time, $category, $url\n; print $output2 $ip/32\n; $matched = $matches; $matches = 0; } } close $output; close $output2; return($matched,$ip,$hour,$time,$category,$url); } -- Ryan Lamberton - Original Message - From: Jeff 'japhy' Pinyan [EMAIL PROTECTED] To: FamiLink Admin [EMAIL PROTECTED] Cc: beginners@perl.org Sent: Wednesday, September 28, 2005 12:24 PM Subject: Re: a little help... On Sep 28, FamiLink Admin said: I am trying to read a log file and get a list of how many times an IP address get blocked each hour by category PO. An example line in the log with a block is: - [2005-09-28 10:05:03 -7:00] 127.0.0.1 71.32.59.249 216.163.137.3 - http://www.playboy.com/ blocked 0 PO - What I have kinda works but I am not sure if it is the best practice. This is the first time programming in perl and this is what I have so far: Your indentation leaves much to be desired, so I've fixed it. sub Scanlog { local($ipb) = @_; No reason to use 'local'; stick with 'my' here. But... what is $ipb? You don't use it anywhere! open my $slog, -|, tail -n 5 $log or die Unable to open $log:$!\n; open (OUTPUT,/etc/squid/iplist.txt); open (OUTPUT2,/etc/squid/SuspendIpList.txt); You should also die if neither of those could be opened: open(OUTPUT, ...) or die can't create /etc/squid/iplist.txt: $!; while ($slog){ # assigns each line in turn to $_ # use an array slice to select the fields we want @data = (split ,$_)[1,4,10,5,7]; $hr = (split /:/ ,$data[0])[0]; $ip = $data[1]; Those three variables should all be declared with 'my'. Your line assigning to @data has a typo that hasn't effected you, but it might eventually. my @data = (split)[1,4,10,5,7]; # why out of order? my $hr = (split /:/, $data[0])[0]; my $ip = $data[1]; # no need to quote $data[1] here if ($flag eq $data[2]) { Where is $flag coming from? if ($hr eq $hour) { Where is $hour coming from? Those two if statements can be combined into one, since you don't do anything if they aren't both true. if
RE: a little help...
FamiLink Admin wrote: Jeff , Thanks for all your help! This is what I have now (below and this time the whole thing): I think I have included all that you talked about plus others: The sub scanlog does write the information to the files but it does not return anything back to the main program and I also get the error: Use of uninitialized value in split at ./test.pl line 9. Also, is there a better way of counting the number of times each IP address gets blocked with category PO? Each time I get to the blocklimit it writes to the file but I really just want the max number of blocks over the limit. It will write the same IP each time it gets over the blocklimit though. If you are only concerned about $ip and if they went over that limit and not desiring the detail of said offense, then you could use the $ip as a key into a hash. Then you could count all the occurances. At the conclusion of that processing then you could loop through the hash and any count greater than your max, then you could write to the suspend file. For email, then could again use the hash to put together a list of $ip's that are over your limit. I have not followed the topic, but unless you do something with the $ip, I would assume that the log is just that a log. You would have interspersed $ip and so I am unsure how you would be able to say $ip is at fault. I see nothing in your code which isolates to the $ip. Again, are these static ip addr or when someone logs out, they are ready for use by someone else. If it is released then you have to figure out when this occurs to get an accurate rcd. If static, then not a problem. Wags ;) -- #!/usr/bin/perl -w require Mail::Send; $|=1; # no buffering use constant IP_LIST_FILE = /etc/squid/iplist.txt; use constant SUSPEND_FILE = /etc/squid/SuspendIpList.txt; use constant LOG_FILE = /opt/n2h2/logs/filter_log; my $sysop = [EMAIL PROTECTED]; my $flag = PO; my $hour = (split, localtime)[2]; my $blocklimit = 5; my $matches = 0; my $matched = 0; { ($matched,$ip,$hour,$time,$category,$url) = Scanlog($flag,$hour,$blocklimit,$matches,); if($matched $blocklimit){ $msg = new Mail::Send Subject='SuspendIpList', To=$sysop; $fh = $msg-open; print $fh Someone has tried to access $matches banned sites today\n; print $fh Their IP address ($ip) has been added to /etc/squid/SuspendIpList.txt\n; print $fh To unblock them, remove their entry from the file and run squid -k reconfigure\n; print $fh $matches, $ip, $hour, $time, $category, $url\n; $fh-close; # complete the message and send it $matched = 0; } else{ open my $output2, , SUSPEND_FILE or die Can't write @{[SUSPEND_FILE]}: $!; print $output2 10.0.0.252/32\n; close $output2; } } sub Scanlog { my ($flag,$hour,$blocklimit,$matches,)[EMAIL PROTECTED]; open my $slog, -|, tail -n 25000 @{[LOG_FILE]} or die Unable to open $log:$!\n; open my $output, , IP_LIST_FILE or die Can't write @{[IP_LIST_FILE]}: $!; open my $output2, , SUSPEND_FILE or die Can't write @{[SUSPEND_FILE]}: $!; while (my $line = $slog){ # assigns each line in turn to $line #use an array slice to select the fields we want my ($time, $ip, $url, $category) = (split , $line)[1,4,7,10]; my ($hr) = split /:/, $time; if($flag eq $category and $hr eq $hour){ $matches += 1 ; } if($matches $blocklimit){ print $output $matches, $ip, $hour, $time, $category, $url\n; print $output2 $ip/32\n; $matched = $matches; $matches = 0; } } close $output; close $output2; return($matched,$ip,$hour,$time,$category,$url); } -- Ryan Lamberton - Original Message - From: Jeff 'japhy' Pinyan [EMAIL PROTECTED] To: FamiLink Admin [EMAIL PROTECTED] Cc: beginners@perl.org Sent: Wednesday, September 28, 2005 12:24 PM Subject: Re: a little help... On Sep 28, FamiLink Admin said: I am trying to read a log file and get a list of how many times an IP address get blocked each hour by category PO. An example line in the log with a block is: - [2005-09-28 10:05:03 -7:00] 127.0.0.1 71.32.59.249 216.163.137.3 - http://www.playboy.com/ blocked 0 PO - What I have kinda works but I am not sure if it is the best practice. This is the first time programming in perl and this is what I have so far: Your indentation leaves much to be desired, so I've fixed it. sub Scanlog { local($ipb) = @_; No reason to use
Re: a little help...
I am only concerned about the IP. The rest is just to verify the data for now. What code would I use to key the $IP in to hash for counting?. Most of the IP's are not static but are from broadband and don't change too often. An example log is: - [2005-09-28 10:05:03 -7:00] 127.0.0.1 71.32.59.249 216.163.137.3 - http://www.playboy.com/ blocked 0 PO - the IP I want to count is 71.32.59.249 (for this log) and the category is PO Ryan Lamberton - Original Message - From: Wagner, David --- Senior Programmer Analyst --- WGO [EMAIL PROTECTED] To: FamiLink Admin [EMAIL PROTECTED] Cc: beginners@perl.org Sent: Wednesday, September 28, 2005 5:18 PM Subject: RE: a little help... FamiLink Admin wrote: Jeff , Thanks for all your help! This is what I have now (below and this time the whole thing): I think I have included all that you talked about plus others: The sub scanlog does write the information to the files but it does not return anything back to the main program and I also get the error: Use of uninitialized value in split at ./test.pl line 9. Also, is there a better way of counting the number of times each IP address gets blocked with category PO? Each time I get to the blocklimit it writes to the file but I really just want the max number of blocks over the limit. It will write the same IP each time it gets over the blocklimit though. If you are only concerned about $ip and if they went over that limit and not desiring the detail of said offense, then you could use the $ip as a key into a hash. Then you could count all the occurances. At the conclusion of that processing then you could loop through the hash and any count greater than your max, then you could write to the suspend file. For email, then could again use the hash to put together a list of $ip's that are over your limit. I have not followed the topic, but unless you do something with the $ip, I would assume that the log is just that a log. You would have interspersed $ip and so I am unsure how you would be able to say $ip is at fault. I see nothing in your code which isolates to the $ip. Again, are these static ip addr or when someone logs out, they are ready for use by someone else. If it is released then you have to figure out when this occurs to get an accurate rcd. If static, then not a problem. Wags ;) -- #!/usr/bin/perl -w require Mail::Send; $|=1; # no buffering use constant IP_LIST_FILE = /etc/squid/iplist.txt; use constant SUSPEND_FILE = /etc/squid/SuspendIpList.txt; use constant LOG_FILE = /opt/n2h2/logs/filter_log; my $sysop = [EMAIL PROTECTED]; my $flag = PO; my $hour = (split, localtime)[2]; my $blocklimit = 5; my $matches = 0; my $matched = 0; { ($matched,$ip,$hour,$time,$category,$url) = Scanlog($flag,$hour,$blocklimit,$matches,); if($matched $blocklimit){ $msg = new Mail::Send Subject='SuspendIpList', To=$sysop; $fh = $msg-open; print $fh Someone has tried to access $matches banned sites today\n; print $fh Their IP address ($ip) has been added to /etc/squid/SuspendIpList.txt\n; print $fh To unblock them, remove their entry from the file and run squid -k reconfigure\n; print $fh $matches, $ip, $hour, $time, $category, $url\n; $fh-close; # complete the message and send it $matched = 0; } else{ open my $output2, , SUSPEND_FILE or die Can't write @{[SUSPEND_FILE]}: $!; print $output2 10.0.0.252/32\n; close $output2; } } sub Scanlog { my ($flag,$hour,$blocklimit,$matches,)[EMAIL PROTECTED]; open my $slog, -|, tail -n 25000 @{[LOG_FILE]} or die Unable to open $log:$!\n; open my $output, , IP_LIST_FILE or die Can't write @{[IP_LIST_FILE]}: $!; open my $output2, , SUSPEND_FILE or die Can't write @{[SUSPEND_FILE]}: $!; while (my $line = $slog){ # assigns each line in turn to $line #use an array slice to select the fields we want my ($time, $ip, $url, $category) = (split , $line)[1,4,7,10]; my ($hr) = split /:/, $time; if($flag eq $category and $hr eq $hour){ $matches += 1 ; } if($matches $blocklimit){ print $output $matches, $ip, $hour, $time, $category, $url\n; print $output2 $ip/32\n; $matched = $matches; $matches = 0; } } close $output; close $output2; return($matched,$ip,$hour,$time,$category,$url); } -- Ryan Lamberton - Original Message - From: Jeff 'japhy' Pinyan [EMAIL PROTECTED] To: FamiLink Admin [EMAIL PROTECTED] Cc: beginners@perl.org Sent: Wednesday, September 28, 2005 12:24 PM Subject: Re: a
RE: a little help...
FamiLink Admin wrote: I am only concerned about the IP. The rest is just to verify the data for now. What code would I use to key the $IP in to hash for counting?. Most of the IP's are not static but are from broadband and don't change too often. An example log is: - [2005-09-28 10:05:03 -7:00] 127.0.0.1 71.32.59.249 216.163.137.3 - http://www.playboy.com/ blocked 0 PO - the IP I want to count is 71.32.59.249 (for this log) and the category is PO I would do something like: my %MIAI = (); my $MyIpAddrInfo = \%MIAI; Now as you go through the scan loop, you would take the if which is doing the check on the $flag and the do something like $MyIpAddInfo-{$ip}++; Now you complete your scan and then run throuh your loop like: foreach $MyIpAddr (sort keys %{MyIpAddrInfo}) { next if ( $MyIpAddrInfo-{MyIpAddr} = $blocklimit ); # if less than or equal get next key # write your suspend and you could put together your email at the same time } A start. Wags ;) Ryan Lamberton - Original Message - From: Wagner, David --- Senior Programmer Analyst --- WGO [EMAIL PROTECTED] To: FamiLink Admin [EMAIL PROTECTED] Cc: beginners@perl.org Sent: Wednesday, September 28, 2005 5:18 PM Subject: RE: a little help... FamiLink Admin wrote: Jeff , Thanks for all your help! This is what I have now (below and this time the whole thing): I think I have included all that you talked about plus others: The sub scanlog does write the information to the files but it does not return anything back to the main program and I also get the error: Use of uninitialized value in split at ./test.pl line 9. Also, is there a better way of counting the number of times each IP address gets blocked with category PO? Each time I get to the blocklimit it writes to the file but I really just want the max number of blocks over the limit. It will write the same IP each time it gets over the blocklimit though. If you are only concerned about $ip and if they went over that limit and not desiring the detail of said offense, then you could use the $ip as a key into a hash. Then you could count all the occurances. At the conclusion of that processing then you could loop through the hash and any count greater than your max, then you could write to the suspend file. For email, then could again use the hash to put together a list of $ip's that are over your limit. I have not followed the topic, but unless you do something with the $ip, I would assume that the log is just that a log. You would have interspersed $ip and so I am unsure how you would be able to say $ip is at fault. I see nothing in your code which isolates to the $ip. Again, are these static ip addr or when someone logs out, they are ready for use by someone else. If it is released then you have to figure out when this occurs to get an accurate rcd. If static, then not a problem. Wags ;) -- #!/usr/bin/perl -w require Mail::Send; $|=1; # no buffering use constant IP_LIST_FILE = /etc/squid/iplist.txt; use constant SUSPEND_FILE = /etc/squid/SuspendIpList.txt; use constant LOG_FILE = /opt/n2h2/logs/filter_log; my $sysop = [EMAIL PROTECTED]; my $flag = PO; my $hour = (split, localtime)[2]; my $blocklimit = 5; my $matches = 0; my $matched = 0; { ($matched,$ip,$hour,$time,$category,$url) = Scanlog($flag,$hour,$blocklimit,$matches,); if($matched $blocklimit){ $msg = new Mail::Send Subject='SuspendIpList', To=$sysop; $fh = $msg-open; print $fh Someone has tried to access $matches banned sites today\n; print $fh Their IP address ($ip) has been added to /etc/squid/SuspendIpList.txt\n; print $fh To unblock them, remove their entry from the file and run squid -k reconfigure\n; print $fh $matches, $ip, $hour, $time, $category, $url\n; $fh-close; # complete the message and send it $matched = 0; } else{ open my $output2, , SUSPEND_FILE or die Can't write @{[SUSPEND_FILE]}: $!; print $output2 10.0.0.252/32\n; close $output2; } } sub Scanlog { my ($flag,$hour,$blocklimit,$matches,)[EMAIL PROTECTED]; open my $slog, -|, tail -n 25000 @{[LOG_FILE]} or die Unable to open $log:$!\n; open my $output, , IP_LIST_FILE or die Can't write @{[IP_LIST_FILE]}: $!; open my $output2, , SUSPEND_FILE or die Can't write @{[SUSPEND_FILE]}: $!; while (my $line = $slog){ # assigns each line in turn to $line #use an array slice to select the fields we want my ($time, $ip, $url, $category) = (split , $line)[1,4,7,10]; my ($hr) = split /:/, $time; if($flag