Compiler options perl
Hello, I would like to know, how to force perl unfold foreach expression during the compilation, i.e. I want next code: foreach (1..100) { block } to be compiled like this: block[$_ = 1] block[$_ = 2] block[$_ = 3] . . . block[$_ = 100] Where block[$_ = n] means block with all occurences of $_ substituted with n. Of course, we presume that array like (1..100) is known at compile time. Regards, -edwin -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Cannot resolve the read/write issue...
Please help... *This is the original line in outFile* *This goes into inFile* I was expecting to find the two lines above in the file *outFile* after running the code below: #!/usr/bin/env perl use 5.010; `touch inFile`; `touch outFile`; `chmod 744 inFile`; `chmod 744 outFile`; `echo This goes into inFile inFile`; print Finished writing to inFile\n; `echo This is the original line in outFile outFile`; print Finished writing to outFile\n; open(IN,inFile) || die cannot open input file; print Input file opened...\n; open(OUT,outFile) || die cannot open output file; print Output file opened...\n; while(IN) { print OUT $_; print Data entered in outFile\n; } close(IN); close(OUT); `echo This 2nd line goes into inFile inFile`; print Finished writing to inFile\n; `echo This is the 2nd original line in outFile outFile`; print Finished writing to outFile\n; This is the output on the console: Finished writing to inFile Finished writing to outFile Input file opened… Output file opened… Data entered in outFile Finished writing to inFile Finished writing to outFile When I open the outFile, I find this single line instead of the expected two: *This is the original line in outFile* I was expecting to find this line as well: *This goes into inFile*, in the outFilel. It seems between the *open(IN...)* and *close(OUT)* something is not working but I cannot figure it out. Please assist. Joseph. -- Isaiah 40:31 But they that wait upon the LORD shall renew their strength...
Re: Compiler options perl
On 4/16/09 Thu Apr 16, 2009 1:39 PM, edw...@yandex.ru edw...@yandex.ru scribbled: Hello, I would like to know, how to force perl unfold foreach expression during the compilation, i.e. I want next code: foreach (1..100) { block } to be compiled like this: block[$_ = 1] block[$_ = 2] block[$_ = 3] . . . block[$_ = 100] Where block[$_ = n] means block with all occurences of $_ substituted with n. Of course, we presume that array like (1..100) is known at compile time. Can you explain why you want to do this? Compiler and run-time options for Perl are given in 'perldoc perlrun'. I don't see anything there that will affect how the Perl compiler optimizes the code it generates or specifically to unfold loops. I suggest you write a program that measures the execution times for the iterative-loop case and the unfolded case and compare them to see if you will gain enough execution time (if that is indeed what you are seeking) to justify the additional memory needed to contain the unwrapped block statements. (Were I to do this, I would write a Perl program to generate the Perl program!) See the Benchmark module. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
help needed to get over endless loop
Hi I had this semi-working, changed something and can't remember where I went right, so would appreciate some help getting back on top. I know 1..10 and 2..10 probably won't work in the following example, I have just changed lines to show what I am trying to get. $mystart = -2; $i = 1; for ($i = 1..10 ) { while ($i = 1 ) { if ($mystart 1 ) {print line 1 no data} if ($mystart 0 ) {print line 1 data} $mystart++ ; $i++; } } while ($i = 2..10 ) { if ($mystart 1 ) {print line 2..10 no data} if ($mystart 0 ) {print line 2..10 with data} $mystart++ ; $i++; } } thanks Brian -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
On 4/17/09 Fri Apr 17, 2009 10:02 AM, Brian brian5432...@yahoo.co.uk scribbled: Hi I had this semi-working, changed something and can't remember where I went right, so would appreciate some help getting back on top. I know 1..10 and 2..10 probably won't work in the following example, I have just changed lines to show what I am trying to get. $mystart = -2; $i = 1; for ($i = 1..10 ) { for my $I ( 1..10 ) { while ($i = 1 ) { '=' is assignment, '==' is test for numerical equality. This loop will never end, as $I gets assigned to the value 1 each time through the loop. while( $i == 1 ) { if ($mystart 1 ) {print line 1 no data} if ($mystart 0 ) {print line 1 data} $mystart++ ; $i++; You probably don't want to change $i inside a 'for my $I ( 1 .. 10 )' loop, as the change will be overwritten at the next loop iteration. If you really need to modify the loop iterator, then you should use a C-style for loop: for( my $i = 0; $i =10; $i++ ) { ... } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Cannot resolve the read/write issue...
Joseph Mwesigwa Bbaale wrote: Please help... *This is the original line in outFile* *This goes into inFile* I was expecting to find the two lines above in the file *outFile* after running the code below: #!/usr/bin/env perl use warnings; use strict; use 5.010; `touch inFile`; `touch outFile`; `chmod 744 inFile`; `chmod 744 outFile`; `echo This goes into inFile inFile`; print Finished writing to inFile\n; `echo This is the original line in outFile outFile`; print Finished writing to outFile\n; open OUT, '', 'inFile' or die Cannot open 'inFile' $!; print OUT This goes into inFile\n; close OUT; chmod 0744, 'inFile' or die Cannot chmod 'inFile' $!; print Finished writing to inFile\n; open OUT, '', 'outFile' or die Cannot open 'outFile' $!; print OUT This is the original line in outFile\n; close OUT; chmod 0744, 'outFile' or die Cannot chmod 'outFile' $!; print Finished writing to outFile\n; open(IN,inFile) || die cannot open input file; print Input file opened...\n; open(OUT,outFile) || die cannot open output file; print Output file opened...\n; while(IN) { print OUT $_; print Data entered in outFile\n; } close(IN); close(OUT); open IN, '', 'inFile' or die Cannot open 'inFile' $!; print Input file opened...\n; open OUT, '', 'outFile' or die Cannot open 'outFile' $!; print Output file opened...\n; while ( IN ) { print OUT $_; print Data entered in outFile\n; } close IN; close OUT; `echo This 2nd line goes into inFile inFile`; print Finished writing to inFile\n; `echo This is the 2nd original line in outFile outFile`; print Finished writing to outFile\n; open OUT, '', 'inFile' or die Cannot open 'inFile' $!; print OUT This 2nd line goes into inFile\n; print Finished writing to inFile\n; open OUT, '', 'outFile' or die Cannot open 'outFile' $!; print OUT This is the 2nd original line in outFile\n; print Finished writing to outFile\n; John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
RE: help needed to get over endless loop
-Original Message- From: Brian [mailto:brian5432...@yahoo.co.uk] Sent: Friday, April 17, 2009 11:03 To: Perl Beginners Subject: help needed to get over endless loop Hi I had this semi-working, changed something and can't remember where I went right, so would appreciate some help getting back on top. I know 1..10 and 2..10 probably won't work in the following example, I have just changed lines to show what I am trying to get. You are not running with strict, warnings which would give you a heads up $mystart = -2; $i = 1; for ($i = 1..10 ) { while ($i = 1 ) { You would get warning if warnings were on. You are assigning 1 to $i and not $i == 1. Better to give your code for the list to review. If you have any questions and/or problems, please let me know. Thanks. Wags ;) David R. Wagner Senior Programmer Analyst FedEx Freight 1.719.484.2097 TEL 1.719.484.2419 FAX 1.408.623.5963 Cell http://fedex.com/us if ($mystart 1 ) {print line 1 no data} if ($mystart 0 ) {print line 1 data} $mystart++ ; $i++; } } while ($i = 2..10 ) { if ($mystart 1 ) {print line 2..10 no data} if ($mystart 0 ) {print line 2..10 with data} $mystart++ ; $i++; } } thanks Brian -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
Wagner, David --- Senior Programmer Analyst --- CFS wrote: -Original Message- From: Brian [mailto:brian5432...@yahoo.co.uk] Sent: Friday, April 17, 2009 11:03 To: Perl Beginners Subject: help needed to get over endless loop Hi I had this semi-working, changed something and can't remember where I went right, so would appreciate some help getting back on top. I know 1..10 and 2..10 probably won't work in the following example, I have just changed lines to show what I am trying to get. You are not running with strict, warnings which would give you a heads up $mystart = -2; $i = 1; for ($i = 1..10 ) { while ($i = 1 ) { You would get warning if warnings were on. You are assigning 1 to $i and not $i == 1. Better to give your code for the list to review. I pretty much scrapped everything I had and have used the code as posted as a new starting point, therefore that is all I am working with at the moment. if I use strict I get a couple of screenfuls of warnings, mostly Global symbol $xxx requires explicit package name at and a few Bareword warnings, but they seem to be because of strict subs in use If I can answer both you and Jim here... I want to count from 1 to 10, I assigned $i as 1 so as not to need to test $i against negative. However, at the count of 1, I check to see if $mystart is positive or negative. If it is neg, a null value is printed. If it is pos, $mystart is printed in red. From count 2 thru 10... If it is neg, a null value is printed. If it is pos, $mystart is printed in black. I need to retain the value of $i as this block of code will be re-used further into the cgi. regards Brian -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Compiler options perl
On Fri, Apr 17, 2009 at 12:16, Jim Gibson jimsgib...@gmail.com wrote: On 4/16/09 Thu Apr 16, 2009 1:39 PM, edw...@yandex.ru edw...@yandex.ru scribbled: Hello, I would like to know, how to force perl unfold foreach expression during the compilation, i.e. I want next code: snip Can you explain why you want to do this? snip It is called loop unrolling and is a performance hack that many compilers perform when passed a switch to optimize the code it generates. Perl does do some optimization (such as constant folding), but I do not believe you have any control over it and I am nearly 100% certain it does not unroll loops for you. If you have need of that sort of micro-optimization you probably shouldn't be using Perl, or you should be looking into using XS or Inline::C for the high cost loops. snip I suggest you write a program that measures the execution times for the iterative-loop case and the unfolded case and compare them to see if you will gain enough execution time (if that is indeed what you are seeking) to justify the additional memory needed to contain the unwrapped block statements. (Were I to do this, I would write a Perl program to generate the Perl program!) See the Benchmark module. snip Manually unrolling the loop gives you a modest increase in speed for 1,000 items: forloop: 500500 unrolled: 500500 Rate forloop unrolled forloop 4034/s -- -31% unrolled 5852/s 45% -- But almost no benefit for 100,000 items: forloop: 55 unrolled: 55 Rate forloop unrolled forloop 38.6/s -- -1% unrolled 38.9/s 1% -- #!/usr/bin/perl use strict; use warnings; use List::Util qw/sum/; use Benchmark; my $unrolled = 'sub unrolled { my @a; '; for my $i (0 .. 1_000) { $unrolled .= \$a[$i] = $i;; } $unrolled .= 'return \...@a; }; 1'; eval $unrolled or die $@; my %subs = ( forloop = sub { my @a; for my $i (0 .. 1_000) { $a[$i] = $i; } return \...@a; }, unrolled = \unrolled ); for my $sub (sort keys %subs) { print $sub: , sum(@{$subs{$sub}-()}), \n; } Benchmark::cmpthese(-2, \%subs); -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
This is what I'm using upto the code that is giving me a headache. I know it's messy, but I have no training in PERL, I am trying to forward-engineer this cgi by back-engineering from html templates I created and which were chosen using $t-src thanks for any help Brian #! c:\perl\bin\perl.exe -T use warnings; #use strict; use CGI qw/:all/; use CGI::Carp qw/fatalsToBrowser/; use HTMLTMPL; my $t = HTMLTMPL-new(); my $q = new CGI; my $val1 = $q-param('language'); my $val2 = $q-param('year'); #my $submit = $q-param('Submit'); chomp($Lang = $val1); chomp($Year_in = $val2); my ( $day, $mon, $year ) = ( localtime )[ 3, 4, 5 ]; my $today = $year + 1900; set default language if ($Lang eq '' ) {$Lang = en;} handle the strings { $string1 = aaabb; $string2 = cccdd; $string3 = eeeff; $string4 = ggghh; if ($Year_in = 0 ) {$Year_in = $today;} $Year_out = $Year_in; while ($Year_out 100) {$Year_out -= 100;} if (($Year_out 00) ($Year_out = 25)) {$string = $string1;} if (($Year_out 25) ($Year_out = 50)) {$Year_out -= 100;$string = $string2;} if (($Year_out 50) ($Year_out = 75)) {$Year_out -= 200;$string = $string3;} if (($Year_out 75) ($Year_out = 100)) {$Year_out -= 300;$string = $string4;} $Calend = substr $string, $Year_out-1, 1; } user selected language if ($Lang eq en) { ($myjan,$myfeb,$mymar,$myapr,$mymay,$myjun,$myjul,$myaug,$mysep,$myoct,$mynov,$mydec) = (January,February,March,April,May,June,July,August,September,October,November,December); } if ($Lang eq en) { ($mysun_day,$mymon_day,$mytue_day,$mywed_day,$mythu_day,$myfri_day,$mysat_day) = ( Sun,Mon,Tue,Wed,Thu,Fri,Sat); } if ($Lang eq fr) { ($myjan,$myfeb,$mymar,$myapr,$mymay,$myjun,$myjul,$myaug,$mysep,$myoct,$mynov,$mydec) = (Janvier,Fevrier,Mars,Avril,Mai,Juin,Juillet,Août,Septembre,Octobre,Novembre,Decembre); } if ($Lang eq fr) { ($mysun_day,$mymon_day,$mytue_day,$mywed_day,$mythu_day,$myfri_day,$mysat_day) = ( Dim,Lun,Mar,Mer,Jeu,Ven,Sam); } if ($Lang eq de) { ($myjan,$myfeb,$mymar,$myapr,$mymay,$myjun,$myjul,$myaug,$mysep,$myoct,$mynov,$mydec) = (Januar,Februar,März,April,Mai,Juni,Juli,August,September,Oktober,November,Dezember); } if ($Lang eq de) { ($mysun_day,$mymon_day,$mytue_day,$mywed_day,$mythu_day,$myfri_day,$mysat_day) = ( S,M,D,M,D,F,S); } if ($Lang eq ee) { ($myjan,$myfeb,$mymar,$myapr,$mymay,$myjun,$myjul,$myaug,$mysep,$myoct,$mynov,$mydec) = (Jaanuar,Veebruar,Märts,Aprill,Mai,Juuni,Juuli,August,September,Oktoober,November,Detsember); } if ($Lang eq ee) { ($mysun_day,$mymon_day,$mytue_day,$mywed_day,$mythu_day,$myfri_day,$mysat_day) = ( P,E,T,K,N,R,L); } whether or not leapyear { ($myleap = 0 ) } if ($Calend eq b ) {$myleap += 1} if ($Calend eq d ) {$myleap += 1} if ($Calend eq f ) {$myleap += 1} if ($Calend eq h ) {$myleap += 1} whether year starts on a Sunday { ($mystart = 0 ) } if ($Calend eq a || $Calend eq b) {$mystart -= 0} if ($Calend eq c || $Calend eq d) {$mystart -= 1} if ($Calend eq e || $Calend eq f) {$mystart -= 2} if ($Calend eq g || $Calend eq h) {$mystart -= 3} ## this prog originally written to choose a template ## hence the following (superfluous) line #$t-src( c:/apache2/htdocs/calendar/$Lang/$Calend.html ); $t-output( CGI::header ); { print !DOCTYPE html PUBLIC \-\/\/W3C\/\/DTD XHTML 1.0 Transitional\/\/EN\ \http:\/\/www.w3.org\/TR\/xhtml1\/DTD\/xhtml1-transitional.dtd\ html xmlns=\http:\/\/www.w3.org\/1999\/xhtml\ head title$Year_in\/title style type=\text\/css\ !-- #Layer1 { position:absolute; left:39px; top:36px; width:132px; height:167px; z-index:1; } .style1 {color: #00} .style4 {color: #FF3300; } body { background-color: #003399; } .style5 { font-size: 36px; color: #FF; } .style6 {color: #FF} .style7 {color: #FF3300; font-size: 12px; } .style8 {font-size: 12px} .style9 {font-size: 24px} -- \/style \/head body $myjan $Calend $myleap $mystart table width=\10%\ height=\287\ border=\0\ align=\left\ cellpadding=\0\ cellspacing=\0\ tr td align=\left\form action=\http:\/\/localhost\/testing123\/\ method=\get\ enctype=\text\/plain\ name=\year\ id=\year\ p align=\centre\ class=\style1 style6\ label } if
Re: help needed to get over endless loop
Brian wrote: oops, should read.. $Year_out = $Year_in; while ($Year_out 100) {$Year_out -= 100;} if (($Year_out 00) ($Year_out = 25)) {$string = $string1;} if (($Year_out 25) ($Year_out = 50)) {$Year_out -= 25;$string = $string2;} if (($Year_out 50) ($Year_out = 75)) {$Year_out -= 50;$string = $string3;} if (($Year_out 75) ($Year_out = 100)) {$Year_out -= 75;$string = $string4;} -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
Brian, Your while loops aren't actually performing any operation on the variable that is being tested as part of their condition. With while loops, if the condition ($i in this case) is true at the start of the loop, and doesn't change, they will loop forever. They aren't like for loops, where you pre-declare the increment operation that's ran every loop. You need to add some kind of code to increment the $i for each iteration of the loop such that the condition eventually becomes false. I'm not sure exactly what logic you would use to do that, as I'm having a hard time figuring out what these loops are supposed to do. That being said, I think you would realize some benefit by stepping back and looking at the overall design of your program, breaking it down into subroutines and thinking about the clearest way to implement it. I think there are several ways to refactor this to make it much easier to write, debug, and maintain. On Fri, Apr 17, 2009 at 3:46 PM, Brian brian5432...@yahoo.co.uk wrote: This is what I'm using upto the code that is giving me a headache. I know it's messy, but I have no training in PERL, I am trying to forward-engineer this cgi by back-engineering from html templates I created and which were chosen using $t-src thanks for any help Brian #! c:\perl\bin\perl.exe -T use warnings; #use strict; use CGI qw/:all/; use CGI::Carp qw/fatalsToBrowser/; use HTMLTMPL; my $t = HTMLTMPL-new(); my $q = new CGI; my $val1 = $q-param('language'); my $val2 = $q-param('year'); #my $submit = $q-param('Submit'); chomp($Lang = $val1); chomp($Year_in = $val2); my ( $day, $mon, $year ) = ( localtime )[ 3, 4, 5 ]; my $today = $year + 1900; set default language if ($Lang eq '' ) {$Lang = en;} handle the strings { $string1 = aaabb; $string2 = cccdd; $string3 = eeeff; $string4 = ggghh; if ($Year_in = 0 ) {$Year_in = $today;} $Year_out = $Year_in; while ($Year_out 100) {$Year_out -= 100;} if (($Year_out 00) ($Year_out = 25)) {$string = $string1;} if (($Year_out 25) ($Year_out = 50)) {$Year_out -= 100;$string = $string2;} if (($Year_out 50) ($Year_out = 75)) {$Year_out -= 200;$string = $string3;} if (($Year_out 75) ($Year_out = 100)) {$Year_out -= 300;$string = $string4;} $Calend = substr $string, $Year_out-1, 1; } user selected language if ($Lang eq en) { ($myjan,$myfeb,$mymar,$myapr,$mymay,$myjun,$myjul,$myaug,$mysep,$myoct,$mynov,$mydec) = (January,February,March,April,May,June,July,August,September,October,November,December); } if ($Lang eq en) { ($mysun_day,$mymon_day,$mytue_day,$mywed_day,$mythu_day,$myfri_day,$mysat_day) = ( Sun,Mon,Tue,Wed,Thu,Fri,Sat); } if ($Lang eq fr) { ($myjan,$myfeb,$mymar,$myapr,$mymay,$myjun,$myjul,$myaug,$mysep,$myoct,$mynov,$mydec) = (Janvier,Fevrier,Mars,Avril,Mai,Juin,Juillet,Août,Septembre,Octobre,Novembre,Decembre); } if ($Lang eq fr) { ($mysun_day,$mymon_day,$mytue_day,$mywed_day,$mythu_day,$myfri_day,$mysat_day) = ( Dim,Lun,Mar,Mer,Jeu,Ven,Sam); } if ($Lang eq de) { ($myjan,$myfeb,$mymar,$myapr,$mymay,$myjun,$myjul,$myaug,$mysep,$myoct,$mynov,$mydec) = (Januar,Februar,März,April,Mai,Juni,Juli,August,September,Oktober,November,Dezember); } if ($Lang eq de) { ($mysun_day,$mymon_day,$mytue_day,$mywed_day,$mythu_day,$myfri_day,$mysat_day) = ( S,M,D,M,D,F,S); } if ($Lang eq ee) { ($myjan,$myfeb,$mymar,$myapr,$mymay,$myjun,$myjul,$myaug,$mysep,$myoct,$mynov,$mydec) = (Jaanuar,Veebruar,Märts,Aprill,Mai,Juuni,Juuli,August,September,Oktoober,November,Detsember); } if ($Lang eq ee) { ($mysun_day,$mymon_day,$mytue_day,$mywed_day,$mythu_day,$myfri_day,$mysat_day) = ( P,E,T,K,N,R,L); } whether or not leapyear { ($myleap = 0 ) } if ($Calend eq b ) {$myleap += 1} if ($Calend eq d ) {$myleap += 1} if ($Calend eq f ) {$myleap += 1} if ($Calend eq h ) {$myleap += 1} whether year starts on a Sunday { ($mystart = 0 ) } if ($Calend eq a || $Calend eq b) {$mystart -= 0} if ($Calend eq c || $Calend eq d) {$mystart -= 1} if ($Calend eq e || $Calend eq f) {$mystart -= 2} if ($Calend eq g || $Calend eq h) {$mystart -= 3} ## this prog originally written to choose a template ## hence the following (superfluous) line #$t-src( c:/apache2/htdocs/calendar/$Lang/$Calend.html ); $t-output( CGI::header );
Re: Perl Script Error : Can't call method execute_flow without a package or object reference
On 4/16/09 Thu Apr 16, 2009 2:25 PM, Chas. Owens chas.ow...@gmail.com scribbled: 2009/4/16 Jim Gibson jimsgib...@gmail.com: snip This calls the new method in package vpu and assigns the return value, which should be a blessed scalar of some type, usually a reference to a hash, but it could be any scalar. snip $vpu must be a blessed scalar in order to call methods on it. Packages netElement::vpu and vpu are not the same. It is also possible that the new() method has not returned a valid object (blessed scalar) for some reason. You should look at the source code for the new() method and see why that might be. snip Nit picking: you can only bless references, not all scalars. Thanks for the clarification. I believe you can bless any scalar, but the result, the return value from the bless function, is a reference to that scalar. I always use hash references for object instances, but there are some conditions under which using a string or an integer might be advantageous. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
Kevin Ponds wrote: Brian, Your while loops aren't actually performing any operation on the variable I did actually state that the loops wouldn't work. I adjusted them to better show what I want to achieve, not to show how I am trying to achieve it. that is being tested as part of their condition. With while loops, if the condition ($i in this case) is true at the start of the loop, and doesn't change, they will loop forever. They aren't like for loops, where you pre-declare the increment operation that's ran every loop. You need to add some kind of code to increment the $i for each iteration of the loop such that the condition eventually becomes false. I'm not sure exactly what logic you would use to do that, as I'm having a hard time figuring out what these loops are supposed to do. I've tried using while, if and for in different ways, whilst some permutations worked partially, I haven't been able to figure out the correct way of laying the code out to do a couple of basic tests. I'm going back and forth on something that looks to me like it should be so damned simple to figure out. That being said, I think you would realize some benefit by stepping back and looking at the overall design of your program, breaking it down into subroutines and thinking about the clearest way to implement it. I think there are several ways to refactor this to make it much easier to write, debug, and maintain. Yes I agree, but whilst I am at a point where I am having trouble working out one simple step, I really don't think it a good idea to break it into loads of subroutines. I get confused easily, as can be seen by my inability to solve the problem below. On Fri, Apr 17, 2009 at 3:46 PM, Brian brian5432...@yahoo.co.uk wrote: for ( $i =10; $i += 1 ; ) { while ($i == 1 ) { if ($mystart 1 ) { print td width=27 height=27div align=center class=style4/div/td } if ($mystart 0 ) { print td width=27 height=27div align=center class=style4$mystart/div/td } } } while ($i == 2..10 ) { while ($i 1 ) { if ($mystart 1 ) { print td width=27 height=27div align=center/div/td } if ($mystart 0 ) { print td width=27 height=27div align=center$mystart/div/td } } } #} -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Perl Script Error : Can't call method execute_flow without a package or object reference
On Fri, Apr 17, 2009 at 18:01, Jim Gibson jimsgib...@gmail.com wrote: On 4/16/09 Thu Apr 16, 2009 2:25 PM, Chas. Owens chas.ow...@gmail.com scribbled: 2009/4/16 Jim Gibson jimsgib...@gmail.com: snip This calls the new method in package vpu and assigns the return value, which should be a blessed scalar of some type, usually a reference to a hash, but it could be any scalar. snip $vpu must be a blessed scalar in order to call methods on it. Packages netElement::vpu and vpu are not the same. It is also possible that the new() method has not returned a valid object (blessed scalar) for some reason. You should look at the source code for the new() method and see why that might be. snip Nit picking: you can only bless references, not all scalars. Thanks for the clarification. I believe you can bless any scalar, but the result, the return value from the bless function, is a reference to that scalar. snip Why just believe something you can easily test? perl -le 'my $s = foo; my $o = bless $s, Class; print ref $o;' Can't bless non-reference value at -e line 1. snip I always use hash references for object instances, but there are some conditions under which using a string or an integer might be advantageous. snip There are occasionally good reasons to bless other references. For instance Inside Out Objects[1] use a bless'ed scalar reference. Another example would be lexical filehandles, they are bless'ed typeglobs. 1. http://www.stonehenge.com/merlyn/UnixReview/col63.html -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
On 4/17/09 Fri Apr 17, 2009 1:50 PM, Brian brian5432...@yahoo.co.uk scribbled: Brian wrote: oops, should read.. $Year_out = $Year_in; while ($Year_out 100) {$Year_out -= 100;} if (($Year_out 00) ($Year_out = 25)) {$string = $string1;} if (($Year_out 25) ($Year_out = 50)) {$Year_out -= 25;$string = $string2;} if (($Year_out 50) ($Year_out = 75)) {$Year_out -= 50;$string = $string3;} if (($Year_out 75) ($Year_out = 100)) {$Year_out -= 75;$string = $string4;} You are doing quite a few redundant tests here. Note that after your while loop, $Year_out cannot be greater than 100. Also, if $Year_out is greater than 25, it must be greater than 0. In addition, when you are done, $Year_out will be between 0 and 25. Therefore, you can take the value modulo 25 and rearrange the tests to be a little more efficient (I am assuming that $Year_out is always = zero): while ( $Year_out 100 ) { $Year_out -= 100; } if ( $Year_out 75 ) { $string = $string4; }elsif ( $Year_out 50 { $string = $string3; }elsif ( $Year_out 25 ) { $string = $string2; }elsif ( $Year_out 0 ) { $string = $string1; } $Year_out = $Year_out % 25; -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
Jim Gibson wrote: On 4/17/09 Fri Apr 17, 2009 1:50 PM, Brian brian5432...@yahoo.co.uk scribbled: Brian wrote: oops, should read.. $Year_out = $Year_in; while ($Year_out 100) {$Year_out -= 100;} if (($Year_out 00) ($Year_out = 25)) {$string = $string1;} if (($Year_out 25) ($Year_out = 50)) {$Year_out -= 25;$string = $string2;} if (($Year_out 50) ($Year_out = 75)) {$Year_out -= 50;$string = $string3;} if (($Year_out 75) ($Year_out = 100)) {$Year_out -= 75;$string = $string4;} You are doing quite a few redundant tests here. Note that after your while loop, $Year_out cannot be greater than 100. Also, if $Year_out is greater than 25, it must be greater than 0. In addition, when you are done, $Year_out will be between 0 and 25. Therefore, you can take the value modulo 25 and rearrange the tests to be a little more efficient (I am assuming that $Year_out is always = zero): while ( $Year_out 100 ) { $Year_out -= 100; } if ( $Year_out 75 ) { $string = $string4; }elsif ( $Year_out 50 { $string = $string3; }elsif ( $Year_out 25 ) { $string = $string2; }elsif ( $Year_out 0 ) { $string = $string1; } $Year_out = $Year_out % 25; Thanks for that, a better structure, but I don't understand the use of % 25 in this instance. $year is actually always going to be = zero , even if someone enters a negative. I haven't factored for the Julian/Gregorian changeover, so this prog will calc year 1 as it would have been had todays calendar been in use 2000+ years ago. A point I should make here is that I am actually working over 400 year cycles and not 100, so... 100 = 400 75 = 300 50 = 200 25 = 100 but I suppose the results will be this same in your code above. (I changed certain parameters to stop any robots from stealing my code) ;-) -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
Brian wrote: This is what I'm using upto the code that is giving me a headache. I know it's messy, but I have no training in PERL, I am trying to forward-engineer this cgi by back-engineering from html templates I created and which were chosen using $t-src thanks for any help Brian #! c:\perl\bin\perl.exe -T use warnings; #use strict; You should not disable strict, it can help you find mistakes. use CGI qw/:all/; use CGI::Carp qw/fatalsToBrowser/; use HTMLTMPL; my $t = HTMLTMPL-new(); my $q = new CGI; my $val1 = $q-param('language'); my $val2 = $q-param('year'); #my $submit = $q-param('Submit'); chomp($Lang = $val1); chomp($Year_in = $val2); chomp() removes the contents of the $/ variable from the end of the string. What makes you think that $val1 and $val2 need to be chomp()ed? my ( $day, $mon, $year ) = ( localtime )[ 3, 4, 5 ]; my $today = $year + 1900; set default language if ($Lang eq '' ) {$Lang = en;} Strings need to be quoted, either 'en' or en. This is a requirement of most, if not all, programming languages. handle the strings { $string1 = aaabb; $string2 = cccdd; $string3 = eeeff; $string4 = ggghh; Again, you must quote your strings. if ($Year_in = 0 ) {$Year_in = $today;} $Year_out = $Year_in; while ($Year_out 100) {$Year_out -= 100;} No need for a loop ( $Year_out %= 100 ) ||= 100; if (($Year_out 00) ($Year_out = 25)) {$string = $string1;} if (($Year_out 25) ($Year_out = 50)) {$Year_out -= 100;$string = $string2;} if (($Year_out 50) ($Year_out = 75)) {$Year_out -= 200;$string = $string3;} if (($Year_out 75) ($Year_out = 100)) {$Year_out -= 300;$string = $string4;} $Calend = substr $string, $Year_out-1, 1; my @strings = ( ( 'a' ) x 11, ( 'b' ) x 14, ( 'c' ) x 11, ( 'd' ) x 14, ( 'e' ) x 11, ( 'f' ) x 14, ( 'g' ) x 11, ( 'h' ) x 14, ); if ( $Year_out 0 $Year_out = 100 ) { $Calend = $strings[ $Year_out - 1 ]; $Year_out -= int( ( $Year_out - 1 ) / 25 ) * 100; } } user selected language if ($Lang eq en) Again, you must quote your strings. { ($myjan,$myfeb,$mymar,$myapr,$mymay,$myjun,$myjul,$myaug,$mysep,$myoct,$mynov,$mydec) = (January,February,March,April,May,June,July,August,September,October,November,December); } [ *SNIP* ] # $i actually required to be greater than a 10 count, but if I can get # the 2 blocks below to work, I will be able to play about with # the code and increase it to the desired level. # for ( $i =10; $i += 1 ; ) { The for loop synax is: for ( STATEMENT; CONDITIONAL; STATEMENT ) { So $i =10 is superfluous and $i += 1 is always true if $i is true so it will loop until $i becomes 0 which can only happen if $i starts out as a negative number. John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Compiler options perl
17.04.09, 20:16, Jim Gibson jimsgib...@gmail.com: On 4/16/09 Thu Apr 16, 2009 1:39 PM, edw...@yandex.ru edw...@yandex.ru scribbled: Hello, I would like to know, how to force perl unfold foreach expression during the compilation, i.e. I want next code: foreach (1..100) { block } to be compiled like this: block[$_ = 1] block[$_ = 2] block[$_ = 3] . . . block[$_ = 100] Where block[$_ = n] means block with all occurences of $_ substituted with n. Of course, we presume that array like (1..100) is known at compile time. Can you explain why you want to do this? Compiler and run-time options for Perl are given in 'perldoc perlrun'. I don't see anything there that will affect how the Perl compiler optimizes the code it generates or specifically to unfold loops. I suggest you write a program that measures the execution times for the iterative-loop case and the unfolded case and compare them to see if you will gain enough execution time (if that is indeed what you are seeking) to justify the additional memory needed to contain the unwrapped block statements. (Were I to do this, I would write a Perl program to generate the Perl program!) See the Benchmark module. The problem is that I did this benchmarking before. Actually, the code snippets above are run in a very long cycle, and if we have foreach statement, then we need to store some value for loop iteration and even one assignment of this variable consumes time significantly in my case (around several seconds in the whole). So, I ended up with solution with unfolding, but as I have mentioned I do not want to do it manually. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
Brian wrote: This is what I'm using upto the code that is giving me a headache. I know it's messy, but I have no training in PERL, I am trying to forward-engineer this cgi by back-engineering from html templates I created and which were chosen using $t-src [ *SNIP* ] whether or not leapyear { ($myleap = 0 ) } if ($Calend eq b ) {$myleap += 1} if ($Calend eq d ) {$myleap += 1} if ($Calend eq f ) {$myleap += 1} if ($Calend eq h ) {$myleap += 1} The usual way to calculate a leap year is: sub is_leap_year { my $year = shift; return $year % 4 == 0 $year % 100 != 0 || $year % 400 == 0 } whether year starts on a Sunday { ($mystart = 0 ) } if ($Calend eq a || $Calend eq b) {$mystart -= 0} if ($Calend eq c || $Calend eq d) {$mystart -= 1} if ($Calend eq e || $Calend eq f) {$mystart -= 2} if ($Calend eq g || $Calend eq h) {$mystart -= 3} use Time::Local; sub year_starts_sunday { my $year = shift; return !( gmtime timegm 0, 0, 12, 1, 0, $year - 1900 )[ 6 ] } John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
John W. Krahn wrote: Brian wrote: This is what I'm using upto the code that is giving me a headache. I know it's messy, but I have no training in PERL, I am trying to forward-engineer this cgi by back-engineering from html templates I created and which were chosen using $t-src #! c:\perl\bin\perl.exe -T use warnings; #use strict; You should not disable strict, it can help you find mistakes. But it works with strict turned off. chomp($Lang = $val1); chomp($Year_in = $val2); chomp() removes the contents of the $/ variable from the end of the string. What makes you think that $val1 and $val2 need to be chomp()ed? Oops, they are leftovers from when I was using STDIN my ( $day, $mon, $year ) = ( localtime )[ 3, 4, 5 ]; my $today = $year + 1900; set default language if ($Lang eq '' ) {$Lang = en;} Strings need to be quoted, either 'en' or en. This is a requirement of most, if not all, programming languages. handle the strings { $string1 = aaabb; $string2 = cccdd; $string3 = eeeff; $string4 = ggghh; As I wasn't expecting help at this point, I replaced the strings true data. I was of the understanding that PERL could handle a string of any length, when I originally tried to use a string of 400 chars the prog would refuse, so I changed it to 4 strings of 100 chars. I would love to be able to be able to use a single string, but as this method works, I am stuck with it. I still need to use $Calend = substr $string, $Year_out-1, 1; if (($Year_out 00) ($Year_out = 25)) {$string = $string1;} if (($Year_out 25) ($Year_out = 50)) {$Year_out -= 100;$string = $string2;} if (($Year_out 50) ($Year_out = 75)) {$Year_out -= 200;$string = $string3;} if (($Year_out 75) ($Year_out = 100)) {$Year_out -= 300;$string = $string4;} $Calend = substr $string, $Year_out-1, 1; } user selected language if ($Lang eq en) Again, you must quote your strings. { ($myjan,$myfeb,$mymar,$myapr,$mymay,$myjun,$myjul,$myaug,$mysep,$myoct,$mynov,$mydec) = (January,February,March,April,May,June,July,August,September,October,November,December); } [ *SNIP* ] # $i actually required to be greater than a 10 count, but if I can get # the 2 blocks below to work, I will be able to play about with # the code and increase it to the desired level. # for ( $i =10; $i += 1 ; ) { The for loop synax is: for ( STATEMENT; CONDITIONAL; STATEMENT ) { So $i =10 is superfluous and $i += 1 is always true if $i is true so it will loop until $i becomes 0 which can only happen if $i starts out as a negative number. I have 2 books, 1 is the Camel book of PERL, the other is SAMS teach yourself cgi in 24 hours. About £20 each. In SAMS, the only reference to code for counting is as follows $i = 0; while ($i 100) { $i++; } with the following text This loop doesn't actually do anything, it just increments $i every time the loop is executed and exits when $i gets to 99. And the PERL book isn't any more helpful. Teach yourself in 24 hrs? I have spent the last 72 hrs trying to work out how to test to see if $i = 1 and $mystart 1 $i = 1 and $mystart 0 $i = 2..10 and $mystart = 1 Si = 2..10 and $mystart = 0 and print according to the result. and I still can't do it. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
Brian wrote: John W. Krahn wrote: Brian wrote: chomp($Lang = $val1); chomp($Year_in = $val2); chomp() removes the contents of the $/ variable from the end of the string. What makes you think that $val1 and $val2 need to be chomp()ed? Oops, they are leftovers from when I was using STDIN Actually, I have just tried to remove the chomps and the program stops working properly. So it looks like they will have to stay put for a while. :-( -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
John W. Krahn wrote: Brian wrote: This is what I'm using upto the code that is giving me a headache. I know it's messy, but I have no training in PERL, I am trying to forward-engineer this cgi by back-engineering from html templates I created and which were chosen using $t-src [ *SNIP* ] whether or not leapyear { ($myleap = 0 ) } if ($Calend eq b ) {$myleap += 1} if ($Calend eq d ) {$myleap += 1} if ($Calend eq f ) {$myleap += 1} if ($Calend eq h ) {$myleap += 1} The usual way to calculate a leap year is: sub is_leap_year { my $year = shift; return $year % 4 == 0 $year % 100 != 0 || $year % 400 == 0 } I guess that % means 'wholly divisible by' And presumably starts out as a 4 digit number, gets tested and overwritten by 0 or 1, 1 being true? whether year starts on a Sunday { ($mystart = 0 ) } if ($Calend eq a || $Calend eq b) {$mystart -= 0} if ($Calend eq c || $Calend eq d) {$mystart -= 1} if ($Calend eq e || $Calend eq f) {$mystart -= 2} if ($Calend eq g || $Calend eq h) {$mystart -= 3} use Time::Local; sub year_starts_sunday { my $year = shift; return !( gmtime timegm 0, 0, 12, 1, 0, $year - 1900 )[ 6 ] } Oh oh oh, I think you think I am only working with the current year. This is how my live prog works using cgi loading templates http://www.absey-vine.co.uk/calendar/ and this is how my cgi only runs, it only displays 2006 regardless of the year you input. http://www.absey-vine.co.uk/calendar/testing123/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
Brian wrote: John W. Krahn wrote: Brian wrote: This is what I'm using upto the code that is giving me a headache. I know it's messy, but I have no training in PERL, I am trying to forward-engineer this cgi by back-engineering from html templates I created and which were chosen using $t-src [ *SNIP* ] whether or not leapyear { ($myleap = 0 ) } if ($Calend eq b ) {$myleap += 1} if ($Calend eq d ) {$myleap += 1} if ($Calend eq f ) {$myleap += 1} if ($Calend eq h ) {$myleap += 1} The usual way to calculate a leap year is: sub is_leap_year { my $year = shift; return $year % 4 == 0 $year % 100 != 0 || $year % 400 == 0 } I guess that % means 'wholly divisible by' perldoc perlop [ *SNIP* ] Binary % computes the modulus of two numbers. Given integer operands $a and $b: If $b is positive, then $a % $b is $a minus the largest multiple of $b that is not greater than $a. If $b is negative, then $a % $b is $a minus the smallest multiple of $b that is not less than $a (i.e. the result will be less than or equal to zero). Note that when use integer is in scope, % gives you direct access to the modulus operator as implemented by your C compiler. This operator is not as well defined for negative operands, but it will execute faster. And presumably starts out as a 4 digit number, gets tested and overwritten by 0 or 1, 1 being true? What starts out as a 4 digit number? What gets tested? What gets overwritten? You will have to be more specific on what you think is happening for me to either agree with you or correct you. whether year starts on a Sunday { ($mystart = 0 ) } if ($Calend eq a || $Calend eq b) {$mystart -= 0} if ($Calend eq c || $Calend eq d) {$mystart -= 1} if ($Calend eq e || $Calend eq f) {$mystart -= 2} if ($Calend eq g || $Calend eq h) {$mystart -= 3} use Time::Local; sub year_starts_sunday { my $year = shift; return !( gmtime timegm 0, 0, 12, 1, 0, $year - 1900 )[ 6 ] } Oh oh oh, I think you think I am only working with the current year. No, I do not think that. However using timegm() implies a certain upper and lower bound to the range of years that can correctly be calculated. John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Turn off $ anchor greedy behavior
Perl sucks...go Ruby...I did and I am much happier! - Original Message From: Michael Alipio daem0n...@yahoo.com To: Perl Beginners beginners@perl.org; John W. Krahn jwkr...@shaw.ca Sent: Tuesday, April 14, 2009 10:06:39 AM Subject: Re: Turn off $ anchor greedy behavior Aha, found it.. The split returned a list and you've just sliced it. giving [-1] means the list will start running through the elements backwards. --- On Tue, 4/14/09, Michael Alipio daem0n...@yahoo.com wrote: From: Michael Alipio daem0n...@yahoo.com Subject: Re: Turn off $ anchor greedy behavior To: Perl Beginners beginners@perl.org, John W. Krahn jwkr...@shaw.ca Date: Tuesday, April 14, 2009, 10:02 PM Or use split and return the last field: $ perl -le' my $string = boy, pig, 123, 123:412adbd, d0g, lajdlf134_ lkadsf !234,\n; my $value = ( split /,\s+/, $string )[ -1 ]; Another mind bogling example... :-) I thought I would do: my @value = ( split /,\s+/, $string ); print $value[6]; How could your example, have printed the last field using [ -1 ]? Can I also print say, the 3rd field using this trick? print $value; ' lajdlf134_ lkadsf !234 John -- Those people who think they know everything are a great annoyance to those of us who do.-- Isaac Asimov -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Turn off $ anchor greedy behavior
On Fri, Apr 17, 2009 at 21:26, oryann9 orya...@yahoo.com wrote: Perl sucks...go Ruby...I did and I am much happier! snip I looked at Ruby. You couldn't pay me to go back to a language that uses a stupid visual pun for a concatenation operator or forces me to cast variables into different types. 11 + 5 is 16 not a type error. -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
John W. Krahn wrote: Brian wrote: John W. Krahn wrote: Brian wrote: This is what I'm using upto the code that is giving me a headache. I know it's messy, but I have no training in PERL, I am trying to forward-engineer this cgi by back-engineering from html templates I created and which were chosen using $t-src [ *SNIP* ] whether or not leapyear { ($myleap = 0 ) } if ($Calend eq b ) {$myleap += 1} if ($Calend eq d ) {$myleap += 1} if ($Calend eq f ) {$myleap += 1} if ($Calend eq h ) {$myleap += 1} The usual way to calculate a leap year is: sub is_leap_year { my $year = shift; return $year % 4 == 0 $year % 100 != 0 || $year % 400 == 0 } I guess that % means 'wholly divisible by' perldoc perlop [ *SNIP* ] Binary % computes the modulus of two numbers. Given integer operands $a and $b: If $b is positive, then $a % $b is $a minus the largest multiple of $b that is not greater than $a. If $b is negative, then $a % $b is $a minus the smallest multiple of $b that is not less than $a (i.e. the result will be less than or equal to zero). Note that when use integer is in scope, % gives you direct access to the modulus operator as implemented by your C compiler. This operator is not as well defined for negative operands, but it will execute faster. LOL, I didn't understand any of that. :-) Apart from the last sentence ;-) And presumably starts out as a 4 digit number, gets tested and overwritten by 0 or 1, 1 being true? What starts out as a 4 digit number? What gets tested? What gets $year overwritten? You will have to be more specific on what you think is happening for me to either agree with you or correct you. I'm tired, it's about 5 hours past my bedtime. I think I had better go hit the sack. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: help needed to get over endless loop
On Fri, Apr 17, 2009 at 21:35, Brian brian5432...@yahoo.co.uk wrote: snip LOL, I didn't understand any of that. :-) Apart from the last sentence ;-) snip Think of a clock, they hands can go around as many times as you like but they can never point to anything higher than 12. Modulus works the same way, the second value acts as a cap: 0 % 4 = 0 1 % 4 = 1 2 % 4 = 2 3 % 4 = 3 4 % 4 = 0 5 % 4 = 1 6 % 4 = 2 7 % 4 = 3 8 % 4 = 0 It is really the remainder of the division: 7/4 = 1 plus a remainder of 3. -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Shortening the ?: construct
Now that I am learning how to work with complex data structures, I find myself writing things like this a lot: my $foo = ( defined $very_long_expression ? $very_long_expression : n/a ); or my $foo = ( $very_long_expression 0 ? $very_long_expression : 0 ); (Where the long expression typically involves dereferencing hashes of arrays of hashes.) At some point I thought I read about a shorter way to write this, that did not involve repeating the $very_long_expression in the same statement. Perhaps a special variable or something, acting sort of like a pronoun. Anyone know of a shortcut? Thanks, Chap Harrison Perl 5.8.8 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Shortening the ?: construct
2009/4/17 Chap Harrison c...@pobox.com: Now that I am learning how to work with complex data structures, I find myself writing things like this a lot: my $foo = ( defined $very_long_expression ? $very_long_expression : n/a ); or my $foo = ( $very_long_expression 0 ? $very_long_expression : 0 ); (Where the long expression typically involves dereferencing hashes of arrays of hashes.) At some point I thought I read about a shorter way to write this, that did not involve repeating the $very_long_expression in the same statement. Perhaps a special variable or something, acting sort of like a pronoun. Anyone know of a shortcut? snip Why not my $foo = $very_long_expression; $foo = n/a unless defined $foo; or my $foo = $very_long_expression; $foo = 0 unless $foo 0; If you are using Perl 5.10 you can say my $foo = $very_long_expression // n/a; for the first case. -- Chas. Owens wonkden.net The most important skill a programmer can have is the ability to read. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/