perl.beginners http://www.nntp.perl.org/group/perl.beginners/ ... Copyright 1998-2013 perl.org Wed, 19 Jun 2013 09:37:24 +0000 ask@perl.org Re: Tk.pm by Rein Owen &lt;rcook &lt;at&gt; pcug.org.au&gt; writes:<br/><br/><br/>&gt; Does not Ubuntu, Synaptic, show a perl-tk tk8.4 and tk8.4-dev<br/>&gt; <br/>&gt; Just install those might be easier. Synaptic will find any other<br/>&gt; dependencies.<br/>&gt; <br/>I found perl-tk, installed it and could subsequently install 2 other modules<br/>that were required. The software is now running fine. Thanks for your help.<br/><br/><br/><br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123044.html Wed, 19 Jun 2013 07:25:18 +0000 Re: Tk.pm by Rein Owen &lt;rcook &lt;at&gt; pcug.org.au&gt; writes:<br/><br/><br/>&gt; Does not Ubuntu, Synaptic, show a perl-tk tk8.4 and tk8.4-dev<br/>&gt; <br/>&gt; Just install those might be easier. Synaptic will find any other<br/>&gt; dependencies.<br/><br/>The software is written in perl and requires 2 modules: Tk.pm and<br/>DevicesSerial.pm<br/><br/>Tk4.8 is already installed on the system. :) <br/><br/><br/><br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123043.html Wed, 19 Jun 2013 07:03:52 +0000 Re: Tk.pm by Owen <br/>&gt; I have a program that needs Tk.pm.<br/>&gt; I run perl under Ubuntu 12.04<br/>&gt; After configuring the Makefile &#39;make&#39; gives the following error:<br/><br/><br/><br/>Does not Ubuntu, Synaptic, show a perl-tk tk8.4 and tk8.4-dev<br/><br/>Just install those might be easier. Synaptic will find any other<br/>dependencies.<br/><br/><br/><br/>-- <br/>Owen<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123042.html Wed, 19 Jun 2013 06:22:42 +0000 Tk.pm by Rein I have a program that needs Tk.pm.<br/>I run perl under Ubuntu 12.04<br/>After configuring the Makefile &#39;make&#39; gives the following error:<br/><br/>root@reica3:/home/rein/Downloads/Tk-804.027# make<br/>cd pTk &amp;&amp; make DEFINE=&quot;&quot;<br/>make[1]: Entering directory `/home/rein/Downloads/Tk-804.027/pTk&#39;<br/>cc -c -I.. -I. -Ibitmaps -D_REENTRANT -D_GNU_SOURCE -DDEBIAN<br/>-fno-strict-aliasing -pipe -fstack-protector -I/usr/local/include<br/>-D_LARGEFILE_SOURCE -D_FILE_OFFSET_BITS=64 -O2 -g -DVERSION=\&quot;804.027\&quot;<br/>-DXS_VERSION=\&quot;804.027\&quot; -fPIC &quot;-I/usr/lib/perl/5.14/CORE&quot; -Wall<br/>-Wno-implicit-int -Wno-comment -Wno-unused -D__USE_FIXED_PROTOTYPES__ Xlib_f.c<br/>In file included from Xlib_f.c:8:0:<br/>Xlib.h:22:15: error: conflicting types for &lsquo;XKeycodeToKeysym&rsquo;<br/>/usr/include/X11/Xlib.h:1695:15: note: previous declaration of<br/>&lsquo;XKeycodeToKeysym&rsquo; was here<br/>make[1]: *** [Xlib_f.o] Error 1<br/>make[1]: Leaving directory `/home/rein/Downloads/Tk-804.027/pTk&#39;<br/>make: *** [pTk/libpTk.a] Error 2<br/><br/>Since I am a total beginner as far as perl is concerned I am at a loss how<br/>to solve this problem.<br/>Can anyone help me out? <br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123041.html Wed, 19 Jun 2013 06:05:18 +0000 Fwd: Re: Perl Cookbook by David Christensen Someone wrote:<br/> &gt; I have the 2nd edition.<br/><br/>Make sure you &quot;reply to list&quot; so that everyone can help and/or benefit.<br/><br/>Okay. What about the other two suggestions?<br/><br/>2. Have you checked the errata on the publisher&#39;s web site? Here are <br/>the errata for the current (2nd) edition:<br/><br/> <br/>http://oreilly.com/catalog/**errata.csp?isbn=9780596003135&lt;http://oreilly.com/catalog/errata.csp?isbn=9780596003135&gt;<br/><br/>3. Please post the mistakes on the list and perhaps someone can help you.<br/><br/><br/>David<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123040.html Tue, 18 Jun 2013 03:43:44 +0000 Richard Fernandez by Richard Fernandez wer <br/>http://estetikburuncerrahisi.com/mr/lybibl/cddhmsewykrj/ubpejcbsumykym.php Richard Fernandez<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123039.html Mon, 17 Jun 2013 15:39:44 +0000 Re: last by malisetti rammurthy Bill,<br/><br/>Below statement wont be helpful for lee, as he has requirement to loop at<br/>least one time.<br/>############<br/> last if ($counter &gt; 2);<br/><br/>where as below one is working:<br/><br/>############<br/>if ( $counter &gt; 2) {<br/> print &#39;if : &#39; . $counter . &quot;\n&quot;; #could do print &quot;if : $counter\n&quot; as<br/>well<br/> last;<br/>}<br/><br/><br/><br/>On Mon, Jun 17, 2013 at 6:43 PM, bill pemberton &lt;wapembe@gmail.com&gt; wrote:<br/><br/>&gt; I think the rest after the &#39;if&#39; for the last is wrong. either do this:<br/>&gt; last if ($counter &gt; 2);<br/>&gt;<br/>&gt; or<br/>&gt;<br/>&gt; if ( $counter &gt; 2) {<br/>&gt; print &#39;if : &#39; . $counter . &quot;\n&quot;; #could do print &quot;if : $counter\n&quot;<br/>&gt; as well<br/>&gt; last;<br/>&gt; }<br/>&gt;<br/>&gt;<br/>&gt; On Mon, Jun 17, 2013 at 8:56 AM, lee &lt;lee@yun.yagibdah.de&gt; wrote:<br/>&gt;<br/>&gt;&gt; Hi,<br/>&gt;&gt;<br/>&gt;&gt; trying to figure out what `last&#39; actually does, I wrote this test<br/>&gt;&gt; script:<br/>&gt;&gt;<br/>&gt;&gt;<br/>&gt;&gt; use strict;<br/>&gt;&gt; use warnings;<br/>&gt;&gt; use autodie;<br/>&gt;&gt;<br/>&gt;&gt; my $counter = 0;<br/>&gt;&gt;<br/>&gt;&gt; while($counter &lt; 8) {<br/>&gt;&gt; last if($counter &gt; 2) {<br/>&gt;&gt; print &quot;if: &quot; . $counter . &quot;\n&quot;;<br/>&gt;&gt; }<br/>&gt;&gt; else {<br/>&gt;&gt; print &quot;else: &quot; . $counter . &quot;\n&quot;;<br/>&gt;&gt; }<br/>&gt;&gt; $counter++;<br/>&gt;&gt; }<br/>&gt;&gt;<br/>&gt;&gt;<br/>&gt;&gt; Unfortunately, that gives syntax errors. I would expect the following<br/>&gt;&gt; output:<br/>&gt;&gt;<br/>&gt;&gt;<br/>&gt;&gt; else: 0<br/>&gt;&gt; else: 1<br/>&gt;&gt; else: 2<br/>&gt;&gt; if: 3<br/>&gt;&gt;<br/>&gt;&gt;<br/>&gt;&gt; What&#39;s wrong with that (letting aside that it is terribly ambiguous)?<br/>&gt;&gt;<br/>&gt;&gt;<br/>&gt;&gt; --<br/>&gt;&gt; &quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>&gt;&gt; you should be allowed to do anything with functions.&quot;<br/>&gt;&gt; http://www.joelonsoftware.com/items/2006/08/01.html<br/>&gt;&gt;<br/>&gt;&gt; --<br/>&gt;&gt; To unsubscribe, e-mail: beginners-unsubscribe@perl.org<br/>&gt;&gt; For additional commands, e-mail: beginners-help@perl.org<br/>&gt;&gt; http://learn.perl.org/<br/>&gt;&gt;<br/>&gt;&gt;<br/>&gt;&gt;<br/>&gt;<br/>&gt;<br/>&gt; --<br/>&gt; wapembe<br/>&gt; seller of truths, half and whole<br/>&gt; inquire about quantity discounts!<br/>&gt;<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123038.html Mon, 17 Jun 2013 13:20:15 +0000 Re: last by bill pemberton I think the rest after the &#39;if&#39; for the last is wrong. either do this:<br/> last if ($counter &gt; 2);<br/><br/>or<br/><br/>if ( $counter &gt; 2) {<br/> print &#39;if : &#39; . $counter . &quot;\n&quot;; #could do print &quot;if : $counter\n&quot; as<br/>well<br/> last;<br/>}<br/><br/><br/>On Mon, Jun 17, 2013 at 8:56 AM, lee &lt;lee@yun.yagibdah.de&gt; wrote:<br/><br/>&gt; Hi,<br/>&gt;<br/>&gt; trying to figure out what `last&#39; actually does, I wrote this test<br/>&gt; script:<br/>&gt;<br/>&gt;<br/>&gt; use strict;<br/>&gt; use warnings;<br/>&gt; use autodie;<br/>&gt;<br/>&gt; my $counter = 0;<br/>&gt;<br/>&gt; while($counter &lt; 8) {<br/>&gt; last if($counter &gt; 2) {<br/>&gt; print &quot;if: &quot; . $counter . &quot;\n&quot;;<br/>&gt; }<br/>&gt; else {<br/>&gt; print &quot;else: &quot; . $counter . &quot;\n&quot;;<br/>&gt; }<br/>&gt; $counter++;<br/>&gt; }<br/>&gt;<br/>&gt;<br/>&gt; Unfortunately, that gives syntax errors. I would expect the following<br/>&gt; output:<br/>&gt;<br/>&gt;<br/>&gt; else: 0<br/>&gt; else: 1<br/>&gt; else: 2<br/>&gt; if: 3<br/>&gt;<br/>&gt;<br/>&gt; What&#39;s wrong with that (letting aside that it is terribly ambiguous)?<br/>&gt;<br/>&gt;<br/>&gt; --<br/>&gt; &quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>&gt; you should be allowed to do anything with functions.&quot;<br/>&gt; http://www.joelonsoftware.com/items/2006/08/01.html<br/>&gt;<br/>&gt; --<br/>&gt; To unsubscribe, e-mail: beginners-unsubscribe@perl.org<br/>&gt; For additional commands, e-mail: beginners-help@perl.org<br/>&gt; http://learn.perl.org/<br/>&gt;<br/>&gt;<br/>&gt;<br/><br/><br/>-- <br/>wapembe<br/>seller of truths, half and whole<br/>inquire about quantity discounts!<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123037.html Mon, 17 Jun 2013 13:13:50 +0000 Re: last by James Alton lee,<br/><br/>You have a post statement if and then a code block. You can only use one of<br/>two forms:<br/><br/>print &quot;test&quot; if $color eq &quot;blue&quot;; #no parenthesis required<br/>if($color eq &quot;blue&quot;){print &quot;test&quot;;}<br/><br/>As far as last, http://perldoc.perl.org/functions/last.html<br/>The example given:<br/><br/> LINE: while (&lt;STDIN&gt;) {<br/> last LINE if /^$/; # exit when done with header<br/> #...<br/> }<br/><br/>What they&#39;re saying here is that it breaks out of a loop. Its not like a<br/>return where it breaks out of a subroutine, it just continues beyond the<br/>loop.<br/>I think this fixes your program:<br/><br/><br/>use strict;<br/>use warnings;<br/>use autodie;<br/><br/>my $counter = 0;<br/><br/>while($counter &lt; 8) {<br/> if($counter &gt; 2) {<br/> print &quot;if: &quot; . $counter . &quot;\n&quot;;<br/> last;<br/> }<br/> else {<br/> print &quot;else: &quot; . $counter . &quot;\n&quot;;<br/> }<br/> $counter++;<br/>}<br/><br/>Cheers,<br/>James<br/><br/><br/><br/>On Mon, Jun 17, 2013 at 6:56 AM, lee &lt;lee@yun.yagibdah.de&gt; wrote:<br/>&gt;<br/>&gt; Hi,<br/>&gt;<br/>&gt; trying to figure out what `last&#39; actually does, I wrote this test<br/>&gt; script:<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123036.html Mon, 17 Jun 2013 13:10:27 +0000 last by lee Hi,<br/><br/>trying to figure out what `last&#39; actually does, I wrote this test<br/>script:<br/><br/><br/>use strict;<br/>use warnings;<br/>use autodie;<br/><br/>my $counter = 0;<br/><br/>while($counter &lt; 8) {<br/> last if($counter &gt; 2) {<br/> print &quot;if: &quot; . $counter . &quot;\n&quot;;<br/> }<br/> else {<br/> print &quot;else: &quot; . $counter . &quot;\n&quot;;<br/> }<br/> $counter++;<br/>}<br/><br/><br/>Unfortunately, that gives syntax errors. I would expect the following<br/>output:<br/><br/><br/>else: 0<br/>else: 1<br/>else: 2<br/>if: 3<br/><br/><br/>What&#39;s wrong with that (letting aside that it is terribly ambiguous)?<br/><br/><br/>-- <br/>&quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>you should be allowed to do anything with functions.&quot;<br/>http://www.joelonsoftware.com/items/2006/08/01.html<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123035.html Mon, 17 Jun 2013 12:57:04 +0000 Fwd: TEsco::Kesho and malware by David Christensen Someone wrote:<br/> &gt; my TEsco::Kesho, doesn&#39;t run because of chinese invaded my<br/> &gt; laptop.<br/><br/>Please fix your laptop, or get it fixed. (Please understand that the <br/>beginners@perl.org is not the best resource for fixing your laptop. I&#39;d <br/>suggest finding a mailing list or forum specific to your laptop&#39;s <br/>operating system.)<br/><br/>I am unable to locate TEsco::Kesho on CPAN or Google. Is it a Perl module?<br/><br/>David<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123034.html Mon, 17 Jun 2013 03:52:36 +0000 Fwd: Perl Cookbook by David Christensen Someone wrote:<br/> &gt; I bought a book Perlcookbook, and seams to be that the book has some<br/> &gt; mistakes,<br/><br/>1. Which edition do you have -- 1st or 2nd?<br/><br/>2. Have you checked the errata on the publisher&#39;s web site? Here are <br/>the errata for the current (2nd) edition:<br/><br/> http://oreilly.com/catalog/errata.csp?isbn=9780596003135<br/><br/>3. Please post the mistakes on the list and perhaps someone can help you.<br/><br/><br/>David<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123033.html Mon, 17 Jun 2013 03:46:02 +0000 Fwd: Re: OT pedagogy and the beginners@perl.org mailing list by David Christensen I wrote:<br/>&gt; Rheotorical question -- which program would you rather write?<br/>&gt; 1. 1,000 lines of assembly.<br/>&gt; 2. 100 lines of C.<br/>&gt; 3. 10 lines of Java.<br/>&gt; 4. 1 line of Perl.<br/><br/>Someone wrote:<br/> &gt; If this is your best point...<br/><br/>I believe this is one of Perl&#39;s best points. :-)<br/><br/><br/>As I understand it, Perl is not a rigid, formal, highly regulated <br/>language for cargo cult programmers. Perl was designed by a linguist to <br/>approach the flexibility of human languages, with all their quirks, <br/>lumps, and irregularities. This is why there is no EBNF specification <br/>for Perl; it just can&#39;t be done. This is also why it is possible to get <br/>so much done in so little Perl.<br/><br/><br/>I guess the fundamental point is tolerance, which is (used to be?) the <br/>foundation of the FOSS movement. Combine that with Perl&#39;s motto -- <br/>&quot;There is more than one way to do it&quot; (TIMTOWTD) -- Perl programmers <br/>should expect that different Perl programmers can, will, and are <br/>entitled to, write their Perl programs differently; and be tolerant of <br/>those differences.<br/><br/><br/>David<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123032.html Sun, 16 Jun 2013 17:19:23 +0000 Re: perl hash loop: keys() vs values() by richard Thanks. I looked at this site while searching for solutions but I probably<br/>skipped the section on sorting because I wasn&#39;t sorting the hash.<br/><br/>regards, Richard<br/>--<br/>tmqrichard@gmail.com<br/><br/><br/>On Sun, Jun 16, 2013 at 3:17 AM, Peter Gordon &lt;petergo@netspace.net.au&gt;wrote:<br/><br/>&gt; On Sat, 15 Jun 2013 12:12:56 -0400, richard wrote:<br/>&gt; &gt;Hi<br/>&gt; &gt;<br/>&gt; &gt;I&#39;m trying to understand the difference between the keys() and<br/>&gt; &gt;values () operators. What I&#39;m not getting is why a hash must be<br/>&gt; &gt;reversed to get the key corresponding to a value? (as stated in<br/>&gt; &gt;perldoc). Can someone explain this please?<br/>&gt; &gt;<br/>&gt; &gt;<br/>&gt; Read http://www.tizag.com/perlT/perlhashes.php<br/>&gt; particularly the section on sort by keys &amp; by values.<br/>&gt; This should give you a good understanding of the<br/>&gt; how to use them.<br/>&gt;<br/>&gt; --<br/>&gt; Peter Gordon, petergo@netspace.net.au on 06/16/2013<br/>&gt;<br/>&gt;<br/>&gt;<br/>&gt; --<br/>&gt; To unsubscribe, e-mail: beginners-unsubscribe@perl.org<br/>&gt; For additional commands, e-mail: beginners-help@perl.org<br/>&gt; http://learn.perl.org/<br/>&gt;<br/>&gt;<br/>&gt;<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123031.html Sat, 15 Jun 2013 21:54:35 +0000 Re: perl hash loop: keys() vs values() by Peter Gordon On Sat, 15 Jun 2013 12:12:56 -0400, richard wrote:<br/>&gt;Hi<br/>&gt;<br/>&gt;I&#39;m trying to understand the difference between the keys() and<br/>&gt;values () operators. What I&#39;m not getting is why a hash must be<br/>&gt;reversed to get the key corresponding to a value? (as stated in<br/>&gt;perldoc). Can someone explain this please?<br/>&gt;<br/>&gt;<br/>Read http://www.tizag.com/perlT/perlhashes.php<br/>particularly the section on sort by keys &amp; by values.<br/>This should give you a good understanding of the<br/>how to use them.<br/><br/>--<br/>Peter Gordon, petergo@netspace.net.au on 06/16/2013<br/><br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123030.html Sat, 15 Jun 2013 21:18:06 +0000 Re: perl hash loop: keys() vs values() by richard Octavian<br/><br/>Thanks for taking the time to write such a thorough reply. My question is<br/>completely answered by this section:<br/><br/>You can&#39;t get the key for a certain value. Say you have the following hash:<br/><br/>my %hash = (a =&gt; 1, b =&gt; 2, c =&gt; 3, d =&gt; 2, e =&gt; 2);<br/><br/>What&#39;s the key for the value &quot;2&quot;?<br/><br/>I was so preoccupied with values() and keys(), thinking of them as being<br/>mirror-images of one another, that I forgot what a &#39;value&#39; and a &#39;key&#39;<br/>were.<br/><br/>Thanks again<br/><br/><br/>regards, Richard<br/>--<br/>tmqrichard@gmail.com<br/><br/><br/>On Sat, Jun 15, 2013 at 2:19 PM, Octavian Rasnita &lt;orasnita@gmail.com&gt;wrote:<br/><br/>&gt; **<br/>&gt; Perl has 2 types of arrays: common arrays and associative arrays which are<br/>&gt; called hashes.<br/>&gt;<br/>&gt; In order to get the value of an item from an ordinary array, you need to<br/>&gt; specify the index of that array, for example:<br/>&gt;<br/>&gt; my @array = (1, 2, 3);<br/>&gt; print $array[1]; #will print &quot;2&quot; (because the indexes start from 0<br/>&gt;<br/>&gt; A hash associates a key to any value and if you want to get the value, you<br/>&gt; need to specify its key:<br/>&gt; my %hash = (a =&gt; 1, b =&gt; 2, c =&gt; 3);<br/>&gt; print $hash{b}; #will print &quot;2&quot;<br/>&gt;<br/>&gt; You can&#39;t get the key for a certain value. Say you have the following hash:<br/>&gt;<br/>&gt; my %hash = (a =&gt; 1, b =&gt; 2, c =&gt; 3, d =&gt; 2, e =&gt; 2);<br/>&gt;<br/>&gt; What&#39;s the key for the value &quot;2&quot;?<br/>&gt;<br/>&gt; The keys in a hash are always unique but the values can have duplicates.<br/>&gt;<br/>&gt; If you reverse the hash, the keys become values and the values become<br/>&gt; keys, so you will be able to use the value as a key. But if the values<br/>&gt; have duplicates, your reversed hash will contain less elements because that<br/>&gt; hash will contain unique keys.<br/>&gt;<br/>&gt; In the example above, there would be just a single &quot;2&quot; key and not 3.<br/>&gt;<br/>&gt; So, the question is why do you need to access the keys using their values?<br/>&gt;<br/>&gt; Maybe you want to use an array of arrays like:<br/>&gt;<br/>&gt; my @array = (<br/>&gt; [&#39;a&#39;, 1],<br/>&gt; [&#39;b&#39;, 2],<br/>&gt; [&#39;c&#39;, &#39;3],<br/>&gt; [&#39;d&#39;, 2],<br/>&gt; );<br/>&gt;<br/>&gt; To get the &quot;value&quot; for the element with the index 1 you need to do:<br/>&gt;<br/>&gt; my $value = $array[1][1]; #the value &quot;2&quot;<br/>&gt;<br/>&gt; and to get the &quot;key&quot; for the element with the index 1, you can do:<br/>&gt;<br/>&gt; my $key = $array[1][0]; #Will get &quot;b&quot;<br/>&gt;<br/>&gt; I don&#39;t know what you need to do... that&#39;s why I gave the idea of using<br/>&gt; arrays of arrays...<br/>&gt;<br/>&gt; --Octavian<br/>&gt;<br/>&gt; ----- Original Message -----<br/>&gt; *From:* richard &lt;tmqrichard@gmail.com&gt;<br/>&gt; *To:* beginners@perl.org<br/>&gt; *Sent:* Saturday, June 15, 2013 7:12 PM<br/>&gt; *Subject:* perl hash loop: keys() vs values()<br/>&gt;<br/>&gt; Hi<br/>&gt;<br/>&gt; I&#39;m trying to understand the difference between the keys() and values ()<br/>&gt; operators. What I&#39;m not getting is why a hash must be reversed to get the<br/>&gt; key corresponding to a value? (as stated in perldoc). Can someone explain<br/>&gt; this please?<br/>&gt;<br/>&gt; Here is my test script; My perl is freebsd 5.8.9<br/>&gt;<br/>&gt; use warnings;<br/>&gt; use strict;<br/>&gt;<br/>&gt; my %hds = (&#39;1&#39;, &#39;0C8CB35F&#39;, &#39;2&#39;, &#39;0C9CB37D&#39;);<br/>&gt; print &quot;rows: &quot; . scalar keys(%hds) . &quot;\n\n&quot;;<br/>&gt;<br/>&gt; my $value;<br/>&gt; my $key;<br/>&gt;<br/>&gt; foreach $value (values %hds) {<br/>&gt; print &quot;key: $hds{$value} \n&quot;;<br/>&gt; print &quot;value: $value \n&quot;;<br/>&gt; }<br/>&gt;<br/>&gt; print &quot;--------------------------- \n&quot;;<br/>&gt;<br/>&gt; my %r_hds = reverse %hds;<br/>&gt; foreach $value (values %hds) {<br/>&gt; print &quot;key: $r_hds{$value} \n&quot;;<br/>&gt; print &quot;value: $value \n&quot;;<br/>&gt; }<br/>&gt;<br/>&gt; print &quot;--------------------------- \n&quot;;<br/>&gt;<br/>&gt; foreach $key (keys %hds) {<br/>&gt; print &quot;key: $key \n&quot;;<br/>&gt; print &quot;value: $hds{$key} \n&quot;;<br/>&gt; }<br/>&gt;<br/>&gt;<br/>&gt; Here is the output:<br/>&gt;<br/>&gt; Use of uninitialized value in concatenation (.) or string at h line 11.<br/>&gt; key:<br/>&gt; value: 0C8CB35F<br/>&gt; Use of uninitialized value in concatenation (.) or string at h line 11.<br/>&gt; key:<br/>&gt; value: 0C9CB37D<br/>&gt; ---------------------------<br/>&gt; key: 1<br/>&gt; value: 0C8CB35F<br/>&gt; key: 2<br/>&gt; value: 0C9CB37D<br/>&gt; ---------------------------<br/>&gt; key: 1<br/>&gt; value: 0C8CB35F<br/>&gt; key: 2<br/>&gt; value: 0C9CB37D<br/>&gt;<br/>&gt;<br/>&gt; --<br/>&gt; regards, Richard<br/>&gt; --<br/>&gt; tmqrichard@gmail.com<br/>&gt;<br/>&gt;<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123029.html Sat, 15 Jun 2013 19:57:14 +0000 Re: perl hash loop: keys() vs values() by Octavian Rasnita Perl has 2 types of arrays: common arrays and associative arrays which are called hashes.<br/><br/>In order to get the value of an item from an ordinary array, you need to specify the index of that array, for example:<br/><br/>my @array = (1, 2, 3);<br/>print $array[1]; #will print &quot;2&quot; (because the indexes start from 0<br/><br/>A hash associates a key to any value and if you want to get the value, you need to specify its key:<br/>my %hash = (a =&gt; 1, b =&gt; 2, c =&gt; 3);<br/>print $hash{b}; #will print &quot;2&quot;<br/><br/>You can&#39;t get the key for a certain value. Say you have the following hash:<br/><br/>my %hash = (a =&gt; 1, b =&gt; 2, c =&gt; 3, d =&gt; 2, e =&gt; 2);<br/><br/>What&#39;s the key for the value &quot;2&quot;?<br/><br/>The keys in a hash are always unique but the values can have duplicates.<br/><br/>If you reverse the hash, the keys become values and the values become keys, so you will be able to use the value as a key. But if the values have duplicates, your reversed hash will contain less elements because that hash will contain unique keys.<br/><br/>In the example above, there would be just a single &quot;2&quot; key and not 3.<br/><br/>So, the question is why do you need to access the keys using their values?<br/><br/>Maybe you want to use an array of arrays like:<br/><br/>my @array = (<br/> [&#39;a&#39;, 1],<br/> [&#39;b&#39;, 2],<br/> [&#39;c&#39;, &#39;3],<br/> [&#39;d&#39;, 2],<br/>);<br/><br/>To get the &quot;value&quot; for the element with the index 1 you need to do:<br/><br/>my $value = $array[1][1]; #the value &quot;2&quot;<br/><br/>and to get the &quot;key&quot; for the element with the index 1, you can do:<br/><br/>my $key = $array[1][0]; #Will get &quot;b&quot;<br/><br/>I don&#39;t know what you need to do... that&#39;s why I gave the idea of using arrays of arrays...<br/><br/>--Octavian<br/><br/> ----- Original Message ----- <br/> From: richard <br/> To: beginners@perl.org <br/> Sent: Saturday, June 15, 2013 7:12 PM<br/> Subject: perl hash loop: keys() vs values()<br/><br/><br/> Hi<br/><br/><br/> I&#39;m trying to understand the difference between the keys() and values () operators. What I&#39;m not getting is why a hash must be reversed to get the key corresponding to a value? (as stated in perldoc). Can someone explain this please?<br/><br/><br/> Here is my test script; My perl is freebsd 5.8.9<br/><br/><br/> use warnings;<br/> use strict;<br/><br/><br/> my %hds = (&#39;1&#39;, &#39;0C8CB35F&#39;, &#39;2&#39;, &#39;0C9CB37D&#39;);<br/> print &quot;rows: &quot; . scalar keys(%hds) . &quot;\n\n&quot;;<br/><br/><br/> my $value;<br/> my $key;<br/><br/><br/> foreach $value (values %hds) {<br/> print &quot;key: $hds{$value} \n&quot;;<br/> print &quot;value: $value \n&quot;;<br/> }<br/><br/><br/> print &quot;--------------------------- \n&quot;;<br/><br/><br/> my %r_hds = reverse %hds;<br/> foreach $value (values %hds) {<br/> print &quot;key: $r_hds{$value} \n&quot;;<br/> print &quot;value: $value \n&quot;;<br/> }<br/><br/><br/> print &quot;--------------------------- \n&quot;;<br/><br/><br/> foreach $key (keys %hds) {<br/> print &quot;key: $key \n&quot;;<br/> print &quot;value: $hds{$key} \n&quot;;<br/> }<br/><br/><br/><br/><br/> Here is the output:<br/><br/><br/> Use of uninitialized value in concatenation (.) or string at h line 11.<br/> key:<br/> value: 0C8CB35F<br/> Use of uninitialized value in concatenation (.) or string at h line 11.<br/> key:<br/> value: 0C9CB37D<br/> ---------------------------<br/> key: 1<br/> value: 0C8CB35F<br/> key: 2<br/> value: 0C9CB37D<br/> ---------------------------<br/> key: 1<br/> value: 0C8CB35F<br/> key: 2<br/> value: 0C9CB37D<br/><br/><br/><br/><br/> --<br/> regards, Richard<br/> --<br/> tmqrichard@gmail.com<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123028.html Sat, 15 Jun 2013 18:19:34 +0000 perl hash loop: keys() vs values() by richard Hi<br/><br/>I&#39;m trying to understand the difference between the keys() and values ()<br/>operators. What I&#39;m not getting is why a hash must be reversed to get the<br/>key corresponding to a value? (as stated in perldoc). Can someone explain<br/>this please?<br/><br/>Here is my test script; My perl is freebsd 5.8.9<br/><br/>use warnings;<br/>use strict;<br/><br/>my %hds = (&#39;1&#39;, &#39;0C8CB35F&#39;, &#39;2&#39;, &#39;0C9CB37D&#39;);<br/>print &quot;rows: &quot; . scalar keys(%hds) . &quot;\n\n&quot;;<br/><br/>my $value;<br/>my $key;<br/><br/> foreach $value (values %hds) {<br/> print &quot;key: $hds{$value} \n&quot;;<br/> print &quot;value: $value \n&quot;;<br/> }<br/><br/>print &quot;--------------------------- \n&quot;;<br/><br/> my %r_hds = reverse %hds;<br/> foreach $value (values %hds) {<br/> print &quot;key: $r_hds{$value} \n&quot;;<br/> print &quot;value: $value \n&quot;;<br/> }<br/><br/>print &quot;--------------------------- \n&quot;;<br/><br/> foreach $key (keys %hds) {<br/> print &quot;key: $key \n&quot;;<br/> print &quot;value: $hds{$key} \n&quot;;<br/> }<br/><br/><br/>Here is the output:<br/><br/>Use of uninitialized value in concatenation (.) or string at h line 11.<br/>key:<br/>value: 0C8CB35F<br/>Use of uninitialized value in concatenation (.) or string at h line 11.<br/>key:<br/>value: 0C9CB37D<br/>---------------------------<br/>key: 1<br/>value: 0C8CB35F<br/>key: 2<br/>value: 0C9CB37D<br/>---------------------------<br/>key: 1<br/>value: 0C8CB35F<br/>key: 2<br/>value: 0C9CB37D<br/><br/><br/>--<br/>regards, Richard<br/>--<br/>tmqrichard@gmail.com<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123027.html Sat, 15 Jun 2013 16:13:05 +0000 OT pedagogy and the beginners@perl.org mailing list by David Christensen On 06/13/13 22:55, Shlomi Fish wrote:<br/>&gt; That&#39;s nice and dandy, but please don&#39;t publish the products of your<br/>&gt; &quot;one-liners turned into scripts&quot; here without cleaning them up first, because<br/>&gt; there are beginners on this mailing list who need to learn good practices from<br/>&gt; the code and posts here, and code such as yours encourages bad practices.<br/>&gt; People may also find code like that by web search which will further set back<br/>&gt; the pedagogic level.<br/>&gt; You&#39;ve also wasted my time in me having to comment on your post, and yours in<br/>&gt; having to comment on my response to yours.&lt;sigh /&gt;<br/><br/>Pedagogy has it&#39;s place, but beginners also need to see that:<br/><br/>1. Easy things can be easy in Perl.<br/><br/>2. Perl provides more TIMTOWDI than most other computer programming <br/>languages; this is a design feature.<br/><br/><br/>Rheotorical question -- which program would you rather write?<br/><br/>1. 1,000 lines of assembly.<br/><br/>2. 100 lines of C.<br/><br/>3. 10 lines of Java.<br/><br/>4. 1 line of Perl.<br/><br/><br/>David<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123026.html Sat, 15 Jun 2013 05:11:13 +0000 Re: sha-2 sum of files? by lee Shlomi Fish &lt;shlomif@shlomifish.org&gt; writes:<br/><br/>&gt;&gt; &gt; But if the size hasn&#39;t changed, then you still need to check something<br/>&gt;&gt; &gt; else. You can do another light check, or decide to do the heavy one.<br/>&gt;&gt; &gt;<br/>&gt;&gt; &gt; This is also important because a hash-value is only a fingerprint, so<br/>&gt;&gt; &gt; different files have (a small chance on having) the same hash value.<br/>&gt;&gt; &gt;<br/>&gt;&gt; &gt; The file size check makes the chance even smaller that you don&#39;t<br/>&gt;&gt; &gt; detect the change.<br/>&gt;&gt; <br/>&gt;&gt; Hm ok, this kinda sucks ... Imagine I check size and mtime and I have<br/>&gt;&gt; a SHA-256 hash. Now there are the following cases:<br/>&gt;&gt; <br/>&gt;&gt; <br/>&gt;&gt; + mtime AND size changed: file has changed, put into report, update index<br/>&gt;&gt; <br/>&gt;&gt; + EITHER mtime changed, size is same, OR size changed, mtime is same:<br/>&gt;<br/>&gt; If the size changed, then the file definitely changed.<br/><br/>You could have the (odd) case that I look at the file, make a change,<br/>decide to undo it and in the process I somehow add some whitespace<br/>without noticing: The file changed size, but it didn&#39;t really change ;)<br/><br/>&gt;&gt; How likely is it that the hash is the same though the file did change?<br/>&gt;<br/>&gt; Well, if you take SHA-256 for example, then its hash has 256 bits so you have a<br/>&gt; chance of 1 / (2**256) that two non-identical byte vectors will have the same<br/>&gt; contents.<br/>&gt;<br/>&gt; shlomif@telaviv1:~$ perl -Mbigint -E &#39;say 2 ** 256&#39;<br/>&gt; 115792089237316195423570985008687907853269984665640564039457584007913129639936<br/>&gt; shlomif@telaviv1:~$ <br/><br/>Hm that would be a really small chance :) As it&#39;s now, the script<br/>relies on size and mtime, i. e. when none of them change, the SHA-256<br/>isn&#39;t considered. Changing a file without having the change detected<br/>would have to be done deliberately, and it could still be detected with<br/>a modified script, unless I&#39;d also manage to keep the SHA-256 the same<br/>...<br/><br/><br/>-- <br/>&quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>you should be allowed to do anything with functions.&quot;<br/>http://www.joelonsoftware.com/items/2006/08/01.html<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123025.html Fri, 14 Jun 2013 21:48:29 +0000 Re: the perl script (SOLVED: sha-2 sum of files?) by lee Jim Gibson &lt;jimsgibson@gmail.com&gt; writes:<br/><br/>&gt; Some comments on your code:<br/>&gt;<br/>&gt; 1. Rather than doing this:<br/>&gt;<br/>&gt; while ( &lt;$curridx&gt;) { chomp $_; my $current_file = $_;<br/>&gt;<br/>&gt; you should do this:<br/>&gt;<br/>&gt; while ( my $current_file = &lt;$curridx&gt; ) { chomp($current_file);<br/><br/>Ah cool :) I really didn&#39;t like the way I did that, this is much nicer<br/>:)<br/><br/>&gt; 2. I would return undef from gethash() if the file doesn&#39;t exist,<br/>&gt; rather than &#39;~&#39;. Undef is false in a logical test, and can also be<br/>&gt; tested with the define() function.<br/><br/>Hmmmm ... When you run<br/><br/> perl -e &#39;if(undef == undef) {print &quot;eq\n&quot;;}&#39;<br/><br/>it prints &quot;eq&quot;. I don&#39;t know what<br/><br/> dbupdatehash($hashs_list_new, $current_file, $current_info[0],<br/> $current_info[1], gethash($current_file) );<br/><br/>would write into the hashs.list when gethash() returns undef. I guess<br/>it would fail with an error along the lines that an undefined value<br/>cannot be printed (joined since I changed that) with others.<br/><br/><br/>Working with non-existing files is actually a glitch in the script atm<br/>because when gethash() returns &#39;~&#39; and that is put into the hashs.list,<br/>next time when the non-existing file still doesn&#39;t exist, it may be<br/>considered as changed again because dbreadhash() returns (-1, -1, &quot;&quot;)<br/>either when the syntax is incorrect or the file isn&#39;t listed in the<br/>hashs.list, and getinfo() returns (-100, -100) as an impossible file<br/>size and impossible mtime.<br/><br/>That comes down to the script comparing a &quot;faked&quot; stored hash for the<br/>non-existing file that looks like (-1, -1, &quot;&quot;) with another &quot;faked&quot; hash<br/>created for a non-existing file that looks like (-100, -100, &quot;~&quot;). The<br/>non-existing file will then be considered as changed and reported.<br/><br/>Not knowing about &#39;undef&#39;, I did that intentional to work around the<br/>problem of non-existing files letting the scipt fail when it comes<br/>across them. Maybe that was a bad idea.<br/><br/>So gethash(), getinfo() and dbreadhash() would have to return &#39;undef&#39; in<br/>some cases. That breaks dbupdatehash(), so I&#39;d have to check their<br/>return values to skip non-existing files. I should make the script<br/>write its output to a logfile and make it read a configuration file<br/>telling it which filenames to use to work with.<br/><br/>&gt; 3. You are reading the close.list file for each file tested. It would<br/>&gt; be more efficient to read the close.list file and put the closed file<br/>&gt; names into a hash, and then you can test each file to see if it is in<br/>&gt; that hash, either with the exists() function, or just be testing the<br/>&gt; hash value. A hash evaluates to undef (false) if the key does not<br/>&gt; exist in the hash.<br/><br/>Cool --- I&#39;m reading up about such hashs now. What if the closed.index<br/>gets really large and needs to be kept in memory for this?<br/><br/>&gt; 4. You can use the join(EXPR,LIST) function here:<br/>&gt;<br/>&gt; print $file $key . &quot;:&quot; . $size . &quot;:&quot; . $mtime . &quot;:&quot; . $hash . &quot;\n&quot;;<br/>&gt;<br/>&gt; and do this instead:<br/>&gt;<br/>&gt; print $file join(&#39;:&#39;,($key,$size,$mtime,$hash)), &quot;\n&quot;;<br/><br/>sweet :)<br/><br/><br/>-- <br/>&quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>you should be allowed to do anything with functions.&quot;<br/>http://www.joelonsoftware.com/items/2006/08/01.html<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123024.html Fri, 14 Jun 2013 21:48:28 +0000 Re: sha-2 sum of files? by lee Jim Gibson &lt;jimsgibson@gmail.com&gt; writes:<br/><br/>&gt; On Jun 13, 2013, at 1:30 PM, lee wrote:<br/>&gt;<br/>&gt;&gt; In my application, my estimate is that there will be a set of around<br/>&gt;&gt; 100--150 files. Once a file is closed and reported one last time, it<br/>&gt;&gt; doesn&#39;t need to be considered anymore, so the number of relevant files<br/>&gt;&gt; is limited. Each file is only about 2kB in size.<br/>&gt; [...]<br/>&gt;<br/>&gt; If you have only 150 files, each 2kB in size, you can just make a copy<br/>&gt; of each file in another folder. Then, use the File::Compare module to<br/>&gt; compare the copy with the updated files in the original folder. No<br/>&gt; persistent data needed, no hashing, no looking at dates or sizes,<br/>&gt; guaranteed to find differences.<br/><br/>Yes, I&#39;ve been thinking about doing it that way. Detecting changes<br/>would be guaranteed, and it would limit the versatility of the script<br/>and create quite an overhead in duplicate files.<br/><br/>OTOH, what are the chances that when a file has changed, size /and/<br/>mtime /and/ SHA-256 hash remain the same? I&#39;d probably have to deal<br/>with many billions of files, or something would have to go really wrong,<br/>before this becomes a significant issue ...<br/><br/>There&#39;s no way to get around persistent data for this. To be able to<br/>detect changes, I would have to keep a copy of each file until the file<br/>is closed.<br/><br/><br/>-- <br/>&quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>you should be allowed to do anything with functions.&quot;<br/>http://www.joelonsoftware.com/items/2006/08/01.html<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123023.html Fri, 14 Jun 2013 21:48:27 +0000 Re: the perl script (SOLVED: sha-2 sum of files?) by Jim Gibson <br/>On Jun 13, 2013, at 10:57 PM, lee wrote:<br/><br/>&gt; Hi,<br/>&gt; <br/>&gt; so I&#39;ve done this script now --- my fourth perl script ever --- and<br/>&gt; since I&#39;m getting so much help here, I thought I&#39;d post it here. I&#39;m<br/>&gt; sure it could be done much better, it&#39;s just plain and simple.<br/>&gt; <br/>&gt; It has one problem: The list of closed files can grow indefinitely, and<br/>&gt; since the script checks whether a file that has been closed is already<br/>&gt; listed, performance will degrade with the number of files on the closed<br/>&gt; list increasing. This check isn&#39;t exactly needed and can be removed;<br/>&gt; other than that, I don&#39;t know (yet) what to do about that.<br/><br/>Some comments on your code:<br/><br/>1. Rather than doing this:<br/><br/> while ( &lt;$curridx&gt;) {<br/> chomp $_;<br/> my $current_file = $_;<br/><br/>you should do this:<br/><br/> while ( my $current_file = &lt;$curridx&gt; ) {<br/> chomp($current_file);<br/><br/>2. I would return undef from gethash() if the file doesn&#39;t exist, rather than &#39;~&#39;. Undef is false in a logical test, and can also be tested with the define() function.<br/><br/>3. You are reading the close.list file for each file tested. It would be more efficient to read the close.list file and put the closed file names into a hash, and then you can test each file to see if it is in that hash, either with the exists() function, or just be testing the hash value. A hash evaluates to undef (false) if the key does not exist in the hash.<br/><br/>4. You can use the join(EXPR,LIST) function here:<br/><br/> print $file $key . &quot;:&quot; . $size . &quot;:&quot; . $mtime . &quot;:&quot; . $hash . &quot;\n&quot;;<br/><br/>and do this instead:<br/><br/> print $file join(&#39;:&#39;,($key,$size,$mtime,$hash)), &quot;\n&quot;;<br/><br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123022.html Fri, 14 Jun 2013 13:50:12 +0000 Re: sha-2 sum of files? by Dr.Ruud On 14/06/2013 08:02, Shlomi Fish wrote:<br/>&gt; On Thu, 13 Jun 2013 22:51:24 +0200<br/>&gt; lee &lt;lee@yun.yagibdah.de&gt; wrote:<br/><br/>&gt;&gt; How likely is it that the hash is the same though the file did change?<br/>&gt;<br/>&gt; Well, if you take SHA-256 for example, then its hash has 256 bits so you have a<br/>&gt; chance of 1 / (2**256) that two non-identical byte vectors will have the same<br/>&gt; contents.<br/><br/>If you would only store the hash value, and none of the filename, <br/>filesize, etc. with it, then there is the &#39;birthday paradox&#39;.<br/>http://en.wikipedia.org/wiki/Birthday_problem<br/><br/><br/>For example, I have 2**20 (100M) email-addresses, each represents a <br/>customer.<br/><br/>I gave them a pseudo-id: the left half of the MD5 of the (normalized) <br/>email-address. A handy unsigned int of 64 bits. (MySQL: bigint unsigned)<br/><br/>No clashes yet, but in this case any clashes are expected and acceptable.<br/><br/>Also easy to test: generate 100M email-address-like strings,<br/>and count the MD5/2-clashes.<br/><br/>-- <br/>Ruud<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123021.html Fri, 14 Jun 2013 07:07:42 +0000 Re: sha-2 sum of files? by Shlomi Fish Hi Lee,<br/><br/>On Thu, 13 Jun 2013 22:51:24 +0200<br/>lee &lt;lee@yun.yagibdah.de&gt; wrote:<br/><br/>&gt; &quot;Dr.Ruud&quot; &lt;rvtol+usenet@isolution.nl&gt; writes:<br/>&gt; <br/>&gt; &gt; On 12/06/2013 10:27, lee wrote:<br/>&gt; &gt;<br/>&gt; &gt;&gt; File sizes do not reliably indicate whether a file has been modified or<br/>&gt; &gt;&gt; not.<br/>&gt; &gt;<br/>&gt; &gt; If the file size has changed, then your file has changed. That is 100%<br/>&gt; &gt; reliable, and is a quick and cheap check.<br/>&gt; <br/>&gt; It works only one way: different size --&gt; file has changed. What<br/>&gt; doesn&#39;t work is: file has changed --&gt; different size.<br/><br/>Right.<br/><br/>&gt; <br/>&gt; &gt; But if the size hasn&#39;t changed, then you still need to check something<br/>&gt; &gt; else. You can do another light check, or decide to do the heavy one.<br/>&gt; &gt;<br/>&gt; &gt; This is also important because a hash-value is only a fingerprint, so<br/>&gt; &gt; different files have (a small chance on having) the same hash value.<br/>&gt; &gt;<br/>&gt; &gt; The file size check makes the chance even smaller that you don&#39;t<br/>&gt; &gt; detect the change.<br/>&gt; <br/>&gt; Hm ok, this kinda sucks ... Imagine I check size and mtime and I have<br/>&gt; a SHA-256 hash. Now there are the following cases:<br/>&gt; <br/>&gt; <br/>&gt; + mtime AND size changed: file has changed, put into report, update index<br/>&gt; <br/>&gt; + EITHER mtime changed, size is same, OR size changed, mtime is same:<br/><br/>If the size changed, then the file definitely changed.<br/><br/>&gt; * compute hash<br/>&gt; * hash is different: file has changed, put into report, update index<br/>&gt; * hash is the same: manual intervention is required to decide<br/>&gt; whether the file should be in the report or not<br/>&gt; <br/>&gt; + NEITHER mtime, NOR size changed: do nothing<br/>&gt; <br/>&gt; <br/>&gt; How likely is it that the hash is the same though the file did change?<br/><br/>Well, if you take SHA-256 for example, then its hash has 256 bits so you have a<br/>chance of 1 / (2**256) that two non-identical byte vectors will have the same<br/>contents.<br/><br/>shlomif@telaviv1:~$ perl -Mbigint -E &#39;say 2 ** 256&#39;<br/>115792089237316195423570985008687907853269984665640564039457584007913129639936<br/>shlomif@telaviv1:~$ <br/><br/>&gt; And when you think of it, it&#39;s even possible (yet unlikely) that the<br/>&gt; file changed _and_ has still the same size and same mtime.<br/>&gt; <br/>&gt; So how do you reliably detect whether a file has changed or not? Create<br/>&gt; a copy of the file as it went into the report and when generating<br/>&gt; another report, compare the current file with the backup? Maybe not a<br/>&gt; bad idea, it would be pretty reliable without any complicated ado.<br/>&gt; <br/><br/>Yes, that&#39;s the only 100% fail-proof way that I can think about, but relying on<br/>a hash is not a bad idea.<br/><br/>Regards,<br/><br/> Shlomi Fish<br/><br/>-- <br/>-----------------------------------------------------------------<br/>Shlomi Fish http://www.shlomifish.org/<br/>Best Introductory Programming Language - http://shlom.in/intro-lang<br/><br/>COBOL is the old Java.<br/><br/>Please reply to list if it&#39;s a mailing list post - http://shlom.in/reply .<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123020.html Fri, 14 Jun 2013 06:02:17 +0000 the perl script (SOLVED: sha-2 sum of files?) by lee Hi,<br/><br/>so I&#39;ve done this script now --- my fourth perl script ever --- and<br/>since I&#39;m getting so much help here, I thought I&#39;d post it here. I&#39;m<br/>sure it could be done much better, it&#39;s just plain and simple.<br/><br/>It has one problem: The list of closed files can grow indefinitely, and<br/>since the script checks whether a file that has been closed is already<br/>listed, performance will degrade with the number of files on the closed<br/>list increasing. This check isn&#39;t exactly needed and can be removed;<br/>other than that, I don&#39;t know (yet) what to do about that.<br/><br/>In any case, I come to really like perl :)<br/><br/><br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123019.html Fri, 14 Jun 2013 05:57:29 +0000 Re: sha-2 sum of files? by Shlomi Fish Hi David,<br/><br/>On Thu, 13 Jun 2013 21:30:00 -0700<br/>David Christensen &lt;dpchrist@holgerdanske.com&gt; wrote:<br/><br/>&gt; On 06/13/13 01:41, Shlomi Fish wrote:<br/>&gt; &gt; 1. You&#39;re lacking strict and warnings:<br/>&gt; &gt; 2. You&#39;re looping using $_ :<br/>&gt; &gt; 3. You&#39;re using md5_hex by slurping the contents of the file into memory,<br/>&gt; &gt; 4. read_file was not explictly imported from File::Slurp:<br/>&gt; &gt; Both the Perl Cookbook and Programming Perl are showing their age.<br/>&gt; <br/>&gt; I have different quality metrics/ expectations for one-liners turned <br/>&gt; into scripts than I do for large-scale multi-person systems software <br/>&gt; products:<br/><br/>That&#39;s nice and dandy, but please don&#39;t publish the products of your<br/>&quot;one-liners turned into scripts&quot; here without cleaning them up first, because<br/>there are beginners on this mailing list who need to learn good practices from<br/>the code and posts here, and code such as yours encourages bad practices.<br/>People may also find code like that by web search which will further set back<br/>the pedagogic level.<br/><br/>You&#39;ve also wasted my time in me having to comment on your post, and yours in<br/>having to comment on my response to yours. &lt;sigh /&gt;<br/><br/>Regards,<br/><br/> Shlomi Fish<br/><br/>-- <br/>-----------------------------------------------------------------<br/>Shlomi Fish http://www.shlomifish.org/<br/>Buffy Factoids - http://www.shlomifish.org/humour/bits/facts/Buffy/<br/><br/>Chuck Norris has 0 messages in his E-mail inbox. Including already read ones.<br/> &mdash; http://www.shlomifish.org/humour/bits/facts/Chuck-Norris/<br/><br/>Please reply to list if it&#39;s a mailing list post - http://shlom.in/reply .<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123018.html Fri, 14 Jun 2013 05:55:38 +0000 Re: sha-2 sum of files? by David Christensen On 06/13/13 01:41, Shlomi Fish wrote:<br/>&gt; 1. You&#39;re lacking strict and warnings:<br/>&gt; 2. You&#39;re looping using $_ :<br/>&gt; 3. You&#39;re using md5_hex by slurping the contents of the file into memory,<br/>&gt; 4. read_file was not explictly imported from File::Slurp:<br/>&gt; Both the Perl Cookbook and Programming Perl are showing their age.<br/><br/>I have different quality metrics/ expectations for one-liners turned <br/>into scripts than I do for large-scale multi-person systems software <br/>products:<br/><br/>$ perl -MFile::Slurp -e &#39;use Digest::MD5 qw(md5_hex); print(&quot;$_: &quot;, <br/>md5_hex(read_file $_), &quot;\n&quot;) for @ARGV&#39; cgi*<br/>cgi_cookie.pl: 073a858f3e7c7d8461743f758bc99a76<br/>cgi_header.pl: 99b11f007ed9efceb3b500b8a30e0558<br/>cgi_param.pl: 25b88f62883301d08554cd5707e00d10<br/>cgi_redirect.pl: 69622c750a131d00b1c30c553fc7d916<br/><br/><br/>David<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123017.html Fri, 14 Jun 2013 04:30:20 +0000 Re: sha-2 sum of files? by Jim Gibson <br/>On Jun 13, 2013, at 1:30 PM, lee wrote:<br/><br/>&gt; In my application, my estimate is that there will be a set of around<br/>&gt; 100--150 files. Once a file is closed and reported one last time, it<br/>&gt; doesn&#39;t need to be considered anymore, so the number of relevant files<br/>&gt; is limited. Each file is only about 2kB in size. Reports will be<br/>&gt; generated only monthly.<br/>&gt; <br/>&gt; Considering this, it seems doubtful that the additional effort in<br/>&gt; programming and procedure required to handle exceptions in which not<br/>&gt; /both/ mtime and size have changed compared to simply go by hash only is<br/>&gt; worthwhile the benefit in performance: The effective difference in this<br/>&gt; case is probably like the difference between &quot;(almost) instantly&quot; and<br/>&gt; &quot;about 3 seconds&quot;.<br/><br/>If you have only 150 files, each 2kB in size, you can just make a copy of each file in another folder. Then, use the File::Compare module to compare the copy with the updated files in the original folder. No persistent data needed, no hashing, no looking at dates or sizes, guaranteed to find differences.<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123016.html Fri, 14 Jun 2013 02:21:17 +0000 Re: sha-2 sum of files? by lee &quot;Dr.Ruud&quot; &lt;rvtol+usenet@isolution.nl&gt; writes:<br/><br/>&gt; On 12/06/2013 11:33, lee wrote:<br/>&gt;&gt; Jim Gibson &lt;jimsgibson@gmail.com&gt; writes:<br/>&gt;&gt;&gt; On Jun 11, 2013, at 9:44 PM, lee wrote:<br/>&gt;<br/>&gt;&gt;&gt;&gt; I&#39;ve been googling for examples of how to create a sha-2 sum of a<br/>&gt;&gt;&gt;&gt; file in perl without success. What I&#39;m looking for is something<br/>&gt;&gt;&gt;&gt; like:<br/>&gt;&gt;&gt;&gt;<br/>&gt;&gt;&gt;&gt; $hash = create_sha2_sum( $filename);<br/>&gt;&gt;&gt;&gt;<br/>&gt;&gt;&gt;&gt; Do you know of any examples I could look at? Or is there a better<br/>&gt;&gt;&gt;&gt; way to figure out if a file has been modified?<br/>&gt;&gt;&gt;<br/>&gt;&gt;&gt; The first thing to do would be to check the file size. If the file<br/>&gt;&gt;&gt; size has changed, then the file has been modified. So you will want to<br/>&gt;&gt;&gt; save the file size.<br/>&gt;&gt;<br/>&gt;&gt; The file might be modified without changing its size ...<br/>&gt;&gt;<br/>&gt;&gt;&gt; If the files sizes are the same, then you can compare some sort of<br/>&gt;&gt;&gt; digest e.g. SHA. I haven&#39;t used any, so I cannot advise.<br/>&gt;&gt;<br/>&gt;&gt; ... so I&#39;m better off by just using a hash which I&#39;d need anyway.<br/>&gt;<br/>&gt; No. If the file is real big, then calculating the hash (of the new<br/>&gt; file) can take a long time. Which would be superfluous if the file<br/>&gt; size also has changed.<br/>&gt;<br/>&gt; I store: file size, fingerprint of first 256 bytes, fingerprint of<br/>&gt; total file. So only if both the size and the light fingerprint are the<br/>&gt; same, I need to check the full fingerprint.<br/><br/>Oh now I see your point: You are trying to avoid having to compute hashs<br/>for large files when this isn&#39;t needed, so you get much better<br/>efficiency by checking other information first.<br/><br/><br/>In my application, my estimate is that there will be a set of around<br/>100--150 files. Once a file is closed and reported one last time, it<br/>doesn&#39;t need to be considered anymore, so the number of relevant files<br/>is limited. Each file is only about 2kB in size. Reports will be<br/>generated only monthly.<br/><br/>Considering this, it seems doubtful that the additional effort in<br/>programming and procedure required to handle exceptions in which not<br/>/both/ mtime and size have changed compared to simply go by hash only is<br/>worthwhile the benefit in performance: The effective difference in this<br/>case is probably like the difference between &quot;(almost) instantly&quot; and<br/>&quot;about 3 seconds&quot;.<br/><br/><br/>OTOH, it&#39;s nicer to make it so that file size doesn&#39;t have a major<br/>impact on performance because the solution would be more versatile.<br/>Unfortunately, creating a hash only over (random) parts of the files<br/>won&#39;t suffice because a different part of the file might have changed<br/>than the one sampled. I don&#39;t want the handling of exceptions to<br/>require manual intervention, either. This means that I can&#39;t get around<br/>saving hashs for whole files. I could only save computing hashs for<br/>those files that still have the same size /and/ same mtime they had a<br/>month ago.<br/><br/>Having that said, I do like this idea. Hashs would need to be computed<br/>during report generation only when size and mtime indicate that a file<br/>might have changed, instead of computing them all every time just to see<br/>if a file did change. I think I&#39;ll probably go for that.<br/><br/><br/>-- <br/>&quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>you should be allowed to do anything with functions.&quot;<br/>http://www.joelonsoftware.com/items/2006/08/01.html<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123015.html Fri, 14 Jun 2013 00:17:53 +0000 Re: sha-2 sum of files? by lee &quot;Dr.Ruud&quot; &lt;rvtol+usenet@isolution.nl&gt; writes:<br/><br/>&gt; On 12/06/2013 10:27, lee wrote:<br/>&gt;<br/>&gt;&gt; File sizes do not reliably indicate whether a file has been modified or<br/>&gt;&gt; not.<br/>&gt;<br/>&gt; If the file size has changed, then your file has changed. That is 100%<br/>&gt; reliable, and is a quick and cheap check.<br/><br/>It works only one way: different size --&gt; file has changed. What<br/>doesn&#39;t work is: file has changed --&gt; different size.<br/><br/>&gt; But if the size hasn&#39;t changed, then you still need to check something<br/>&gt; else. You can do another light check, or decide to do the heavy one.<br/>&gt;<br/>&gt; This is also important because a hash-value is only a fingerprint, so<br/>&gt; different files have (a small chance on having) the same hash value.<br/>&gt;<br/>&gt; The file size check makes the chance even smaller that you don&#39;t<br/>&gt; detect the change.<br/><br/>Hm ok, this kinda sucks ... Imagine I check size and mtime and I have<br/>a SHA-256 hash. Now there are the following cases:<br/><br/><br/>+ mtime AND size changed: file has changed, put into report, update index<br/><br/>+ EITHER mtime changed, size is same, OR size changed, mtime is same:<br/> * compute hash<br/> * hash is different: file has changed, put into report, update index<br/> * hash is the same: manual intervention is required to decide<br/> whether the file should be in the report or not<br/><br/>+ NEITHER mtime, NOR size changed: do nothing<br/><br/><br/>How likely is it that the hash is the same though the file did change?<br/>And when you think of it, it&#39;s even possible (yet unlikely) that the<br/>file changed _and_ has still the same size and same mtime.<br/><br/>So how do you reliably detect whether a file has changed or not? Create<br/>a copy of the file as it went into the report and when generating<br/>another report, compare the current file with the backup? Maybe not a<br/>bad idea, it would be pretty reliable without any complicated ado.<br/><br/><br/>-- <br/>&quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>you should be allowed to do anything with functions.&quot;<br/>http://www.joelonsoftware.com/items/2006/08/01.html<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123014.html Fri, 14 Jun 2013 00:17:52 +0000 split() (Re: sha-2 sum of files?) by lee Jim Gibson &lt;jimsgibson@gmail.com&gt; writes:<br/><br/>&gt; On Jun 13, 2013, at 10:51 AM, lee wrote:<br/>&gt;&gt; + When I create files with lines like &quot;filename:size:mtime&quot; or<br/>&gt;&gt; &quot;filename:hash&quot;, is there something built in to read a syntax like<br/>&gt;&gt; this into, let&#39;s say, an array like &quot;my @fileinfo;&quot; with $fileinfo[0]<br/>&gt;&gt; being the file name, $fileinfo[1] the hash?<br/>&gt;&gt; Such &#39;key:value:...&#39; combinations is probably something frequently<br/>&gt;&gt; used. I&#39;m finding some examples to this and don&#39;t know what would be<br/>&gt;&gt; the best way to do it.<br/>&gt;<br/>&gt;<br/>&gt; The split function is the normal way of parsing lines like &#39;filename:hash&#39;. You have to make sure that your data fields do not contain the &#39;:&#39; character, or whatever character you use to delimit your fields.<br/>&gt;<br/>&gt; my @fields = split(&#39;:&#39;,$line);<br/>&gt;<br/>&gt; For more complicated formats, the Text::CSV module can be used.<br/>&gt;<br/>&gt; You could also use the Storable or Data::Dumper modules to write out a hash or array containing your data structure to a file and read it back the next time you run your program.<br/><br/>Thank you!<br/><br/>So I started implementing some functions (simple and inefficient), and<br/>for reading size, mtime and has from a file, I came up with this:<br/><br/><br/>[CODE]<br/>[...]<br/>sub dbreadhash {<br/> #<br/> # dbreadhash(&quot;hashs.list&quot;, &quot;filename&quot;): find the entry for &quot;filename&quot; in the<br/> # hashs.list and return size, mtime and hash<br/> #<br/> my ($filename, $key) = @_;<br/><br/> $key .= &quot;:&quot;;<br/> my @ret;<br/><br/> if( -e $filename) {<br/> open(my $file, &quot;&lt;$filename&quot;);<br/> while( &lt;$file&gt; ) {<br/> chomp $_;<br/> if( m/^$key/ ) {<br/> @ret = split(&#39;:&#39;, $&#39;); #&#39;<br/> last;<br/> }<br/> }<br/> close($file);<br/> }<br/> else {<br/> print $filename . &quot; not found\n&quot;;<br/> }<br/><br/> if(scalar(@ret) != 3) {<br/> print &quot;no or invalid syntax in &quot; . $filename . &quot;\n&quot;;<br/> @ret = ( -1, -1, &quot;&quot;);<br/> }<br/><br/> return @ret;<br/>}<br/><br/><br/># test: try out dbreadhash<br/><br/>my @test = dbreadhash(&quot;testfile&quot;, &quot;this&quot;);<br/>print $test[0] . &quot;\n&quot;;<br/>[/CODE]<br/><br/><br/>This is expecting lines like:<br/><br/>/path/filename:123:456:hash<br/><br/>How is the return value of the split() function defined for instances<br/>when splitting is not possible? All the documentation I found uses<br/>examples that can be split, and checking the array size like this is<br/>merely based on assumptions ...<br/><br/><br/>-- <br/>&quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>you should be allowed to do anything with functions.&quot;<br/>http://www.joelonsoftware.com/items/2006/08/01.html<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123013.html Fri, 14 Jun 2013 00:17:49 +0000 Re: SOLVED: sha-2 sum of files? by lee Jim Gibson &lt;jimsgibson@gmail.com&gt; writes:<br/><br/>&gt; On Jun 13, 2013, at 11:33 AM, lee wrote:<br/>&gt;<br/>&gt;&gt; Shlomi Fish &lt;shlomif@shlomifish.org&gt; writes:<br/>&gt;&gt; <br/>&gt;&gt; <br/>&gt;&gt;&gt;&gt;&gt;&gt; our $fh;<br/>&gt;&gt;&gt;&gt;&gt; <br/>&gt;&gt;&gt;&gt;&gt; Why the our?<br/>&gt;&gt;&gt;&gt; <br/>&gt;&gt;&gt;&gt; I&#39;ve been reading that you need to declare variables before using them<br/>&gt;&gt;&gt;&gt; when you use strict. A &quot;my $fh;&quot; would probably suffice.<br/>&gt;&gt;&gt; <br/>&gt;&gt;&gt; You can do the declaration during the open.<br/>&gt;&gt; <br/>&gt;&gt; Yes --- but I like it when the variables are declared in a &quot;consistent&quot;<br/>&gt;&gt; way like you do in C, rather than declaring them all over the place<br/>&gt;&gt; and/or even within other statements. I haven&#39;t figured out yet how I&#39;ll<br/>&gt;&gt; do this in perl.<br/>&gt;<br/>&gt; You should declare variables as late as possible. That is true in both<br/>&gt; C and Perl. Old C compilers would force you to declare variables at<br/>&gt; the beginning of each subroutine, but that is no longer the<br/>&gt; case. Putting all of the variable declarations at the beginning of the<br/>&gt; subroutine expands their scope and lifetime unnecessarily, which can<br/>&gt; lead to variable interference problems and turns local variables into<br/>&gt; global ones.<br/><br/>Hmmmm ... that&#39;s a good point. It&#39;s &quot;more natural&quot;, too, so I&#39;ll do it<br/>that way.<br/><br/><br/>-- <br/>&quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>you should be allowed to do anything with functions.&quot;<br/>http://www.joelonsoftware.com/items/2006/08/01.html<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123012.html Fri, 14 Jun 2013 00:17:49 +0000 Re: sha-2 sum of files? by John W. Krahn Jim Gibson wrote:<br/>&gt;<br/>&gt; On Jun 13, 2013, at 10:51 AM, lee wrote:<br/>&gt;&gt;<br/>&gt;&gt; + Is &quot;print&quot; and &quot;printf&quot; pretty much the same thing implementation<br/>&gt;&gt; wise? I&#39;m wondering if &quot;printf&quot; might involve more overhead so it<br/>&gt;&gt; might be less efficient, depending on what you&#39;re doing.<br/>&gt;<br/>&gt; They are pretty much the same. print will use its default formats for<br/>&gt; converting numerical data to strings. printf will use the format<br/>&gt; specifiers you provide. There can&#39;t be much difference in execution<br/>&gt; speed, probably not even enough to measure.<br/><br/>print and printf both use filehandles the same:<br/><br/>$ perl -Mwarnings -Mstrict -e&#39;<br/>print STDERR &quot;Hello print\n&quot;;<br/>printf STDERR q/%s/, &quot;Hello printf\n&quot;;<br/>&#39;<br/>Hello print<br/>Hello printf<br/><br/><br/>Perl allows interpolation in double quoted strings so you shouldn&#39;t use <br/>it in printf&#39;s format string:<br/><br/>$ perl -Mwarnings -Mstrict -e&#39;<br/>my ( $x, $y, $z ) = ( &quot;10%&quot;, &quot;15%&quot;, &quot;26%&quot; );<br/>print &quot;Hello $x, $y, $z\n&quot;;<br/>printf &quot;Hello $x, $y, $z\n&quot;;<br/>&#39;<br/>Hello 10%, 15%, 26%<br/>Invalid conversion in printf: &quot;%,&quot; at -e line 4.<br/>Invalid conversion in printf: &quot;%,&quot; at -e line 4.<br/>Invalid conversion in printf: &quot;%\012&quot; at -e line 4.<br/>Hello 10%, 15%, 26%<br/><br/><br/>print is affected by the variables $, and $\ but printf isn&#39;t:<br/><br/>$ perl -Mwarnings -Mstrict -e&#39;<br/>my ( $x, $y, $z ) = ( &quot;10%&quot;, &quot;15%&quot;, &quot;26%&quot; );<br/>( $,, $\ ) = ( &quot;&lt;*MIDDLE*&gt;&quot;, &quot;&lt;*THE END*&gt;&quot; );<br/>print &quot;Hello&quot;, $x, $y, $z, &quot;\n&quot;;<br/>printf &quot;Hello %s, %s, %s\n&quot;, $x, $y, $z;<br/>&#39;<br/>Hello&lt;*MIDDLE*&gt;10%&lt;*MIDDLE*&gt;15%&lt;*MIDDLE*&gt;26%&lt;*MIDDLE*&gt;<br/>&lt;*THE END*&gt;Hello 10%, 15%, 26%<br/><br/><br/><br/><br/>John<br/>-- <br/>Any intelligent fool can make things bigger and<br/>more complex... It takes a touch of genius -<br/>and a lot of courage to move in the opposite<br/>direction. -- Albert Einstein<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123011.html Thu, 13 Jun 2013 22:01:07 +0000 Re: SOLVED: sha-2 sum of files? by Jim Gibson <br/>On Jun 13, 2013, at 11:33 AM, lee wrote:<br/><br/>&gt; Shlomi Fish &lt;shlomif@shlomifish.org&gt; writes:<br/>&gt; <br/>&gt; <br/>&gt;&gt;&gt;&gt;&gt; our $fh;<br/>&gt;&gt;&gt;&gt; <br/>&gt;&gt;&gt;&gt; Why the our?<br/>&gt;&gt;&gt; <br/>&gt;&gt;&gt; I&#39;ve been reading that you need to declare variables before using them<br/>&gt;&gt;&gt; when you use strict. A &quot;my $fh;&quot; would probably suffice.<br/>&gt;&gt; <br/>&gt;&gt; You can do the declaration during the open.<br/>&gt; <br/>&gt; Yes --- but I like it when the variables are declared in a &quot;consistent&quot;<br/>&gt; way like you do in C, rather than declaring them all over the place<br/>&gt; and/or even within other statements. I haven&#39;t figured out yet how I&#39;ll<br/>&gt; do this in perl.<br/><br/>You should declare variables as late as possible. That is true in both C and Perl. Old C compilers would force you to declare variables at the beginning of each subroutine, but that is no longer the case. Putting all of the variable declarations at the beginning of the subroutine expands their scope and lifetime unnecessarily, which can lead to variable interference problems and turns local variables into global ones.<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123010.html Thu, 13 Jun 2013 20:28:31 +0000 Re: sha-2 sum of files? by Jim Gibson <br/>On Jun 13, 2013, at 10:51 AM, lee wrote:<br/><br/>&gt; #!/bin/perl<br/>&gt; <br/>&gt; use strict;<br/>&gt; use warnings;<br/>&gt; <br/>&gt; chomp $ARGV[0];<br/>&gt; my @info = (stat ($ARGV[0] ) );<br/>&gt; <br/>&gt; printf(&quot;%s: %d bytes, %d mtime\n&quot;, $ARGV[0], $info[7], $info[9] );<br/>&gt; <br/>&gt; <br/>&gt; Some more questions to this:<br/>&gt; <br/>&gt; <br/>&gt; + Is &quot;chomp $ARGV[0];&quot; even allowed? I wouldn&#39;t do anything like that<br/>&gt; in C :)<br/><br/>chomp($ARGV[0]) does nothing, because the elements of the @ARGV array do not have end-of-line characters (not even the last one). chomp is used after reading a line from a file with the read line operator &lt;&gt;, which leaves the EOL character at the end.<br/><br/>&gt; <br/>&gt; + Is &quot;print&quot; and &quot;printf&quot; pretty much the same thing implementation<br/>&gt; wise? I&#39;m wondering if &quot;printf&quot; might involve more overhead so it<br/>&gt; might be less efficient, depending on what you&#39;re doing.<br/><br/>They are pretty much the same. print will use its default formats for converting numerical data to strings. printf will use the format specifiers you provide. There can&#39;t be much difference in execution speed, probably not even enough to measure.<br/><br/>&gt; <br/>&gt; + When I create files with lines like &quot;filename:size:mtime&quot; or<br/>&gt; &quot;filename:hash&quot;, is there something built in to read a syntax like<br/>&gt; this into, let&#39;s say, an array like &quot;my @fileinfo;&quot; with $fileinfo[0]<br/>&gt; being the file name, $fileinfo[1] the hash?<br/>&gt; Such &#39;key:value:...&#39; combinations is probably something frequently<br/>&gt; used. I&#39;m finding some examples to this and don&#39;t know what would be<br/>&gt; the best way to do it.<br/><br/><br/>The split function is the normal way of parsing lines like &#39;filename:hash&#39;. You have to make sure that your data fields do not contain the &#39;:&#39; character, or whatever character you use to delimit your fields.<br/><br/> my @fields = split(&#39;:&#39;,$line);<br/><br/>For more complicated formats, the Text::CSV module can be used.<br/><br/>You could also use the Storable or Data::Dumper modules to write out a hash or array containing your data structure to a file and read it back the next time you run your program.<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123009.html Thu, 13 Jun 2013 20:20:40 +0000 Re: SOLVED: sha-2 sum of files? by lee Shlomi Fish &lt;shlomif@shlomifish.org&gt; writes:<br/><br/>&gt; Hi Lee,<br/>&gt;<br/>&gt; thanks for replying to the list and for not top posting.<br/><br/>Yvw :) I hate top posting --- only in rare cases, I find it<br/>useful/reasonable. And posting the solution here might help others.<br/><br/>&gt;&gt; &gt;&gt; #!/bin/perl<br/>&gt;&gt; &gt;<br/>&gt;&gt; &gt; Is your Perl at /bin? Shouldn&#39;t it be in /usr/bin?<br/>&gt;&gt; <br/>&gt;&gt; It&#39;s in /bin:<br/>&gt;&gt; <br/>&gt;&gt; ,----<br/>&gt;&gt; | [~/src/perl/test] which perl<br/>&gt;&gt; | /bin/perl<br/>&gt;&gt; | [~/src/perl/test]<br/>&gt;&gt; `----<br/>&gt;&gt; <br/>&gt;<br/>&gt; Ah, is this part of the /usrmove ?<br/>&gt; http://fedoraproject.org/wiki/Features/UsrMove<br/><br/>They&#39;re saying: &quot;Provide a simple way of mounting almost the entire<br/>installed operating system read-only, atomically snapshot it, or share<br/>it between multiple hosts to save maintenance and space.&quot;<br/><br/>I don&#39;t know, the installer of Fedora 17 forced me to put /usr onto the<br/>root partition, so I probably can&#39;t mount it ro. Fedora has its quirks<br/>...<br/><br/>&gt;&gt; &gt;&gt; our $fh;<br/>&gt;&gt; &gt;<br/>&gt;&gt; &gt; Why the our?<br/>&gt;&gt; <br/>&gt;&gt; I&#39;ve been reading that you need to declare variables before using them<br/>&gt;&gt; when you use strict. A &quot;my $fh;&quot; would probably suffice.<br/>&gt;<br/>&gt; You can do the declaration during the open.<br/><br/>Yes --- but I like it when the variables are declared in a &quot;consistent&quot;<br/>way like you do in C, rather than declaring them all over the place<br/>and/or even within other statements. I haven&#39;t figured out yet how I&#39;ll<br/>do this in perl.<br/><br/>&gt;&gt; &gt;&gt; $ctx-&gt;addfile( $fh);<br/>&gt;&gt; &gt;<br/>&gt;&gt; &gt; You can also add a filename using &quot;-&gt;addfile ()&quot;<br/>&gt;&gt; <br/>&gt;&gt; The description on [1] says it needs a file handle?<br/>&gt;<br/>&gt; Well, https://metacpan.org/module/MSHELOR/Digest-SHA-5.84/lib/Digest/SHA.pm is<br/>&gt; OK with giving a file.<br/><br/>Hm indeed --- and it&#39;s working. That shows that using google to find<br/>info about perl can be very misleading.<br/><br/>&gt;&gt; &gt; Perhaps learn Perl properly using some of the resources here:<br/>&gt;&gt; <br/>&gt;&gt; Yes, I should start at the beginning and/or take a class. I got so far<br/>&gt;&gt; that I can do what is needed, with the exception of creating the hashs.<br/>&gt;&gt; So maybe I&#39;ll have to use an external program like shasum until I figure<br/>&gt;&gt; out how to use the Digest ...<br/>&gt;<br/>&gt; There are some reasons for why not to use external programs:<br/>&gt;<br/>&gt; http://perl-begin.org/tutorials/bad-elements/#calling-the-shell-too-much<br/><br/>It&#39;s probably very inefficient, that&#39;s why I don&#39;t want to do it ...<br/>I&#39;ll have to read this page :)<br/><br/><br/>-- <br/>&quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>you should be allowed to do anything with functions.&quot;<br/>http://www.joelonsoftware.com/items/2006/08/01.html<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123008.html Thu, 13 Jun 2013 18:34:14 +0000 Re: sha-2 sum of files? by lee Jim Gibson &lt;jimsgibson@gmail.com&gt; writes:<br/><br/>&gt; On Jun 12, 2013, at 2:33 AM, lee wrote:<br/>&gt;<br/>&gt;&gt; Jim Gibson &lt;jimsgibson@gmail.com&gt; writes:<br/>&gt;&gt; <br/>&gt;&gt;&gt; <br/>&gt;&gt;&gt; The first thing to do would be to check the file size. If the file<br/>&gt;&gt;&gt; size has changed, then the file has been modified. So you will want to<br/>&gt;&gt;&gt; save the file size.<br/>&gt;&gt; <br/>&gt;&gt; The file might be modified without changing its size ...<br/>&gt;<br/>&gt; That is true. The advantage of checking the file size first is that it is a very quick test that does not require reading the entire file. You can get the file size in Perl one of two ways:<br/>&gt;<br/>&gt; 1. my $size = (stat($filename))[7];<br/>&gt;<br/>&gt; or <br/>&gt;<br/>&gt; 2. my $size = -s $filename;<br/><br/>Number 2 is particularly nice :)<br/><br/>A combination of size and mtime might be useful. I&#39;d have to handle an<br/>exception for instances when either the size or the mtime has changed<br/>but not both. That&#39;s something to think about; it&#39;s surely much more<br/>efficient than computing hashs ...<br/><br/><br/>#!/bin/perl<br/><br/>use strict;<br/>use warnings;<br/><br/>chomp $ARGV[0];<br/>my @info = (stat ($ARGV[0] ) );<br/><br/>printf(&quot;%s: %d bytes, %d mtime\n&quot;, $ARGV[0], $info[7], $info[9] );<br/><br/><br/>Some more questions to this:<br/><br/><br/>+ Is &quot;chomp $ARGV[0];&quot; even allowed? I wouldn&#39;t do anything like that<br/> in C :)<br/><br/>+ Is &quot;print&quot; and &quot;printf&quot; pretty much the same thing implementation<br/> wise? I&#39;m wondering if &quot;printf&quot; might involve more overhead so it<br/> might be less efficient, depending on what you&#39;re doing.<br/><br/>+ When I create files with lines like &quot;filename:size:mtime&quot; or<br/> &quot;filename:hash&quot;, is there something built in to read a syntax like<br/> this into, let&#39;s say, an array like &quot;my @fileinfo;&quot; with $fileinfo[0]<br/> being the file name, $fileinfo[1] the hash?<br/> Such &#39;key:value:...&#39; combinations is probably something frequently<br/> used. I&#39;m finding some examples to this and don&#39;t know what would be<br/> the best way to do it.<br/><br/><br/>-- <br/>&quot;Object-oriented programming languages aren&#39;t completely convinced that<br/>you should be allowed to do anything with functions.&quot;<br/>http://www.joelonsoftware.com/items/2006/08/01.html<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123007.html Thu, 13 Jun 2013 18:16:49 +0000 Re: sha-2 sum of files? by Shlomi Fish Hello David,<br/><br/>some comments on your code.<br/><br/>On Wed, 12 Jun 2013 15:53:32 -0700<br/>David Christensen &lt;dpchrist@holgerdanske.com&gt; wrote:<br/><br/>&gt; On 06/12/13 02:28, lee wrote:<br/>&gt; &gt; Yes, I&#39;ve been looking at descriptions like that and I don&#39;t know perl<br/>&gt; &gt; well enough to understand them.<br/>&gt; ...<br/>&gt; &gt; This is why I was looking for an example so I can see how these things<br/>&gt; &gt; are being used.<br/>&gt; <br/>&gt; Does this make sense?<br/>&gt; <br/>&gt; 2013-06-12 15:51:48 dpchrist@desktop ~/sandbox/perl<br/>&gt; $ cat Digest.pl<br/>&gt; #!/usr/bin/perl<br/>&gt; use Digest::MD5 qw(md5_hex);<br/>&gt; use File::Slurp;<br/>&gt; for (@ARGV) {<br/>&gt; print &quot;$_: &quot;, md5_hex(read_file $_), &quot;\n&quot;;<br/>&gt; }<br/><br/>1. You&#39;re lacking strict and warnings:<br/><br/>http://perl-begin.org/tutorials/bad-elements/#no-strict-and-warnings<br/><br/>2. You&#39;re looping using $_ :<br/><br/>http://perl-begin.org/tutorials/bad-elements/#overuse_dollar_underscore <br/><br/>3. You&#39;re using md5_hex by slurping the contents of the file into memory,<br/>which may be very large. It is better to let it read from the file handle, or<br/>the filename directly.<br/><br/>4. read_file was not explictly imported from File::Slurp:<br/><br/>http://perl-begin.org/tutorials/bad-elements/#non_explicitly_imported_symbols<br/><br/>( Note that the perl-begin.org site is my work, but I base my advice on other<br/>sources. ).<br/><br/>&gt; <br/>&gt; 2013-06-12 15:51:51 dpchrist@desktop ~/sandbox/perl<br/>&gt; $ perl Digest.pl cgi*.pl<br/>&gt; cgi_cookie.pl: 073a858f3e7c7d8461743f758bc99a76<br/>&gt; cgi_header.pl: 99b11f007ed9efceb3b500b8a30e0558<br/>&gt; cgi_param.pl: 25b88f62883301d08554cd5707e00d10<br/>&gt; cgi_redirect.pl: 69622c750a131d00b1c30c553fc7d916<br/>&gt; <br/>&gt; &gt; I wanted to learn perl for ages and never got around to it. Now I&#39;m<br/>&gt; &gt; finding it extremely useful, and I&#39;m learning it the wrong way by not<br/>&gt; &gt; starting at the beginning ...<br/>&gt; <br/>&gt; I learned Perl from three books:<br/>&gt; <br/>&gt; http://www.mail-archive.com/beginners@perl.org/msg115211.html<br/>&gt; <br/><br/>Both the Perl Cookbook and Programming Perl are showing their age. There are<br/>some more up-to-date resources at http://perl-begin.org/ and<br/>http://perl-tutorial.org/ .<br/><br/>Regards,<br/><br/> Shlomi Fish <br/><br/>-- <br/>-----------------------------------------------------------------<br/>Shlomi Fish http://www.shlomifish.org/<br/>Beginners Site for the Vim text editor - http://vim.begin-site.org/<br/><br/>Chuck Norris doesn&rsquo;t commit changes, the changes commit for him.<br/> &mdash; Araujo<br/><br/>Please reply to list if it&#39;s a mailing list post - http://shlom.in/reply .<br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123006.html Thu, 13 Jun 2013 08:41:58 +0000 Re: sha-2 sum of files? by Dr.Ruud On 12/06/2013 10:27, lee wrote:<br/><br/>&gt; File sizes do not reliably indicate whether a file has been modified or<br/>&gt; not.<br/><br/>If the file size has changed, then your file has changed. That is 100% <br/>reliable, and is a quick and cheap check.<br/><br/>But if the size hasn&#39;t changed, then you still need to check something <br/>else. You can do another light check, or decide to do the heavy one.<br/><br/>This is also important because a hash-value is only a fingerprint, so <br/>different files have (a small chance on having) the same hash value.<br/><br/>The file size check makes the chance even smaller that you don&#39;t detect <br/>the change.<br/><br/>-- <br/>Ruud<br/><br/> http://www.nntp.perl.org/group/perl.beginners/2013/06/msg123005.html Thu, 13 Jun 2013 08:06:59 +0000