Re: add newline
On 4 August 2010 01:59, Rob Dixon rob.di...@gmx.com wrote: Speed of execution is the last goal of all. First of all make your program functional and intelligible. Only after that, if you have problems with resources (including time, disk space, or processor) tune it to be more efficient. This, a thousand times. It's easier to make a correct and readble program fast than it is to make a fast program correct and readable. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: has and non unique keys
On 2 August 2010 12:11, Sharan Basappa sharan.basa...@gmail.com wrote: I am not trying to be critical of perl or anything (if that is what you felt). I didn't think you were; it didn't come across as criticism. I am only trying to see if a certain feature exists or not. The current problem I am working on has duplicate key values and hence the question. The STL multipmap library explanation is below: http://www.cplusplus.com/reference/stl/multimap/ An equivalent perl construct to an STL multimap is a hash of array references: my %hash = ( keyA = [valueA valueC valueF], keyB = [valueB valueH], ); For much more detail, please see the following perldoc pages: perldata perllol perldsc perlref. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Need simple perl script
On 5 June 2010 17:47, Mephistopheles loda.sc...@gmail.com wrote: 1. Run sdiff on file1 and file2--supress identical lines 2. OUtput column1 to outputfile1 and column2 to outputfile2 What have you tried so far? We can't correct your code if you don't give us any code to correct. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Use of uninitialized value in print at...
On 6 June 2010 14:37, Shawn H Corey shawnhco...@gmail.com wrote: On 10-06-06 09:06 AM, Chas. Owens wrote: But that is not the problem; autovivification will create the references: perl -MData::Dumper -le '$c-[0]{a}; print Dumper $c' But that is the problem. Autovivification should not happen for r-values, only l-values. I take it you mean it should only happen when you write to a location ($c-[3] = 'foo') not when you read from a location (print $c-[3]) ? I would think that $c-[0]{a} is an lvalue by most definitions - it's a value which can be written to. It's an lvalue even when on the right side of an assignment. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Undefined subroutine main::
On 24 May 2010 11:49, Weizhong Dai weizhong@gmail.com wrote: Hi guys, I met a problem. When I tried this script below: // sub print_instruction { my ($disk, $start, $end) = @_; print Move disk #$disk from $start to $end.\n; } sub hanoi { my ($n, $start, $end, $extra, $m) = @_; if ($n == 1) { $m-(1, $start, $end); } else { hanoi($n-1, $start, $extra, $end); $m-($n, $start, $end); hanoi($n-1, $extra, $end, $start); } } hanoi(3, 'A', 'C', 'B', \print_instruction); //- I got a error : Undefined subroutine main:: called at this is an example in High Order Perl, so it should not be wrong, I just don't known what is the problem. Plz help me. You didn't use strict. If you turn on use strict, the problem becomes much clearer: Can't use string () as a subroutine ref while strict refs in use at weizhong.pl line 10. Line 10, in my copypasted script, is this: $m-(1, $start, $end); $m is being used as a sub ref but it's complaining that $m is a string. As Rob Coops accurately pointed out, this is because you aren't passing enough arguments in the recursive call: hanoi($n-1, $start, $extra, $end); So I have two questions for you: 1) Do you know about use strict and use warnings? do you aspire to *always* use them? 2) Is this *really* the exact code from HOP? Does HOP make the same mistake? If so, is it documented at the HOP errata page? If not, where did you go wrong in copying from the book? Incidentally, the original error message: Undefined subroutine main:: called at is caused by the same error. Without use strict 'refs', a string can be used as a subref, so for example you can say: my $ref = 'foo'; $ref-(); # same as foo(); $m is uninitialized, and its value is converted to the empty string , and then the subroutine in package main with name is called, i.e. the subroutine main::. This subroutine doesn't exist, which causes the error message seen. If you use warnings as well, you also get the warning Use of uninitialized value in subroutine entry at which warns you that $m is uninitialised. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to arrange a default action in a dispatch table
On 8 May 2010 13:09, Harry Putnam rea...@newsguy.com wrote: Uri Guttman u...@stemsystems.com writes: yes, i disable syntax coloring since it makes it HARDER for me to see [...] I'm terrible about leaving out one of '' () {} [] etc.. I always enter them in pairs. eg I first type if (condition) {} then put the cursor between the curlies and separate them: if (condition) { } then start the block: if (condition) { my $foo; my $bar = baz(); } by always entering delimiters in pairs, I never leave one out. the same approach works for (), [], , '', . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to arrange a default action in a dispatch table
On 8 May 2010 13:35, Harry Putnam rea...@newsguy.com wrote: I'm curious about one thing I see there: `/\A\d+\z/ ' as opposed to `/^\d+$/' in this snippet: if ( $chosen =~ /\A\d+\z/ $chosen = 1 $chosen = @h ) { print Taking some action on $h[$chosen - 1]\n; last; } [...] Is the second formulation (`/^\d+$/' - the one I used) likely to miss under certain conditions? It's all in perldoc perlre. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to arrange a default action in a dispatch table
On 8 May 2010 14:47, Harry Putnam rea...@newsguy.com wrote: Philip Potter philip.g.pot...@gmail.com writes: On 8 May 2010 13:35, Harry Putnam rea...@newsguy.com wrote: I'm curious about one thing I see there: `/\A\d+\z/ ' as opposed to `/^\d+$/' in this snippet: if ( $chosen =~ /\A\d+\z/ $chosen = 1 $chosen = @h ) { print Taking some action on $h[$chosen - 1]\n; last; } [...] Is the second formulation (`/^\d+$/' - the one I used) likely to miss under certain conditions? It's all in perldoc perlre. If you mean this (from perlre): [...] A word boundary (\b) is a spot between two characters that has a \w on one side of it and a \W on the other side of it (in either order), counting the imaginary characters off the beginning and end of the string as matching a \W. (Within character classes \b represents backspace rather than a word boundary, just as it normally does in any double-quoted string.) The \A and \Z are just like ^ and $, except that they won't match multiple times when the /m modifier is used, while ^ and $ will match at every internal line boundary. To match the actual end of the string and not ignore an optional trailing newline, use \z. But there are no trailing new lines.. guaranteed by: chomp(my $chosen = STDIN); You are quite correct - there is no practical difference in this situation. [nitpick: actually, if $/ has been set to something other than \n, $chosen may have a trailing \n character.] Or do you mean its just better practice in general to use \A and \z, just in case. Perl Best Practices, among others, considers \A and \z to be better style than ^ and $ because you are almost always matching on string boundaries rather than line boundaries and you should say what you mean. ^$ can match in midstring under /m and this behaviour is surprising to those who think that ^$ only ever match at string boundaries, rather than line boundaries. As a result, the argument goes, you should always use \A and \z to match on string boundary. Using \z rather than \Z is just about being explicit with what you do to your data. You can't accidentally ignore a trailing \n if you use \z. I'm not 100% convinced by these arguments, but I'm not against them either. It's good to know about \A, \Z and \z just for those situations when you come across code that uses it. But you can always look it up in perlre if you forget. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to arrange a default action in a dispatch table
On 8 May 2010 15:43, Harry Putnam rea...@newsguy.com wrote: Philip Potter philip.g.pot...@gmail.com writes: You are quite correct - there is no practical difference in this situation. [nitpick: actually, if $/ has been set to something other than \n, $chosen may have a trailing \n character.] I was asking, not suggesting one over the other. And was not offering an opinion on it don't now enough. You've trimmed the context I was referring to. But you said: But there are no trailing new lines.. guaranteed by: [example snipped] which looks like a statement and not a question to me. And it is a statement which is correct (subject to above nitpick). You should have more confidence in yourself to read and understand the docs! Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: about dispatch tables
On 5 May 2010 14:36, Harry Putnam rea...@newsguy.com wrote: Uri Guttman u...@stemsystems.com writes: HP The output from the script below: HP Shows 6 elements arrive in dispt($g, @ar) as @_. But when sub N(@_) HP is called, no variables arrive there. @_ is empty. When it seems like HP 5 elements should have arrived there well, it helps if you actually pass in arguments. @_ is NOT a global.FF HP my $code = $dispt{$selection} || $dispt{'error'} ; HP $code-(); you aren't passing anything in to $code. you need to put something in the () which then is set in the @_ of the called sub. As usual, I'm a little confused here. First, what is a `global.FF'? I don't see FF in uri's original post, your reader may have mangled it. He said @_ is NOT a global. And why would it matter that `...@_' is not global when its content was placed into a sub function? Inside dispt {...the sub function @_...} `...@_' is alive and well (I'm changing the name of the hash `%dispt' (inside sub dispt {...})to %hash, it was probably a poor choice of names) --- - ---=--- - #!/blah/blah/perl ## out here in global country �...@_' is unknown dispt($var,@ar); sub dispt { ... ## @_ is alive here containing $var,@ar. %hash = ( print N(@_ # `...@_' is dead here at the N(@_) call) ); ...} which is also inside sub dispt {the sub function}. Where does global come in? The problem is in these lines: sub dispt { # snip my %dispt = ( N = sub { print N(@_). \n; }, # THIS @_ HERE is the problem # snip ); # snip my $code = $dispt{$selection} || $dispt{'error'} ; $code-(); # no arguments passed to $code } The problem is that you have two nested subs. You have sub dispt {} but you also have the anonymous sub { print N(@_).\n; }. @_ is the argument list to the *innermost* sub. So @_ within that anonymous sub is not the argument list to dispt (which is ($var, @ar) ) but the argument list to the call to the anonymous sub, which happens in the line $code-();. There is nothing between the parens, so you pass nothing to the anonymous sub. Therefore, within that anonymous sub, @_ is an empty array. Example with nested subs and arglists: sub foo { print @_; # prints hello sub bar { print @_; # prints howdy. @_ is bar's arglist, not foo's } my $subref = sub { print @_; # prints awooga. @_ is the anonymous sub's arglist, not foo's }; bar('howdy'); $subref-('awooga'); } foo('hello'); Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: about dispatch tables
On 5 May 2010 15:26, Thomas Bätzler t.baetz...@bringe.com wrote: Harry Putnam rea...@newsguy.com asked: [...] which is also inside sub dispt {the sub function}. Where does global come in? Hint: $code-(@_); Or indeed $code; if you want to be cryptic :) Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: about dispatch tables
On 5 May 2010 17:29, Harry Putnam rea...@newsguy.com wrote: Anyway, I understood he was saying NOT global. What I asked is why that would matter. That is, the values or elements in @_ arrive inside the `sub dispt {...}', so should be available to anything inside `sub dispt {...}' right? And `%hash = (...)' is inside `sub dispt {. %hash = (...)..}' I do get confused often about how scope works. Did you read the second part of my message where I addressed this very issue? You haven't quoted it or answered it so I have nothing to say other than to ask you to read it again and tell me what you still find confusing. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Still pondering working with hashs
On 3 May 2010 22:06, Harry Putnam rea...@newsguy.com wrote: Philip Potter philip.g.pot...@gmail.com writes: [...] Both you and Uri are right to a degree. I have to respect Uris' experience, but in fact I have presented goals at every step in this thread. Uri just doesn't want to recognize them. 1) how to find what files are in one tree but not the other I've received a few good techniqes to do that... goal accomplished. 2) ditto but reverse Samething applies. 3) how to find all the matches again, reponses supplied some good code that could be edited to my purposes. 4) How to setup a dispatch table... involved in other threads more than here but still the same project (Again, good solid posts, more than one by Uri himself as I recall - goal accomplished) 5) How to use Data::Dumper for this kind of stuff - goal accomplished Ok yes, that's pretty reasonable; i agree no message has gone wasted. But I'm not sure how much closer you are to solving your problem. If you're trying to identify duplicate files in a filesystem, I would think there's a better way of doing it than what you've got. At first you were only trying to find duplicate filenames, but now you say you care about whether they are actually the same file. But you still haven't explained *why* you are doing this comparison in this thread. [You might have done elsewhere, but I don't read every thread.] It comes down to this: are you happy learning Perl haphazard and piecemeal? Or do you want to learn Perl by actually getting things done in Perl? If you want to actually get something done, you should state what it is. Then we can help you with the design as well as the implementation. I have to support what Uri says here. You shouldn't start writing *any* code at all until you know what you want it to do. If you don't know what you want to do, you're groping in the dark. If you ask for help, people can't give you the help you need because they don't know what you want to do either. I have done that at every step... and people have been thoroughly capable of giving sound help. Uri is talking through his hat. I still don't know what your overarching goal is here. You're looking to find files with the same name but you also care that they are the same file.. but why? What is the overall purpose? Every software engineering method incorporates this principle, usually as a step called requirements analysis. It's a fancy way of saying what do I want to do? That is the kind of `always true' thing one might say. I forgot what the term is but it means its fairly meaningless and mainly sounds good. But none the less true. A tautology? No, it's not. I'm saying you can either do requirements analysis or you can skip it, but if you skip it you haven't got a criterion for success. You won't know what you're trying to do or when you've done it. If you do requirements analysis, you define what you want to do up front. You know what you're trying to do it and you'll know when you've done it. About driving around not knowing where I'm going. I'd wager a full pension check that everyone here has done that and maybe not so long ago. I have done it recently. I didn't enjoy it. I prefer to get things done. [In particular, I'm working with MooseX::Method::Signatures, which is great when it works, but its cryptic error messages can leave you struggling to find the one-liner which fixes your code. I'd rather just find it and fix it than grope around for 10 minutes per bug.] [If you're meaning literal driving, then no, because I haven't got my license yet :P ] Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Still pondering working with hashs
On 4 May 2010 13:45, Bob McConnell r...@cbord.com wrote: From: Uri Guttman HP == Harry Putnam rea...@newsguy.com writes: HP Uri Guttman u...@stemsystems.com writes: nope. been doing this for 35 years and it is solid advice. you can't do a proper program unless you have a proper goal which is what the specification is. HP Some of it looks suspiciously like hair splitting and karping of the HP first order. what you think is hair splitting, we think of as moving mountains. this is what experience in developing projects (big and small) tells us. you came here to learn perl. there is much more to programming than learning a particular language. in fact most programming skills and knowledge is language independent and that is also important to know. This is sounding more and more like an argument between waterfall and agile managers about the best methodology for developing applications. In waterfall you always started with a locked down requirements document, but in agile we never do. The best we can get is the product manager's interpretation of what she heard the client describe. That usually changes as soon as she sees the first prototype. Harry has said he is just beginning to learn the language. As a result, I would expect his short range goals to be adjusted as he learns what is possible and what it takes to accomplish it. That does require some 'driving around' to get an idea of the lay of the land and the paths available to get from here to there. It is also called experimenting with the tool set, or working through the exercises at the end of the chapter. As long as he is learning, what difference does it make what his final destination is. Do any of us know what that will be when we start playing in a new sand box? You have to start with *some* goal. Even in agile, you start with stories to work out in what direction you are headed. You formalise your requirements into tests and then you start coding. Yes, you revise your stories, requirements and tests as you learn more about the design and the technology, but you still need to start from somewhere. Agile is a more nuanced approach to having everything planned out before you begin coding but it's still the same underlying model of [requirements] - [design] - [code], just repeated daily ad nauseum. I do agree that there is a contiuum here, with totally uninformed undirected exploring and experimentation on one end, and totally preplanned careful single-minded coding on the other. Waterfall is totally preplanned, agile is somewhere in the middle. I don't think anyone advocates having *no plan* at all and guessing everywhere. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Still pondering working with hashs
On 4 May 2010 15:19, Harry Putnam rea...@newsguy.com wrote: Philip Potter philip.g.pot...@gmail.com writes: haven't explained *why* you are doing this comparison in this thread. [You might have done elsewhere, but I don't read every thread.] Uri actually did (most of) it for me at one point in a recent post on this thread. Message-ID: 87mxwgbvyo@quad.sysarch.com on gmane.comp.lang.perl.beginners This is probably overly verbose... as I am not very good at explain the full project simply. Project: Merge two directory structures that contain: Many files in one and not the other But different thing have to happen in each structures case. 1) A larger structure has many files not contained in a smaller structure. Those files need to disappear. 2) The smaller structure has many files not present in the larger structure. Those files need to remain. NOTE: Those two chores are in hand... due to excellent examples supplied here. 3) Of the remaining files: Many of them have the same final (end name) but different paths... besides the obvious difference of root path. b1/some/path/file (from larger hierarch) b2/path/file (from smaller hierarch) But also there are many cases where the end name is the same but the files themselves are different. (Those are handled through a dispatch table of functions. One being to check the files size and another supplies command line to diff the files.). In fact usually the size is enough to make a choice. So, if they are the same file then the file from the larger structure will be moved to new base and over the path of the smaller structure.. That sounds too confusing but illustration will show is not really: b1/some/path/file b2/path/file b1 file will be moved to b3/path/file Overwriting the file from b2. The final confusion is that all this is not really being done, but is generating a list of cmds that will do the complete job. What will happen in the end is all of b2 will be left and all identical files from b1 will overwrite there twin by being move to the twins address. Whatever is in b1 that isn't in b2 at all will be deleted. Thank you harry for writing this. Can I ask some questions? You have path P1 and path P2. P1 has more files than P2, but P2 may contain files that P1 doesn't. You want to create P3, which is a merge of P1 and P2. But if a file is in P1 and not P2, that file gets ignored. Doesn't this mean P3 is identical to P2? If two files are identical, you want to copy the P1 version over the P2 version. If the files are identical, what effect will this copy have? In step 1. rm (b1) [ files not in b2] remove from b1 those files which are in b1 but not in b2. ok. step 2) add (b2) [ files not in b1 ] add to b2 those files which are in b2 but not in b1. -- Aren't they already in b2? step 3) mv matching b1 over there twin in b2 move identical files from b1 over their twins in b2. -- If they are identical, why bother moving them? The equivalent cmds will be carried out inside a Distributed Versioning System called `git'. I'm not familiar yet with git. At least not much, but can generate a list of cmds to be carrried out by that program. git merge probably does what you want in one command. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: testing if divisible by?
2010/5/3 Jay Savage daggerqu...@gmail.com: On Sat, May 1, 2010 at 7:45 AM, Philip Potter philip.g.pot...@gmail.com wrote: On 1 May 2010 12:15, Paul opensou...@unixoses.com wrote: Hello all. How can I test to see if a number is divisible by say, 40? Use the modulo operator %. Given integers $x and $y, the expression $x And there's the rub: number ne integer. % is fine if you're only interested in integers, but if you want to compare other numbers use fmod() from POSIX.pm: perl -MPOSIX -wle 'print POSIX::fmod(35, 17.5)' fmod is a fine replacement for % in general for testing remainders of floating-point valued quotients, but using it as a divisibility test requires caution and serious consideration of a different approach. It has the same issues as a floating-point equality test: that is, because floating-point is an inexact representation, the results can depend on whether a value was rounded up or down: D:\perl -MPOSIX -wle print POSIX::fmod(0.2, 0.1) 0 D:\perl -MPOSIX -wle print POSIX::fmod(0.3, 0.1) 0.1 D:\perl -MPOSIX -wle print POSIX::fmod(0.4, 0.1) 0 D:\perl -MPOSIX -wle print POSIX::fmod(0.5, 0.1) 0.1 These examples have strange results because 0.1 is not exactly representable in binary floating-point. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Still pondering working with hashs
On 3 May 2010 20:56, Uri Guttman u...@stemsystems.com wrote: HP == Harry Putnam rea...@newsguy.com writes: HP Uri Guttman u...@stemsystems.com writes: this is what has been bothering me here. you haven't yet spit out a proper problem specification. as i kept saying comparing dir trees is tricky and you kept showing incomplete examples which now all seem to be wrong as you just made a major change in the 'spec'. now duplicate filenames could actually be different files vs just copies in different places. this is a whole other mess of fish. HP None of what I've shown contradicts the full program... quit karping HP about it. Sloppy... yes, I have been. I wonder if that might be HP because I don't really have a good idea of what I'm doing or how to do HP it. What a surprise... that's why its called perl.beginners. [snip] you could easily have written up a short goal document. i have some directory trees which may have duplicate filenames and those files could be the same or actual different files. i want to scan the two trees, compare them and locate common filenames in both. then i want users to be able to choose from menu what to do with the files when dups are found. that is close to what you are doing. was that too hard to write? you are really closing your eyes here and just driving around. sure you have learned some coding skills but you aren't seeing the bigger picture. this will keep biting you until you discover it yourself with more experience. that deep experience is what i am offering here and you are refusing it. note that others aren't refuting what i am saying, they all work with goal documents at some time or other. I have to support what Uri says here. You shouldn't start writing *any* code at all until you know what you want it to do. If you don't know what you want to do, you're groping in the dark. If you ask for help, people can't give you the help you need because they don't know what you want to do either. Every software engineering method incorporates this principle, usually as a step called requirements analysis. It's a fancy way of saying what do I want to do? Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Redirecting the Output and Error of Perl script
On 30 April 2010 18:45, Parag Kalra paragka...@gmail.com wrote: Hey All, I am trying to execute a Perl via shell script. I want to redirect output of Perl script to one file and error occured (if any) to other file. This is the snippet from my shell script: perl output_error.pl 1 Report.log 2Error.log What you have written works for me. I think your error is somewhere else. $ cat foo.pl #!perl print stdout\n; print STDERR stderr\n; $ perl foo.pl 1 out 2 err $ cat out stdout $ cat err stderr Note that appends to the end of a file, while replaces the file. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: testing if divisible by?
On 1 May 2010 12:15, Paul opensou...@unixoses.com wrote: Hello all. How can I test to see if a number is divisible by say, 40? Thanks. Use the modulo operator %. Given integers $x and $y, the expression $x % $y gives the remainder when $x is divided by $y. As a result, if (and only if) $x is exactly divisible by $y, $x % $y is equal to 0. #!perl use 5.010; # for 'say' say 5 % 2; say 6 % 2; say 7 % 2; say 79 % 40; say 80 % 40; say 81 % 40; For more information, see http://perldoc.perl.org/perlop.html#Multiplicative-Operators Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: ExtUtils::MakeMaker, execute Perl code during make test
On 16 April 2010 02:05, Steve Bertrand st...@ibctech.ca wrote: On 2010.04.15 18:50, Steve Bertrand wrote: Hi all, In one of my projects, I've written a test file t/22-upgrade.t. [..snip..] However, when I run make test, the Perl code for print does not execute. Replying my own post, this project is currently only used by us internally. What I've done to 'rectify' the issue so that it is clear that the config files differ, is rename the test to the highest test number (999-upgrade.t) so it will run last, and we will run make test with the TEST_VERBOSE=1 flag. So instead of this: acct-dev: ISP % sudo make test no need for sudo here. You only need it for make install (and not even then if you're using local::lib). I'm still reading through the docs, but if someone has any quick advice on how to make this more granular so I don't have to be extremely verbose on all tests, that'd be great. I'm not entirely sure what you're asking, but if you *only* want to run your new test and you want to get verbose output from it, try: perl Makefile.PL make prove -bv t/22-upgrade.t prove is a utility which comes with Test::Harness, which makes running tests under harness from the command line relatively simple. See http://search.cpan.org/perldoc?prove -b adds blib/lib/ to @INC so the tests can find the module you're testing (you can use -l instead to use lib/) -v produces verbose output Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to create a hash on the fly
On 16 April 2010 03:17, Steve Bertrand st...@ibctech.ca wrote: On 2010.04.15 03:37, raphael() wrote: But as a beginning Perl programmer I find references extremely complicated. Although I have to learn them sometime. Although I Am Not A Programmer, if you do actually desire spending time on writing Perl code, my strong advice would be to ingrain references into your head as soon and as fast as possible. snip Great post, Steve. Does perl-beginners have a website where this could be archived for later reference? Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: ExtUtils::MakeMaker, execute Perl code during make test
On 16 April 2010 13:20, Steve Bertrand st...@ibctech.ca wrote: I use prove often, usually when I want to quickly and non-verbosely (-Q) work with a single test file that I'm currently adding new tests to, or to ensure existing tests still pass if making changes to a function. What I want to be able to do, essentially, is allow Perl code to run within small sections of tests while under make test. In essence, some pseudo code that I'd put into 999-update.t: #test55 perl on print result to STDOUT perl off # back to no printing Tests already *are* perl code, from start to finish. 'print' statements work fine in tests under prove -v. What are you *actually* trying to do? What are you testing and why does this test require you to print output to the user under make test? Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: ExtUtils::MakeMaker, execute Perl code during make test
On 16 April 2010 14:38, Steve Bertrand st...@ibctech.ca wrote: On 2010.04.16 09:15, Philip Potter wrote: What are you *actually* trying to do? What are you testing and why does this test require you to print output to the user under make test? This particular test: - checks to see if an existing installation of the project exists - if yes, it reads both the currently installed config file, and the default config file in the source tree - it compares version numbers - it collects all sections and directives from both files - if versions differ, it informs the user that this is an upgrade - if directives have changed, it informs the user which ones have been added, and which ones have been removed I don't want to merge the configuration files, I just want the user to be informed of what exactly they need to change. 'make test' does not provide the ability to print out individual statements like prove does (from what I can tell). As I said, I think my approach is wrong, and I need to move this code out of the test. 'make test' is for running a suite of tests in an automated fashion, so it's not designed to be easy to produce interactive output. It sounds like your test isn't a test, it's a setup tool. Tests in projdir/t are there to test if the stuff in projdir/lib or projdir/blib works, and shouldn't be dependent on what's already on your system other than ordinary module dependencies. I'm afraid this is beyond my skill level, but I'm sure it's possible to get EU::MM to report to the user when they try to install a new module over an old one. Sorry I can't be more help :( Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: the range of an array
2010/4/13 Xubo qd20080...@hotmail.com: Hi,all: the learning perl say an array can have any number of elements,but when i run such codes: #!perl @abc=1..9; print @abc; it shows range iterator outside integer range at 123 line 2. Regards, George (You should ask your question explicitly. Is your question why doesn't this work??) This is nothing to do with how many elements an array can have, it has to do with what arguments the range operator can take. The range iterator is expecting a fixed-width integer, which means that it probably will only work with numbers up to around 2**31-1. But you probably shouldn't be using ranges that big anyway. Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: the range of an array
2010/4/13 Xubo qd20080...@hotmail.com: thank you for responding to me,but supposing i want to use an array which should includes plenty of elements,what should i do? Do you really need an array with 2,000,000,000 elements? If you use such large arrays, you're probably not using Perl in the best way that you can. You can always write a program to avoid such large arrays. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: the range of an array
2010/4/13 WashingtonGeorge qd20080...@hotmail.com: Sorry,my expressions had something wrong a moment ago.i wanted to say in case i must to use a number bigger than 2**31-1,what should i do? Please quote the message you are replying to. If you have a dataset with more than 2**31-1 elements, you probably shouldn't be storing it in an array. You might want to use a database and only deal with part of the dataset in memory at any one time. To do this you could use MySQL and DBIx::Class as an Object-Relational Mapping (ORM) around the database. But dealing with such huge datasets is *not* a beginner topic. It is difficult and requires knowledge of many different topics. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: free memory
On 13 April 2010 16:25, Patrick Dupre pd...@york.ac.uk wrote: Hello, I have the a hash of hash of ... of array (see below hwo I can list it) whose i wish to free the memory at one point. Right now the desallocation is not clean, ie that evert time that I reallocate the hash, the programme requires more and mor space and finally swap the memory ! How can I free the mempry right. I already tried several trivial thing without success: Thank for your help. foreach my $sig (sort keys %$trans) { foreach my $vt_u (sort keys %{$$trans {$sig}}) { foreach my $Nu (sort keys %{$$trans {$sig}{$vt_u}}) { foreach my $ku (sort keys %{$$trans {$sig}{$vt_u}{$Nu}}) { foreach my $vt_l (sort keys %{$$trans {$sig}{$vt_u}{$Nu}{$ku}}) { foreach my $Nl (sort keys %{$$trans {$sig}{$vt_u}{$Nu}{$ku}{$vt_l}}) { foreach my $kl (sort keys %{$$trans {$sig}{$vt_u}{$Nu}{$ku}{$vt_l}{$Nl}}) { print $sig, $vt_u, $Nu, $ku, $vt_l, $Nl, $kl: , $$trans {$sig}{$vt_u}{$Nu}{$ku}{$vt_l}{$Nl}{$kl} [0], , $$trans {$sig}{$vt_u}{$Nu}{$ku}{$vt_l}{$Nl}{$kl} [1], \n ; } } } } } } } There is no good solution other than not allocating so much memory in the first place. See perldoc -q free an array. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Subroutine doesn't write to file
On 12 April 2010 04:31, Uri Guttman u...@stemsystems.com wrote: AM == Abimael Martinez abijr@gmail.com writes: AM print {$tmp} $div_start; no need for the {} around a single scalar handle. But it *does* comply with Perl Best Practices #136. * It makes the filehandle obviously different from the other arguments * It makes you less likely to accidentally put a wrong comma in: print {$tmp}, $div_start; * If you forget a comma on a print statement where you just meant to print to STDOUT, it stands out as being wrong: print $arg1 $arg2; # should have been print $arg1, $arg2; or print {$arg1} $arg2 * IOW, it forces you to be more explicit about when you are printing to a fh and when you are printing to STDOUT. So there's at least 2 other people out there (Damian and me) who think the OP's version is reasonable. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Subroutine doesn't write to file
On 12 April 2010 07:55, Uri Guttman u...@stemsystems.com wrote: PP == Philip Potter philip.g.pot...@gmail.com writes: PP On 12 April 2010 04:31, Uri Guttman u...@stemsystems.com wrote: AM == Abimael Martinez abijr@gmail.com writes: AM print {$tmp} $div_start; no need for the {} around a single scalar handle. PP But it *does* comply with Perl Best Practices #136. PP * It makes the filehandle obviously different from the other arguments PP * It makes you less likely to accidentally put a wrong comma in: PP print {$tmp}, $div_start; PP * If you forget a comma on a print statement where you just meant to PP print to STDOUT, it stands out as being wrong: PP print $arg1 $arg2; # should have been print $arg1, $arg2; or print PP {$arg1} $arg2 PP * IOW, it forces you to be more explicit about when you are printing PP to a fh and when you are printing to STDOUT. PP So there's at least 2 other people out there (Damian and me) who think PP the OP's version is reasonable. and did you see my much shorter answer with no file handles at all?? How is that relevant to my point? and PBP isn't always right. it is meant as a list of interesting suggestions to pick and choose from. read the intro to get that. also note that i was one of the tech editors of that book. i know it well. and i disagree with some of it too. damian is fine with that. :) Where did I say PBP was always right? I just didn't want to let your style argument be the only one in this thread, since there are clearly people who disagree with you, too. TIMTOWTDI. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Subroutine doesn't write to file
On 12 April 2010 11:34, Shlomi Fish shlo...@iglu.org.il wrote: Hi Uri and Philip (and Abimael), On Monday 12 Apr 2010 10:32:36 Uri Guttman wrote: PP Where did I say PBP was always right? I just didn't want to let your PP style argument be the only one in this thread, since there are clearly PP people who disagree with you, too. TIMTOWTDI. i disagree with myself most of the time! i am my own stupid boss and stupid worker. try that on for size! :) Can we please have a civil discussion without insulting the other party? For I thought Uri and I were criticising each other's ideas, not throwing insults. I certainly don't feel insulted by Uri. Can you identify any insults so that we can be sure not to say them again? Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Subroutine doesn't write to file
On 12 April 2010 16:39, Uri Guttman u...@stemsystems.com wrote: PP == Philip Potter philip.g.pot...@gmail.com writes: PP On 12 April 2010 11:34, Shlomi Fish shlo...@iglu.org.il wrote: Hi Uri and Philip (and Abimael), On Monday 12 Apr 2010 10:32:36 Uri Guttman wrote: PP Where did I say PBP was always right? I just didn't want to let your PP style argument be the only one in this thread, since there are clearly PP people who disagree with you, too. TIMTOWTDI. i disagree with myself most of the time! i am my own stupid boss and stupid worker. try that on for size! :) Can we please have a civil discussion without insulting the other party? For PP I thought Uri and I were criticising each other's ideas, not throwing PP insults. I certainly don't feel insulted by Uri. Can you identify any PP insults so that we can be sure not to say them again? i agree. at least i never aimed anything directly at philip. we were disagreeing about file handle stuff and how to use PBP and such. Actually, I don't disagree at all on how to use PBP. It's a guide; it's a template for starting a coding standard; it's not a cast-iron set of rules. Nobody should follow everything in PBP, even (especially?) Damian doesn't do that, and nobody should follow anything in PBP without understanding *why* those best practices are considered so. print {$fh} $output; is considered by some to be good style. I brought up PBP to show that Damian Conway thinks that it's good style (or at least he did when he wrote PBP), and to state some of the reasons he holds this view. I don't think this is a controversial claim. Given that Uri dislikes print {$fh} and Damian likes print {$fh} it's clear this is a bikeshed argument. The OP should feel free to make his own decision (or should conform to whichever style is imposed by his team's policy). Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Looping in Test::Harness
On 9 April 2010 18:04, Bob McConnell r...@cbord.com wrote: After a great deal of reading and speculation, it looks like YAML is the way to go. I will have to move the loop inside each test script, and iterate thorough the records read. The biggest problem is that the plan is no longer fixed, so I do lose some auditability, but I don't see a cleaner way to do this. Assuming that you're using Test::More, you don't have to know your plan when you 'use Test::More'; you can create your plan from calculated values later on, provided it's before you start testing: use Test::More; # Read config file and count testcases... plan tests = tests_per_testcase * $num_tests; # Start tests here Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Decimal representation of binary file (unpack)
On 8 April 2010 06:30, Raymond Wan r@aist.go.jp wrote: Hi all, I would like to read in a binary file and extract the 4-byte ints from it. Under Linux, something like od -t uI. I got it working as follows: my $buffer = STDIN; my @buffer = split //, $buffer; for (my $i = 0; $i length ($buffer); $i += 4) { print unpack ('I', $buffer[$i].$buffer[$i + 1].$buffer[$i + 2].$buffer[$i + 3]), \n; } Are you sure you want to read using the line input operator? Consider the file (as a hexdump): 000a There are 8 bytes, corresponding to 2 4-byte ints. The two 4-byte ints have values 0 and 10*256*256 = 655360 (assuming big endian). But if 0a happens to be the value of a newline char (as it is under ascii and utf-8) and you read line-by-line you get two lines: \0\0\0\0\0 and \0\0 which your program will interpret as three ints of values (0,0,0), and not 2 ints with values (0,655360). Use 'read' instead (perldoc -f read) to read binary data. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: AW: about the various styles of `open'
On 6 April 2010 16:48, Harry Putnam rea...@newsguy.com wrote: Thanks for the effort, but I'm still a bit confused. Just need to think it over some more maybe. Is it fair to say that the `magic' open is far and away the most common working case? And that the 3 arg open is for unusual circumstances? No, generally you should always use 3 arg open. Perl Best Practices item #128 discusses this issue if you can beg borrow or steal a copy. Shawn H Corey shawnhco...@gmail.com writes: Harry Putnam wrote: open my $fh, $file or die could not open $file: $!\n; What if the user gave this as $file? rm -fr ~ Not to be argumentative here... but maybe I can't see as quickly as some what this would do. I can't really visualize what would happen there... wouldn't the open just fail? Further do we need to prepare for a vastly ridiculous file name? perhaps spelling things out more explicitly would help. If you write open my $fh, $file or die could not open $file: $!; and $file is a user-supplied option, then the user can effectively make arbitrary magic open calls such as: open my $fh, 'rm -rf ~ |' or die could not open rm -rf ~ |: $!; The | character at the end means that this no longer opens a file, it runs a command. One which will delete your home directory on unixlike systems. And the user could specify any command they like -- perhaps one which emails your personal files to evilhac...@example.com. The three argument form: open my $fh, '', 'rm -rf ~ |' or die could not open rm -rf ~ |: $!; doesn't have this problem. It will try to open a file with quite a funny name, but because the mode is chosen my the second argument and not by a user-supplied string, the user can't execute arbitrary code. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Looping in Test::Harness
On 6 April 2010 16:52, Bob McConnell r...@cbord.com wrote: I have a test harness set up with a series of Selenium test scripts. Each script tests a specific scenario on my web site. But I have some scenarios that I want to test multiple times with different data entered each time. Currently I am using environment parameters to pass the test data into each script. But that doesn't work for multiple passes, since I have no way to save the base data and restore it after modifying it. [snip] Is there a cleaner way to pass those variables into each test script? How can I make this loop through some tests multiple times with different sets of data each time? Hi Bob, I suppose you could group your variables by testcase and put them into a configuration file (say testcases.yml) and get each test to read in the configuration using Config::Any. With Config::Any you can use most config file formats: XML, YAML, JSON, .ini, etc. Also you can use prove.exe to run tests under harness rather than using your perl one-liner. http://search.cpan.org/perldoc?prove for details though I normally just do prove -bv t/*.t. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: about beginner perl site and developing skill
On 6 April 2010 17:21, Rob Coops rco...@gmail.com wrote: On Tue, Apr 6, 2010 at 5:56 PM, Harry Putnam rea...@newsguy.com wrote: This doesn't actually have to be a paying job... I'm retired and do have an income. But it would need to be a situation where I was expected to produce something on a continuing basis. (Of course the prospect of pay, somewhere along the line would be a further benefit). I'm thinking some kind of open source project that could make use of someone with only light weight skills starting out. And when I say light weight... I really mean it. I didn't graduate high school, and never went beyond that in formal education. Anything to do with programming is strictly self taught and therefore has gaping holes in it. On the other hand, I am capable of writing semi complex programs and have written dozens, probably over 100 by now, of scripts for my own use. So, cutting to the chase, where does someone look for that kind of opening? I would say have a poke around on Sourceforge (http://www.sourgeforge.com/) or on Google code (http://code.google.com) and see what you can find there. There are hundreds of projects out there that require anything from a very skilled programmer to someone that is able to do basic coding and have some knowledge of perl. The most important thing at least for me is to pick a project that you would use your self something that really makes you feel like your contributions are worth while. If you can use it and see the benefit form using it then it is not unlikely someone else will enjoy your work as well. I my self have learned PHP in that way, by simply getting my hands dirty and starting to resolve tickets and feature requests for an open source project. In the end it even ended up getting me some money when people where asking me to do custom development for them. And the best thing of all I used most of the code I wrote my self as well on my own site so most of my effort benefited not just others but myself as well. I would add that an important thing to getting involved in an open source project is to communicate with the people who work on it. Find out where they hang out - mailing lists, IRC, forums - and talk to them. Tell them your skill level and ask how you can help (although don't feel you have to accept their answer -- dive into the source and fiddle with whatever takes your fancy). You will need to learn about VCS if you haven't already. The two big ones are git and svn but generally you'll want to learn the one that your chosen project uses. In order to contribute to the project, you need to be working on the latest version so get the code from the VCS repository rather than downloading a tarball or from CPAN. If they have a bug tracker (which they should) have a look through it and try finding out if you can find and fix the bugs. Alternatively, you can start by identifying bugs and filing them in the tracker - this is valuable programming work, even if you're not writing code. It helps if it's a project for a tool which you will use yourself fairly regularly. I am pretty new to open source projects myself, but I've started doing a tiny bit of work on Padre, the Perl IDE. Because I use Padre to write Perl, when I see something that doesn't work, I'm motivated to fix it. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: free memory
On 1 April 2010 16:13, Patrick Dupre pd...@york.ac.uk wrote: Hello, I cannot subscribe to perl-xs so, I am still posting here. I don't understand the problem. Firstly, I just emailed perl-xs-subscr...@perl.org and got a response from the mailing list software. Secondly, how come you were posting on perl-xs yesterday but can't subscribe today? http://www.nntp.perl.org/group/perl.xs/2010/03/msg2562.html Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: good books for novice perl beginners
On 24 March 2010 18:16, NAKO sung...@yahoo.com wrote: Hello All, Can you please recommend some good books for novice beginners? Thanks http://learn.perl.org/books.html is a good start. The rest of the http://learn.perl.org/ website is worth a look too, which is why it's linked in the footers of this mailing list. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Getting garbage fast
On 24 March 2010 00:56, Eric Veith1 eric.ve...@de.ibm.com wrote: Sam p...@net153.net wrote on 03/23/2010 11:18:11 PM: Could you use a file of random data? You can create one of those really easy: dd if=/dev/urandom of=ranfile bs= Theoretically, yes, of course I could just try to create an arbitrary sized file from /dev/urandom via dd. I hoped there would be an equally both fast and elegant solution possible as with the C approach (malloc without init). Bob's idea of just reading and piping files from /bin or /usr/bin was also a good idea. Well, if there's no... more elegant solution, I'll happily go with what has been offered. (Note the around more elegant!) I would say both of the offered solutions are more elegant than using uninitialised malloc()ed memory. You can't rely on malloc to offer you known data, but you *also* can't rely on it to give you sufficiently random data. The /bin option also has the advantage (or disadvantage, depending on application) of being deterministic. I note that a quick search on CPAN for urandom turns up String::Urandom as a more Perlish way to access /dev/urandom. I'm not sure how much it gains you: given that you still depend on /dev/urandom being present, you might as well make it explicit. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: quick question
On 19 March 2010 11:45, trapd...@trapd00r.se wrote: On 19/03/10 13:19 +0200, Chris Knipe wrote: my ($foo, $bar) = 1 I am getting more and more occurances where when I use the later as above, $bar would not have a defined value... I'm not quite sure I understand why. Does; my ($foo,$bar) = 1 x 2; do what you want? Probably not: $ perl foo.pl 11 $ cat foo.pl my ($foo,$bar) = 1 x 2; print $foo\n; print $bar\n; You've concatenated two strings containing '1' into a single scalar '11' and assigned it to $foo. You probably meant (1) x 2, and even that is not terribly readable or maintainable. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Any Good SCM tool to manage Perl Code locally
On 16 March 2010 06:44, Parag Kalra paragka...@gmail.com wrote: Yes I have started using Git and I am very happy with it so far. Just couple of questions - How can I make my code readonly using Git such that it can be edited only when it is checked out. I don't understand the question. If you have source files, they are necessarily part of the working copy and therefore they are checked out. In git your working copy and your copy of the repository do have a cosy relationship and live very near each other; but they are still different things. The source files are part of the working copy. The repository is not stored as individual source files -- it is stored in hidden directories called .git/ and contains the database of all revisions. Since source files are always checked out, they could never be readonly by your criteria. Also if I want to take entire code base to particular revision, I guess I need to use - 'git checkout commit_name'. So is commit name something like - '11666c32ad1008a88a603d7ebc5cea144495789e' Yes that's right, but for convenience you can use a minimal unique prefix rather than the whole checksum, so 11666c32 would probably work in this instance. Normally however you would create a branch so you can refer to it by name -- and catch further updates to that branch pulled in from elsewhere -- rather than referring to a particular commit. Then you just do 'git checkout branchname'. I suggest further questions are taken to the git mailing list: http://dir.gmane.org/gmane.comp.version-control.git or to #git on irc.freenode.net Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Angle bracket operator with list
I found this code example on Stack Overflow to prettyprint a hash: (link: http://stackoverflow.com/questions/2431032/how-to-print-a-key-value-using-the-perl-hases/2431139#2431139 ) use strict; use warnings; my %hash = (2009 = 9, 2010 = 10); print join ( = , each %hash), \n while keys %hash; My question is: what is the angle bracket operator doing? I know its use as FILE, $file, and *.glob, but I've never seen it used like this, and I can't see anything in perlop#I/O Operators which would help. This code works on perl 5.8.8 so it's been a feature for some time, so it would surprise me if it's not documented anywhere, but I just can't find it. Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Angle bracket operator with list
On 12 March 2010 09:17, Uri Guttman u...@stemsystems.com wrote: PP == Philip Potter philip.g.pot...@gmail.com writes: PP I found this code example on Stack Overflow to prettyprint a hash: PP (link: PP http://stackoverflow.com/questions/2431032/how-to-print-a-key-value-using-the-perl-hases/2431139#2431139 PP ) you shouldn't be learning perl from that site! I respectfully disagree. PP my %hash = (2009 = 9, 2010 = 10); see what happens when you have 3 or more pairs in that hash! PP print join ( = , each %hash), \n while keys %hash; try doing a perl one liner with -MO=Deparse and see what perl parses that as. Great idea, although I don't see the need to do it as a one liner: perl -MO=Deparse foo.pl use warnings; use strict 'refs'; my(%hash) = (2009, 9, 2010, 10); use File::Glob (); print join(' = ', each %hash), \n while defined($_ = glob('keys %hash')); foo.pl syntax OK perl -E 'say while keys %hash' keys %hash So keys %hash is being treated as a string, not an expression, the keys function never gets called, and the loop executes twice, once with $_ = 'keys' and once with $_ = '%hash'. Truly horrible! Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Mass value assigment
On 9 March 2010 11:49, Shlomi Fish shlo...@iglu.org.il wrote: Otherwise you can assign several variables the same initial value using the x operator: my ($x, $y, $z) = ((test) x 3); Note that both pairs of parentheses on the right are required. Are the outer pair really required? I would have thought that assigning to a list puts the RHS in list context anyway without requiring parenthesis to force it, and testing seems to confirm this. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Perl Hiding Username/Password
On 10 February 2010 23:04, newbie01 perl newbie01.p...@gmail.com wrote: Hi all, Can anyone please advise how I can change the following codes to work where the username and correct_pass are not exposed? Script is ran via crontab and can also be run manually, at the moment am reading these values from some sort of delimited file. The worry is someone getting access to the script and then putting in some print commands to expose the username and password information. Just thinking in advance before it happen. The original script is a UNIX script but I thought there may be a Perl module that will masked the password where there is none of the same thing for UNIX scripts. #!/usr/bin/perl use DBI; .. .. $dbh = DBI-connect('dbi:Oracle:host=localhost;sid=test;port=1521', 'username', 'correct_pass'); my $sth = $dbh-prepare(alter session set nls_date_format = 'DD-MON- HH24:MI:SS'); $sth-execute(); my $sth = $dbh-prepare(select 'Today is ' || sysdate from dual); $sth-execute(); while (my ($sysdate) = $sth-fetchrow_array()) { print $sysdate, \n; } $sth-finish(); exit 0; Any feedback will be very much appreciated. Thanks in advance What is your threat model? ie what kind of attacker are you trying to protect yourself from? You can prevent casual attacks by following some of the suggestions in perldoc -q hide the source. There is no way to do what you ask in such a way that a determined attacker will not be able to get your password. If this is a problem, you need to redesign your system. If you want to make sure the only way a user can access the database is through your perl script, you'll need to do something to enforce that, such as storing the script on a different server and giving it a web interface, and making the database invisible to everything but the server the script is hosted on. [This might work but it's not necessarily a good idea.] Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Clone an object
2009/12/28 Christoph Friedrich christ...@christophfriedrich.de: I have searched the internet but didn't found a good answer. How do I clone a Perl Object? For example like this: $a = My::Object-new(); $b = $a-clone(); Where $b is an exact copy of $a but in its own memory. You call the function that the object provides for this purpose, if any. The ability to clone an object is not special in Perl; it is treated just like any other method. Perl doesn't provide a clone method for you automatically; if you want one in your own objects, you have to write it yourself. If you want to clone an object from a library provided by someone else, you have to read its documentation and find a clone object. Not all functions provide a clone function; it may not be possible or desirable to clone a given object. Consider for example an object which owns a network socket: if you were able to clone the object, you could create two different objects which own the same socket; this is a Bad Thing. Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Clone an object
2009/12/28 Philip Potter philip.g.pot...@gmail.com: If you want to clone an object from a library provided by someone else, you have to read its documentation and find a clone object. This should be a clone method, of course. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: regex question
2009/12/22 Parag Kalra paragka...@gmail.com: You can try following: $_ =~ s/^\.(\s)+//g; This isn't quite right. There are two ways in which you might use this substitution: either $_ will contain a single line, or it will contain multiple lines. The single line case might look something like this: while () { s/^\.(\s)+//g; print; } In this case, however, the /g modifier is redundant and confusing, since you only want the regex to match once at the start of each line. Take the /g modifier off. The multiple line case might look like this: $_ = do {local $/; ;}; # but better to use File::Slurp s/^\.(\s)+//g; print; This code is broken. The problem is that ^ matches start-of-string, not start-of-line. Therefore, even with the /g modifier the regex can only match at the beginning of the string, and so will only match once. To change ^ to match start-of-line, you need the /m modifier: s/^\.(\s)+//gm; print; Perl Best Practices makes the interesting recommendation that all regexes should use the /m modifier, because having ^$ match line boundaries seems to be what most programmers expect and seems to be useful much more often. If you still need to match start and end of string you can use \A and \z. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Git Talk by Randal
2009/12/19 Parag Kalra paragka...@gmail.com: Hello All, I know most of the big guns on this mailing list may already know this but still felt like sharing this with you all specially for the beginers like me. :) A nice, very informative and well presented talk on Git given by our own old gold Randal :) http://www.youtube.com/watch?v=8dhZ9BXQgc4 The one by Linus is also pretty good: http://www.youtube.com/watch?v=4XpnKHJAok8 Linus's talk is more of a why, while Randal's is more of a how. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Git Talk by Randal
2009/12/19 Erez Schatz moonb...@gmail.com: One problem I have with Merlyn, is when he starts talking about something, you're willing to throw everything away and go where he points. He's such a passionate, compelling speaker. My other problem is that he's mostly right. Who are you talking about? Is Merlyn some nickname I'm not aware of? -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: capture error - the better way?
2009/12/18 Peter Scott pe...@psdt.com: On Wed, 16 Dec 2009 10:09:38 +, Philip Potter wrote: Yes, it can't fold the constant. That's no excuse for changing the behaviour of the program. What it should do is what I wrote in my previous email -- replace it with code that raises a runtime exception when executed -- ie a die Illegal division by zero. This is Perl. The distinction between the compilation and run time phases is almost totally academic. (For instance, use strict throws some exceptions at compile time and others at run time.) Since there's no compiled executable produced, the only difference in behavior is that your program dies sooner than it would have. Of course, conceivably it would not have died at all if the constant folding occurred in conditionally unexecuted code. So think of this as compile-time validation made possible by the use of constants: if your program *were* to execute that code, it would die. Better to know about that in advance than 49 hours into a 50 hour duty cycle. I don't disagree with most of what you said. Yes, runtime and compile time are not as clear as in a language like C. Yes, it's best to know as soon as possible that a program has a potential problem. But my issue is that here, no such problem exists, and yet perl is still complaining about it. Recall that the example here was a runtime error causing an exception to be thrown _from within an eval {} block_. If the program were to execute that code, it would not die, as you claim. It would instead throw an exception which would be caught by the eval {} block, recover, and continue. But by moving the runtime error to compile time, and outside of the systems that you've set up to deal with the error, perl is saying I think this code is broken when in fact it isn't. perl has changed the behaviour of the problem. I wouldn't mind if perl just issued a warning. Having a constant expression 1/0 in code is probably a Bad Thing which the programmer should be warned of. But since it's still valid code with a predictable outcome, it should do what it's told, not try to second guess what you wanted in some sort of DWIMery. Thankfully, the people who wrote perl 5.10 seem to agree with me. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: capture error - the better way?
2009/12/16 Shlomi Fish shlo...@iglu.org.il: On Tuesday 15 Dec 2009 17:14:25 Philip Potter wrote: If evaluating a constant expression results in a runtime exception, that runtime exception must happen at runtime, and not at compile time. In general, it is the duty of an optimizer never to change program behaviour, only performance. But this is a case where the compiler evaluates a constant expression and it results in an exception. So it cannot store it as a folded constant on the side for the run-time place. As a result, it throws a compile-time error. Yes, it can't fold the constant. That's no excuse for changing the behaviour of the program. What it should do is what I wrote in my previous email -- replace it with code that raises a runtime exception when executed -- ie a die Illegal division by zero. Therefore an optimizer which tries to fold a constant expression at compile time only to find a divide by zero exception should write code which raises a divide by zero exception at runtime. Raising a divide by zero exception at compile time is not what the original program did, and so it's not what the optimizer should do either. Why? Uhm, because it changes the behaviour of the program? I don't see what else I can say. If the optimizer changes program behaviour then you have no guarantee that what you wrote is what will happen. If this error had happened at runtime, the program would have caught it and functioned entirely correctly; but by moving the exception to compile time -- and out of the eval block which was supposed to catch any exceptions -- the program won't even compile. Well, Perl's optimizer is not that smart. It does not inline such functions because it doesn't know how many places they will get called. So you're spared in this case. I don't think you understand my point. The point is: even if it could, it shouldn't. Once you allow optimizers to change program behaviour, you have no guarantee that what you write is what will happen. That is why optimizers must *never* change program behaviour. If I am supposed to defend against such an overzealous optimizer, how would I write code that uses any block eval to catch exceptions? A sufficiently advanced optimizer could always use some method like that described above to precalculate the results of the eval, triggering an exception at compile time instead of runtime. Therefore block eval could never be guaranteed to catch runtime exceptions! Well, a program can always have user-input (from files, STDIN, @ARGV, etc.) which no compiler or optimiser can predict. Sometimes this input will trigger certain exceptions which cannot be folded. Sometimes the optimizer will be unable to break my code? That is no reassurance. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: parsing template
2009/12/16 Avani avani.v.puj...@gmail.com: Hi all, I am very new to perl. I have to parse template in perl. do anybody have any reference code to do it? currently I am using regular expression. Right now, i m only able to replace variables. HOw to do it for for loop and if conditions ? You may have to give more details, because I'm not entirely sure what you want. What does parse template mean? What is your input? What is your output? Can you give real examples of what you would like your program to do? If you have a file with a regular structure which you would like to parse according to some kind of template, take a look at Template::Extract in CPAN. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: being smart about script structure
2009/12/16 Bryan R Harris bryan_r_har...@raytheon.com: What's the difference between pointers and references? Where can I read about that difference? The key difference in my mind is this: Perl references are defined in terms of perl datatypes. C pointers are defined (more or less) in terms of memory locations. If you think about Perl references in terms of *what they do* and not *how they work*, it becomes much clearer. There is nothing you can do to a perl reference to get its memory address, so that doesn't fit within the conceptual framework of Perl references. Perl references are simply scalar values which provide an indirect form of access to other perl objects (including scalars, hashes, OO types...). You can create a reference from a named or anonymous variable, and you can dereference that reference to get the original object. You can think of them as labels or tags or handles; all of these ideas fit the paradigm created by the interface of the reference. C pointers, however, let you do so much more, most of it unsafe. They let you add arbitrary integers to pointers; this may well end up with an invalid pointer value. They let you subtract one pointer from another to find out the distance in memory locations between object A and object B. The interface suggests that a pointer is a memory location value, much more than a label on a variable. Note that this thinking in terms of what it does applies to other datatypes. For example: the CGI module is a module which helps write CGI scripts. It emits HTTP headers and lets you write HTML in a Perl-like way. As a user of this module, you don't start with implementation details such as The CGI module stores state about the HTTP connection to determine which MIME types the client can read. To parse this Content-Accept: header, it uses the following regexes... Instead, you talk about what it does: $q = CGI-new;# create new CGI object print $q-header,# create the HTTP header $q-start_html('hello world'), # start the HTML $q-h1('hello world'), # level 1 header $q-end_html; # end the HTML The same thinking applies to references: forget about what they are under the hood. Think only in terms of what they do. If the data structures being referenced (?) by those references are created within the subroutine with my, are they still alive and accessible outside the subroutine? Or do they go away after you return? They're still alive. Perl objects are not harvested by the GC until there are no more references to them. So this is perfectly safe: sub anon_scalar { my $x; return \$x} my $ref = anon_scalar; $$ref = 3; Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: capture error - the better way?
2009/12/15 Xiao Lan (小兰) practicalp...@gmail.com: On Tue, Dec 15, 2009 at 6:34 PM, Shlomi Fish shlo...@iglu.org.il wrote: You can use block eval {} instead of string eval : I did have tried that, but this will get a runtime error. # perl -e ' eval { $x = 12/0 }; if ($@) { print 0 div error }' Illegal division by zero at -e line 2. [OT: Are you running this as root? You should avoid being root whenever you can.] That's odd. It works on my machine: $ perl -le ' eval { $x = 12/0 }; if ($@) { print 0 div error }' 0 div error $ perl -v This is perl, v5.10.0 built for x86_64-linux-gnu-thread-multi I can only guess that perl5.8.8 is not catching exceptions in constants caused by evaluating compile time constants. String eval delays evaluation to runtime which would stop this bug manifesting; but this feature is exactly the reason why string eval is /bad/. Does this work for you? $ perl -le 'eval { die died; }; if ($@) { print Caught exception; }' I would expect this to print Caught exception and not died, which indeed is what happens on my machine. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: capture error - the better way?
2009/12/15 Shlomi Fish shlo...@iglu.org.il: On Tuesday 15 Dec 2009 14:25:28 Xiao Lan (小兰) wrote: On Tue, Dec 15, 2009 at 7:50 PM, Xiao Lan (小兰) practicalp...@gmail.com wrote: I did have tried that, but this will get a runtime error. Sorry this is exactly a compile-time error. # cat except.pl eval { $x=12/0 }; print 0 div error if $@; # perl -c except.pl Illegal division by zero at except.pl line 2. So, can't we capture it at runtime when meeting such error? No, you cannot capture compile-time errors at runtime. You'll need to make sure your code compiles before you run it. Note that you can capture such errors if you do perldoc -f require, perldoc -f do (for a filename), string eval , etc. at run-time, in which case perl 5 invokes its compiler to compile some code at run-time. How can Illegal division by zero be a compile-time error? It seems clear to me that it's a run-time error, which the optimizer has (wrongly) decided to raise at compile-time. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: capture error - the better way?
2009/12/15 Shlomi Fish shlo...@iglu.org.il: On Tuesday 15 Dec 2009 15:53:28 Philip Potter wrote: How can Illegal division by zero be a compile-time error? It seems clear to me that it's a run-time error, which the optimizer has (wrongly) decided to raise at compile-time. Well, the Perl compiler tends to collapse constants. So if it sees something like: ... ( CONSTANT_EXPR() / 0 ) ... it will try to find the value of CONSTANT_EXPR() / 0 so it can optimise it as a constant for the interpreter, and then get a fault there. It's a standard optimisation technique that also exists in other compilers such as gcc that are collapsing such constant expressions. So it does get evaluated at compile- time and as such should be avoided. I'm well aware of constant folding, and yes it is a standard technique. But your description of it is incorrect. If evaluating a constant expression results in a runtime exception, that runtime exception must happen at runtime, and not at compile time. In general, it is the duty of an optimizer never to change program behaviour, only performance. Therefore an optimizer which tries to fold a constant expression at compile time only to find a divide by zero exception should write code which raises a divide by zero exception at runtime. Raising a divide by zero exception at compile time is not what the original program did, and so it's not what the optimizer should do either. If an optimizer *were* allowed to move runtime errors to compile time, it wouldn't just prevent use of 1/0. The following reasonable (if unrealistic) example would not be guaranteed to compile: # Check if a division will emit any sort of exception (divide by zero, overflow, underflow, NaN, whatever) sub check_division { my ($n, $d) = @_; eval { $n/$d}; return Invalid division if $@; return Valid division; } # and elsewhere... check_division(1,0); Why? Because if a sufficiently clever optimizer saw that somewhere in your code you call check_division(1,0), it could decide to inline the function there. And once it's done that, it can propagate the constants 1 and 0 into the local versions of the variables $n and $d, producing the constant expression 1/0 within the eval block. Now it merrily decides to calculate this expression's value at compile time, only to emit a compile-time error. If this error had happened at runtime, the program would have caught it and functioned entirely correctly; but by moving the exception to compile time -- and out of the eval block which was supposed to catch any exceptions -- the program won't even compile. If I am supposed to defend against such an overzealous optimizer, how would I write code that uses any block eval to catch exceptions? A sufficiently advanced optimizer could always use some method like that described above to precalculate the results of the eval, triggering an exception at compile time instead of runtime. Therefore block eval could never be guaranteed to catch runtime exceptions! Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Comparing aggregates for equality when order doesn't matter
Hi all, I have a method which returns two arrayrefs: one is an array of hashes, and the other an array of arrays. I'm writing a test harness for this method, so I put together some testcases and expected results. I don't care what order the arrays are in; I only care that the arrayrefs returned by the function contains exactly the same set of values as the expected results. Something like this: # embeddings is an array of hashes, $anchors is an array of arrays my ($embeddings, $anchors) = $embedder-embed($subgraph); my $expected = { embeddings = [ { a = '1', b = '2', }, { a = '1', b = '3', }, { a = '2', b = '3', }, ], anchors = [ ['1','2'], ['2','3'], ['1','3'], ], }, use Test::More tests = 2; is_deeply($embeddings, $expected-{embeddings}, embeddings match); is_deeply($anchors,$expected-{anchors}, anchors match); ...except that I don't care if the actual and expected array referents are in a different order, only that they contain the same items. My first thought is to sort the arrays according to some arbitrary convention and use is_deeply() as above; but I'm stuck as to how to write a sort ordering well. I'm thinking I should sort the array of hashes by lexicographical ordering of hash values, with lexicographical significance determined by sorted key order, and sort the array of arrays by simple lexicographical order. I have something like this: use List::MoreUtils qw(pairwise); # lexicographical comparison function for lists of hashes sub lexhash { my @seq_a = map {$a-{$_}} sort keys %$a; my @seq_b = map {$b-{$_}} sort keys %$b; for my $pair (pairwise {[$a,$b]} @seq_a, @seq_b) { return $pair-[0] cmp $pair-[1] if $pair-[0] cmp $pair-[1]; } return 0; } # lexicographical comparison function for lists of arrays sub lexarray { for my $pair (pairwise {[$a,$b]} @$a, @$b) { return $pair-[0] cmp $pair-[1] if $pair-[0] cmp $pair-[1]; } return 0; } is_deeply([sort lexhash @$embeddings], [sort lexhash @{$expected-{embeddings}}], embeddings match); is_deeply([sort lexarray @$anchors], [sort lexarray @{$expected-{anchors}}], anchors match); This works and does what I want, but it feels hacky and I'm not entirely happy about it. I have these questions: 1. Is there a set type which holds aggregate data and doesn't care about order, which I could use to compare these results for equality? 2. If not, is my solution to the problem a reasonable design? If not, what better way could I do it? 3. If it's a reasonable design, is there any way my implementation of it could be improved? In particular, could it be made more readable? I'm not convinced that I could work out what this code does next week, let alone in six months time. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Comparing aggregates for equality when order doesn't matter
2009/12/15 Shawn H Corey shawnhco...@gmail.com: Philip Potter wrote: 1. Is there a set type which holds aggregate data and doesn't care about order, which I could use to compare these results for equality? You can use a hash as a set or a bag. Yeah I thought about this -- while I can see how it works with simple scalars, I can't see how to make it work with references: $ cat foo.pl #!/usr/bin/perl use strict; use warnings; use Data::Dumper; # Make Data::Dumper pretty $Data::Dumper::Sortkeys = 1; $Data::Dumper::Indent = 1; # Set maximum depth for Data::Dumper, zero means unlimited $Data::Dumper::Maxdepth = 0; my @a = ([1],[2],[3]); my @b = ([1],[2],[3]); my %set_a = map { $_ = 1 } @a; print '%set_a : ', Dumper \%set_a; my %set_b = map { $_ = 1 } @b; print '%set_b : ', Dumper \%set_b; use Test::More tests = 1; is_deeply(\%set_a, \%set_b, 'Test that references as hash keys works'); __END__ $ perl foo.pl 1..1 %set_a : $VAR1 = { 'ARRAY(0x1b66c40)' = 1, 'ARRAY(0x1b66df0)' = 1, 'ARRAY(0x1d50550)' = 1 }; %set_b : $VAR1 = { 'ARRAY(0x1ba7010)' = 1, 'ARRAY(0x1d1b3a8)' = 1, 'ARRAY(0x1d84698)' = 1 }; not ok 1 - Test that references as hash keys works # Failed test 'Test that references as hash keys works' # at foo.pl line 26. # Structures begin differing at: # $got-{ARRAY(0x1d1b3a8)} = Does not exist # $expected-{ARRAY(0x1d1b3a8)} = '1' # Looks like you failed 1 test of 1. Perhaps a way of collapsing references to unique strings would work? ie: my %set_a = map { stringify($_) = 1 } @a; where stringify() is a function which maps references to unique strings identifying their referent? Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: being smart about script structure
2009/12/11 Bryan R Harris bryan_r_har...@raytheon.com: Seems like a waste to do step 2 in a subroutine since we only do it once, but it does fill the main body of the script with code-noise that makes it harder to debug overall logic problems... Not much logic here, but certainly in more complex scripts. A waste of what exactly? You don't have a limited budget of sub keywords. I guess I figured you had to build in structure (variables?) to be able to pass things back and forth from subroutines that normally you wouldn't have to set up. For example, if I'm populating a complex variable @d with lots of pointers, hashes, arrays, etc. within, if I populate that within a subroutine, how do I get it back out conveniently without it making a whole nother copy of it outside? If it's 500 MB, isn't that horribly inefficient? Plus, I have to keep track of it. Perl doesn't have pointers; it has references. They are similar but not the same. Anyway populate_x looks like this: sub populate_x { return [ # Big complex data structure # ... # ... # ... # ... ]; } There is no large scale copying; the [] construct creates a new array and returns a reference to it. You just return a single scalar value from the function to the calling context. Kind of like C int *populate_x (void) { int *foo = malloc(X_SIZE * sizeof *foo); if (!foo) return NULL; /* Do something to populate *foo */ return foo; } but in C this is dangerous and hairy because you're mucking around with pointers and raw blocks of memory while in Perl you're dealing just with builtin datatypes. If you want to populate an array instead: my @z = populate_z(); # ...and later... sub populate_z { return ( # note parens, not square brackets # Big complex data structure # ... # ... # ... # ... ); } then this will theoretically copy the entire list; but if the list contains references it will only copy the references, and not the things referred to. So it's still not a deep copy, but it's deeper than the reference-based populate_x. Subroutines are not just about code reuse. Which is more readable: my $x = [ # Big complex data structure # ... # ... # ... # ... ]; snip my $x = populate_x(); snip I guess my only struggle there is that any perl person can read the perl code. At first glance, I have no clue what populate_x() does because you gave it a name that's not in the camel book. You can't write any meaningful program with just builtins; sooner or later you have to use a subroutine. And a reader won't know exactly what a function does just by looking at a call to it; but the reader *shouldn't have to*. If the subroutine is hiding information the reader needs, perhaps it was a poor choice of code to put in a subroutine. But consider this: use CGI; my $cgi = CGI-new(); I don't know exactly what this function call did. I know that it create a CGI object and populated it with initial data. I don't know what that object is -- it could be a blessed hash, and blessed array, even a blessed scalar ref. I don't know what initial data I got. But I *don't have to care*, because all I care about is that I have an object which will let me do things like this: print $cgi-header, $cgi-start_html('hello world'), $cgi-h1('hello world'), $cgi-end_html; And as long as these subroutines understand the low-level details of what the $cgi object contains, I don't have to; because I'm dealing only at the higher level, and the subroutines take care of all the messy details for me. [At a deeper level, because I am isolated from these details, the designer of CGI is free to totally change the detailed implementation; and because he controls the only subroutines which ever deal with a CGI object's implementation, he knows that nobody's code will break.] I know what these subroutines do through two means: the name and the documentation. The name is very important; it needs to speak the caller's language and be a short, clear summary of what the subroutine does. The documentation is even more important: it is the subroutine author's guarantee to you of what it does. As a result, it might well be a mistake to write this: my $x = populate_x(); for (@$x) { # Big # Complex # Operation # On # $_ } because in this case, you've hidden how $x was initialized, and then you've gone and written code which the reader can't understand without knowing the details of what $x contains. But if the reader sees this: my $x = create_x(); for my $p (@$x) { # The fact that x is an arrayref is noted in create_x's documentation # but we don't have to care about the exact structure of the contents # of the array; they might be further arrayrefs or hashrefs or simple # scalar values; but we know that we can call process_p on them: process_p($p); } then the reader doesn't know what $x contains exactly, but they *don't have to*. We make
Re: Windoze Woez
2009/12/12 Parag Kalra paragka...@gmail.com: Hello All, This works on Linux - perl -e 'foreach (Sugar,Sex,Simplicity,Sleep,Success,Smoking) { print I am only addicted to - 'Shekels' \n if ($_ =~ /^s.*/i) }' This works on Windoze - perl -e foreach (Sugar,Sex,Simplicity,Sleep,Success,Smoking) { print 'I am only addicted to - Shekels' if ($_ =~ /^s.*/i) } How can I have 1 code that will run both on Nix Windoze. The easy way: use cygwin! The meta way: why do you care? It's a one-liner, and by its very nature a one-off, throwaway program. The cheating way: you can force the windows version to work under *NIX like this: p...@tui:~/tmp$ echo \$_ $_ p...@tui:~/tmp$ perl -e foreach (Sugar,Sex,Simplicity,Sleep,Success,Smoking) { print 'I am only addicted to - Shekels' if ($_ =~ /^s.*/i) } I am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - Shekels this is because in bash, $_ means The last argument of the previous command executed. The echo \$_ command forces $_ to interpolate to a literal $_, allowing the perl script to see it unmolested. The real way: Consider that you must use to surround the program, because cmd.exe won't understand ''. Now, the reason that the existing windows version doesn't work under *NIX is that the shell is trying to interpolate $_ into the string as mentioned above, so we must try to prevent that. We can do this by escaping the $: p...@tui:~/tmp$ perl -e foreach (Sugar,Sex,Simplicity,Sleep,Success,Smoking) { print 'I am only addicted to - Shekels' if (\$_ =~ /^s.*/i) } I am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - Shekels D:\perl -e foreach (Sugar,Sex,Simplicity,Sleep,Success,Smoking) { print 'I am only addicted to - Shekels' if (\$_ =~ /^s.*/i) } I am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - ShekelsI am only addicted to - Shekels In the end though, I fail to see the utility of writing one-liners which execute on both linux and windows. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Windoze Woez
2009/12/12 Parag Kalra paragka...@gmail.com: How is it going to new line without a new line character - '\n'. If I am not wrong - 'q/ /' signifies quoted context thus - a replacement for double quotes but still you should need a new line character to go to next line. q// is single quotes, qq// is double quotes. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Windoze Woez
2009/12/12 Shawn H Corey shawnhco...@gmail.com: Alan Haggai Alavi wrote: Hi, Windows requires you to use double quotes in place of single quotes. Saving to a file and executing it is the only way that is cross-platform, I suppose. Doesn't Windows respond to here redirects? perl EOD print Hello world\n; EOD D:\perl -e EOD was unexpected at this time. :( -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: assign operator
2009/12/11 Steve Bertrand st...@ibctech.ca: Rafa? Pocztarski wrote: There is no := operator in Perl 5. In Perl 6 := is a run-time binding operator: $x = 1; $y := $x; $y = 2; Now both $x ==2 and $y == 2 In Perl 5 you use typeglob assignment to achieve the same thing: $x = 1; *y = \$x; $y = 2; Does this mean no strict 'refs' will no longer be needed in cases where the symbol table must be manipulated manually? It isn't needed right now! p...@tui:~/tmp$ cat bar.pl use strict; use warnings; our ($x, $y) = (0,1); *y = \$x; $y = 3; print \$x = $x and \$y = $y\n; p...@tui:~/tmp$ perl bar.pl $x = 3 and $y = 3 strict 'refs' only prevents messing around with symbolic references. These are hard references. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: being smart about script structure
2009/12/11 Bryan R Harris bryan_r_har...@raytheon.com: I'm not even sure how to ask this question, but here goes: I struggle knowing how to structure my code, what things belong as their own subroutines and what things can stay in the main script. How do the smart guys make these decisions? For example, let's say I need to: 1. Read a complex file 2. Populate a complex dataset 3. Accept user input 4. Based on user input, either modify or add to dataset, or quit 5. Update files on disk 6. Go to step #3 Which steps should be their own subroutines vs. just writing them into the main part of the script? And how did you make that decision? If you like, put everything in its own subroutine, call the main one main, and reduce the main part of your script to just this: main(); If you want a more OO-style script, you might have: my $x = Myprogram-new(); $x-main(); Seems like a waste to do step 2 in a subroutine since we only do it once, but it does fill the main body of the script with code-noise that makes it harder to debug overall logic problems... Not much logic here, but certainly in more complex scripts. A waste of what exactly? You don't have a limited budget of sub keywords. Subroutines are not just about code reuse. Which is more readable: my $x = [ # Big complex data structure # ... # ... # ... # ... ]; my $y = [ # Big complex data structure # ... # ... # ... # ... ]; for my $p ($x) { for my $q ($y) { #Big # complex # multi-statement # manipulation # of # $p # and # $q } } Or this: my $x = populate_x(); my $y = populate_y(); for my $p (@$x) { for my $q (@$y) { process_pq($p, $q); } } Or even: my $x = populate_x(); my $y = populate_y(); process_xy($x, $y); # xy_process now contains the for loops The point is that in the first version, you are constantly bouncing from the big-picture ideas to the low-level messy details. By abstracting code out into subroutines populate_x(), populate_y() and process_xy(), you have the main script which deals in big ideas, and three subroutines which deal with messy details. A subroutine should do one thing, and do it well. Any suggestions? Where can I read more on this stuff? What questions should I be asking that I'm not smart enough to ask? The best that I can suggest is to read other people's code and ask other people to read your code. Think of a specific example and come up with a plan of how you would code it, and ask for criticism. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: matching an element from one array to another
2009/12/10 John W. Krahn jwkr...@shaw.ca: Noah wrote: Hi there, Hello, I am hoping to figure out the best Way to write something. I have two arrays @previous_hostnames and @hostnames. I want to figure out if there is at least one matching element in @previous_hostnames that is found in @hostnames. my $found = map { my $x = $_; grep $x eq $_, @previous_hostnames } @hostnames; Slightly more efficient is List::Util::first use List::Util qw(first); my $found = first { my $x = $_; grep {$x eq $_} @previous_hostnames } @hostnames; because this will stop as soon as the first element satisfying the condition is found. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Problem with break
2009/12/9 Anush anushajl...@gmail.com: Hi all, i have a problem with the break statement, in my program the break is not working, that is the control would not breaks from the loop. Please help me. Can you give an example of your problem? What is your code? What did it do? What did you expect it to do? Perhaps break doesn't do what you think it does. Have you looked at perldoc -f break? Maybe last is what you meant instead? Type perldoc -f last to get its description, or google perldoc last. Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: passing Hash to subroutine help please
2009/12/9 John W. Krahn jwkr...@shaw.ca: Jeff Pang wrote: Noah: sub exiting { my ($hostname, %login) = @_; Passing arguments like this has no such problem. But you'd better pass the hash as a reference to the subroutine. exitint($hostname, \%login); sub exiting { my $hostname = shift; my %login = %{+shift}; What is the point of passing a reference if you are just going to copy the whole hash anyway (which is what the OP was doing)? One possible reason is that if you later want to add a new, optional parameter to the subroutine, you can do this without modifying the existing interface (and therefore modifying all code which calls exiting): sub exiting { my $hostname = shift; my %login = %{+shift}; my $optional = shift // $default_value; # use || before 5.10 and be careful # ... } This isn't possible if you are passing the hash as a list: sub exiting { my ($hostname, %login) = @_; my $optional = ??? # whoops, we've used all arguments already } I'd therefore argue that passing a hashref is more maintainable. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Email does not send attachment ...
2009/12/9 Sneed, Sean P sean.p.sn...@centurylink.com: Try the paths like this C:\\my_file.log Is there a reason that you suggested this? Perl under Windows, just like C and C++ under Windows, accepts / as a directory separator just fine. And if you stick to using / as your directory separator, porting to unix-based systems becomes that much easier. Perlmonks has a far more full discussion of the implications of using / or \\ as your directory separator under Windows: http://www.perlmonks.org/?node_id=110030 and indeed there are some good reasons given for using \\ in filenames under windows, such as when you are passing a filename to a system() call. But there are also good reasons for avoiding \\, such as consistency and portability. I doubt that MIME::Lite will be affected by being passed a / delimited filename but I guess it's worth a go. Without much more information to go on, though, it's hard to diagose the original problem. Phil -Original Message- From: Robert H [mailto:sigz...@gmail.com] Sent: Tuesday, December 08, 2009 6:58 PM To: beginners@perl.org Subject: Re: Email does not send attachment ... On 12/8/09 3:56 PM, Tony Esposito wrote: Hello, I am using Perl 5.8 on WindowsXP and Windows Server 2003. I can not get the following attachment to arrive/attach even though I get the email with no issues. Any ideas? use MIME::Lite; use Net::SMTP; unless (-e 'C:/my_file.log' -s 'C:/my_file.log' ) { print file not found\n; } my $msg = MIME::Lite-new ( From = 'my_em...@yahoo.com', To = 'your_em...@yahoo.com', Subject = 'Testing error email', Type ='multipart/mixed' ) or die Error creating multipart container: $!\n; $msg-attach ( Type = 'TEXT', Data = 'Error in the module that caused this email', ) or die Error adding the body text to email message: $!\n; $msg-attach ( Type = 'TEXT', Path = 'C:/my_file.log', Filename = 'my_file.log', Disposition = 'attachment' ) or die Error adding file attachment: $!\n; MIME::Lite-send('smtp', 'smtp.server.net', Timeout=60); $msg-send; I see why you do that now. Sorry. Bob -- 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: Can I design a website using Perl
2009/12/6 Erez Schatz moonb...@gmail.com: This being said, anything you can do in PHP is available in Perl, PHP being the less talented brother of Perl and all. For instance, for embedding code tags in your HTML page, look no further than some awsome and powerful Perl modules such as Template::Toolkit (www.template-toolkit.org) which gives you EXACTLY that. You might also want to look at Catalyst, (www.catalystframework.org) which is a powerful, flexible and very excellent web-framework that gives you all of what you can pull off with PHP and then some. I support the recommendation of TT [Template::Toolkit], but Catalyst is massive overkill as a recommendation here. Given that the OP said this: I have very basic requirements. I want to use Perl mainly to fetch results from MySQL db inside HTML, just the same thing which we do inside the PHP tags ? ? it is clear that Catalyst is far to big, bloated, and full-featured to be an appropriate tool for the OP's goal. If all he wants to do is a couple of DB lookups, he doesn't need a full blown MVC framework like Catalyst. Question to the OP: Why have you decided to try things out in Perl? If it is because you've heard something like Perl is better than PHP then stop and try to find out *why* and *in what situations* is Perl better than PHP. In the situation of a simple webpage with a simple DB lookup halfway down, I think PHP is probably simpler and easier to use than Perl. Perl becomes better than PHP in more complex situations, with larger websites, more complex databases and so on. Situations in which web development frameworks such as Catalyst become appropriate. If you are doing this as a way to learn Perl, be aware that you have not picked a situation in which Perl outshines PHP; and you may wonder afterwards why people rave about Perl. The answer is that the best language depends on the task you are trying to achieve. If this is a pure learning exercise then I do recommend you learn about MVC and read Catalyst::Manual::Tutorial -- it can show you a totally different paradigm for web design than vanilla PHP offers. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Can I design a website using Perl
2009/12/6 Parag Kalra paragka...@gmail.com: Question to the OP: Why have you decided to try things out in Perl? If it is because you've heard something like Perl is better than PHP then stop and try to find out *why* and *in what situations* is Perl better than PHP. In the situation of a simple webpage with a simple DB lookup halfway down, I think PHP is probably simpler and easier to use than Perl. Perl becomes better than PHP in more complex situations, with larger websites, more complex databases and so on. Situations in which web development frameworks such as Catalyst become appropriate. I have picked Perl recently and have started enjoying it. So thought of using it instead of PHP just for educational purpose. But any-ways thanks to all for sharing and making me aware with all the available frameworks. Hey BTW what does OP stand for ? :). My name is Parag... :) Original Poster -- in this case, you! :) Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: I want to know the reason
2009/12/5 Parag Kalra paragka...@gmail.com: But this code is not working: #!/usr/bin/perl my @column_names=(A..ZZ); for(my $i=0; $i26; $i=$i+1) { my $tmp_file = $column_names[$i]..out; open $column_names[$i], column_names/$tmp_file or die Could not create the file - $tmp_file \n; print $column_names[$i] This is not working\n; Put the indirect filehandle in curlies: print {$column_names[$i]} This is not working\n; A simple variable can be used raw as an indirect filehandle but more complex expressions must be disambiguated with curlies. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Need to process a XML file through Perl.
2009/12/2 Parag Kalra paragka...@gmail.com: Currently I am planning to process the above requirement using simple Perl regex. But I feel it can be made simpler using any of the available modules. So I have following questions: 1. Which are the best available XML modules for Perl? 2. Out of the best available modules, which one would suite my requirement in the best way? 3. Any pointers to specific methods of the XML modules to suffice my needs would be helpful. TIA In general, regexes are to be avoided for context-free or more complex languages, in which category XML, HTML and SGML fall. perlbot on #perl on freenode gives this stock response: Don't parse XML with regex! Use a real parser. Avoid XML::Simple (see the xml::simple factoid). Choices are ::Easy, ::Smart, ::Twig for simple stuff. LibXML for big stuff. See also XML::All. http://perl-xml.sf.net/faq/; That's a good list of XML modules and a good page for much more comprehensive information. I would add, however, that it really depends what you intend to do. Regexes are useful in a very limited set of conditions: * If the XML is simple, regular, and predictable, and it has no danger of getting more complex as time progresses * If you only want to extract or modify one highly localised feature from the file, and if what you want to access will not change or grow more complex And of course if these conditions are satisfied then the question Why are you using XML? is raised. A valid answer might be The information I need is in XML produced by someone else. In Hack #23 of Spidering Hacks, David Landgren gives a case study where he had some printers which reported their status (ink and paper remaining) in an XML file served from a web server. It was such a small and simply-structured amount of data, being generated by a machine which was predictable and which wouldn't change the structure, that regexes were the simplest way to extract the information from them. However, there are many counterindications which show that regexes will become more complex and harder to maintain than the equivalent parser-based implementation. While regexes are (IMHO) acceptable for extracting information from fixed- or restricted-structure, regular, machine generated XML, they will be the Wrong Thing entirely if the XML has arbitrary text in attributes tag attrib=/tag, arbitrary tag nesting structure, or if the XML format is evolving or extensible, or if you may want to retarget your parser at a different XML format in the future. In general, if unsure, use a real parser. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: CGI programming
2009/11/30 Anant Gupta anantgupta...@gmail.com: I am not allowed to write to /var/www/cgi-bin/ I am not the root user of my machine Is their any way i can try out my perl cgi programs on this machine( Red Hat Linux) Ask the root user of your machine. As an example of how these things work, at my university I can put things on my website my putting files in ~/public_html. Perl scripts may be used, but they will only be executed if I give them a particular name. On other systems, perl scripts will only be executed if they are put into a particular directory. The rules are arbitrary and were decided by my system administrators. The only way you can find out is to ask the person who runs your machine. If they run an IT support website, have a look on there. Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Assignment Operator
2009/11/26 Marco Pacini i...@marcopacini.org: Hi All, I'm studying Perl since one week on Learning Perl written by L. Wall and in the paragraph Assignment Operators i don't understand why this: ($temp = $global) += $constant; is equivalent of: $tmp = $global + $constant; Instead, before i read it, i thought it was equivalent of: $temp = $global; $temp = $temp + $constant; Let's relabel the first value of $temp as a new variable, $temp2: my $temp2 = $global; $temp = $temp2 + $constant; Notice that $temp2 has just been given the value of $global, so this second statement is the same as: $temp = $global + $constant; In other words, you're both right, because your version is equivalent to $temp = $global + $constant. Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Code issue with perl5.10
2009/11/25 Steve Bertrand st...@ibctech.ca: Steve Bertrand wrote: Hi all, I just upgraded from perl 5.8 to perl 5.10.1. Everything went well, except for a single module that I need. The offending code is this: ${$self-{__pb_template_list}}[...@{$self-{__PB__TEMPLATE_LIST}}}]-param( $param, $value ); As I understand it, $# has been deprecated in 5.10. Can someone please help me understand the above line of code? What does $# represent in this context? Well, I finally trudged through it. In order to make it work: my $elem = �...@{ $self-{__PB_TEMPLATE_LIST} }; $elem--; ${$self-{__PB_TEMPLATE_LIST}}[$elem]-param( $param, $value ); Not the most elegant solution, but it works for now! Wouldn't this work? ${$self-{__PB_TEMPLATE_LIST}}[$#{$self-{__PB_TEMPLATE_LIST} }]-param( $param, $value ); Originally you had $...@{$foo}} which doesn't make sense to me. If $foo is an arrayref then to get the index of the last element of the array referent you use $#{$foo}. Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Regex to get last 3 digits of a number.
2009/11/23 Alan Haggai Alavi alanhag...@alanhaggai.org: my $number = '0111'; my ($index) = ( $number =~ /\d+(\d{3})/ ); $index would contain the last three digits: '111'. \d+ should be \d* above, otherwise you fail to match on 3 digit numbers: p...@tui:~/tmp$ perl -E ' my $number = q{111}; my ($index) = ( $number =~ /\d+(\d{3})/ ); say $index' p...@tui:~/tmp$ Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Documentation practices
2009/11/24 Steve Bertrand st...@ibctech.ca: I've noticed that the POD for several modules do not include method usage for the functions/methods that are designed as 'internal-only'. I've reached a stage where not having POD for ALL of my methods is becoming overwhelmingly cumbersome, as I have to read the code to remember usage. Is there a common practice for this? If POD doesn't display internal sub usage, where would one document it? This documentation is clearly necessary, but not for the users of your module. What you do is document it in such a way that the users never see it, only the maintainers. There are several ways to do it. The most common is just a series of # comments. There are ways to do it in pod but without making it visible to the users: =for Rationale: This function uses Smith's algorithm instead of Jones's. Jones's is faster in the general case but Smith's performed better when benchmarked with our test dataset. =cut This pod block will not be visible to the users, because it uses the =for pod directive which means only for the named pod translator. There is no translator called Rationale: and so no translator will read it. The =begin and =end pod directives can serve a similar purpose. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: how to write a debug-print sub with one argument?
2009/11/23 Mark_Galeck mark_galeck_spam_mag...@yahoo.com: Hello, I want to write a simple debug-print subroutine, which you could call like this: $foobar = foobar; dbgPrint foobar; One problem with your interface is it breaks under use strict;. You should always use strict and use warnings in any program not part of a perl -e command line. Barewords and symbolic references are both ugly and the source of hard-to-find bugs. Here is a rant about symbolic references and why they are bad: http://perl.plover.com/varvarname.html and it would print the variable name and value. I came up with the following: I would recommend the Smart::Comments module from CPAN. It's used as follows: p...@tui:~/tmp$ cat foo.pl use strict; use warnings; my $x = hello; use Smart::Comments; ### $x p...@tui:~/tmp$ perl foo.pl ### $x: 'hello' The line containing a comment beginning ### is treated as special. Smart::Comments detects it contains a variable name and prints both the name and its value. It works with lexical variables, too. There are many advantages to Smart::Comments, one of which is that when you want to disable debug printing from your program, you just remove the use Smart::Comments; line and all smart comments become normal comments again. Unfortunately I can't tell you how to solve your original question; but I hope this fixes the underlying issue. Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: 答复: the question of one program
2009/11/20 gaochong zjgaoch...@gmail.com: Thanks . But the code is from cpan.org ,and is crappy ,where I will go ? CPAN has no quality control. There is no guarantee that anything you get from CPAN will not be, as you say, crappy. As a result, be selective with what you download from CPAN. Ask questions, go by reputation, check that the module is maintained (if it hasn't been updated since 2003, the owner clearly doesn't care about it much any more), check the pod: good, comprehensive documentation is an indicator that the underlying code is also good (although some good code is badly documented, one rarely sees bad code well documented). Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: redirecting to a file in perl
2009/11/13 Dermot paik...@googlemail.com: 2009/11/13 Subhashini subhashinibasavar...@gmail.com: i did try it but didn't work.. Did try what? Can you please show us what you've tried. Post the updated version. And can you please reply to the mailing list. There will be others how might find the answer to their problem by searching the archives and seeing your mail. Subhashini, In case you don't know, you should click Reply All or Reply to all rather than plain Reply. Make sure that beginners@perl.org is in the Cc: or To: field. If you don't, we can't see your messages, only the person you reply to (in this case Dermot) can see them. I have only seen one message from you in this thread, so even if I wanted to and were able to help I can't, because I don't know what you're asking or having trouble with. Philip PS To the list in general: is there a reason the list doesn't just set Reply-To: beginn...@perl.org? A few other mailing lists I'm on do this and it means that you can't accidentally take something offlist (something I myself did, to my shame, a few days ago). -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Explicit return and anonymous hashes
2009/11/11 Shawn H Corey shawnhco...@gmail.com: Philip Potter wrote: my @aoh = pairwise { { %{$a}, %{$b} } } @aoh_a, @aoh_b; no warnings 'once'; my @aoh = pairwise { +{ %{$a}, %{$b} } } @aoh_a, @aoh_b; Thanks for your response. What are the grammar rules here? Why must I use an explicit return or unary + operator? Why is it not being interpreted as an anonymous array? Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Explicit return and anonymous hashes
2009/11/12 Peter Scott pe...@psdt.com: On Thu, 12 Nov 2009 09:55:39 +, Philip Potter wrote: 2009/11/11 Shawn H Corey shawnhco...@gmail.com: no warnings 'once'; my @aoh = pairwise { +{ %{$a}, %{$b} } } @aoh_a, @aoh_b; Thanks for your response. What are the grammar rules here? Why must I use an explicit return or unary + operator? Why is it not being interpreted as an anonymous array? Because otherwise it's interpreted as a naked block. My question runs deeper than this. What are the criteria? When are curlies a bare block and when are they an anonymous hash? What are the grammar rules? How could I have known a priori to put a return or + in? The camel book page 247 says you may occasionally have to disambiguate braces at the beginning of a statement by putting a + or return in front but doesn't go on to explain *when* those occasions are. This contrasts with the plans for Perl 6, where it's very easy to define what's a block and what's a hashref: http://svn.pugscode.org/pugs/docs/Perl6/Spec/S06-routines.pod C{...} is always a block. However, if it is completely empty or consists of a single list, the first element of which is either a hash or a pair, it is executed immediately to compose a CHash object. What is the equivalent rule for Perl 5? Is it If it can be interpreted as a block, it is? Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
hash keys and readable code
Hi all, I have a subroutine which uses a hashref to accept optional parameters: sub do_test { my ($testfilename, $flags) = @_; ## etc... } Here $flags is given an hashref containing optional arguments such as { thorough = 1, limit = 3, retries = 2}. One (but not all) of the flags is, if present, passed on to a downstream subroutine. So I've got code something like this: sub do_test { my ($testfilename, $flags) = @_; my @results = exists $flags-{limit} ? readfsgs($testfilename, {limit = $flags-{limit} }) : readfsgs($testfilename, { } ); } where readfsgs needs the 'limit' entry but not the others from $flags. I was wondering if maybe its better to build the optional argument hashref first to simplify the call: sub do_test { my ($testfilename, $flags) = @_; my %readfsgs_flags; $readfsgs_flags{limit} = $flags-{limit} if exists $flags-{limit}; my @results = readfsgs($testfilename, \%readfsgs_flags); } but the extra exists test seems a bit ugly and it won't scale up well if I have a few flags to transfer to the child subroutine. The first version will not scale up either. To make it more scalable, I thought about doing it using hash slices: sub do_test { my ($testfilename, $flags) = @_; my %readfsgs_flags; my @flags_to_copy = qw(limit); # can scale up by adding more hash keys here @readfsgs_fla...@flags_to_copy} = @{$flag...@flags_to_copy}; my @results = readfsgs($testfilename, \%readfsgs_flags); } but this has a bug: if $flags doesn't contain limit, %readfsgs_flags will contain (limit = undef); ie $readfsgs_flags will exist but be undefined. It shouldn't exist. What way of doing this would be most maintainable and readable? What is the Perlish way of doing things here? Thanks in advance, Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: hash keys and readable code
2009/11/11 Uri Guttman u...@stemsystems.com: PP == Philip Potter philip.g.pot...@gmail.com writes: PP my %readfsgs_flags; PP my @flags_to_copy = qw(limit); # can scale up by adding more hash keys here PP @readfsgs_fla...@flags_to_copy} = @{$flag...@flags_to_copy}; PP my @results = readfsgs($testfilename, \%readfsgs_flags); PP } PP but this has a bug: if $flags doesn't contain limit, %readfsgs_flags PP will contain (limit = undef); ie $readfsgs_flags will exist but be PP undefined. It shouldn't exist. easy fix. just grep through all the keys you want and test for exists. my @flags_to_copy = grep { exists( $flags-{$_} } qw(limit); then the slice works as you have it and you only get flags you are interested in and also that exists coming in. That fixes the major problem, thanks! you can also simplify the copy a little with a hash ref: my $readfsgs_flags = { map { $_ = $flags-{$_} } @flags_to_copy } ; Is this really simpler than my version? PP @readfsgs_fla...@flags_to_copy} = @{$flag...@flags_to_copy}; It took me a while to parse the map call, and the extra curlies from the anonymous hash get confused with the code block curlies. Is this map call a common technique for constructing subhashes (analogous to subsets)? Also, is the hash ref necessary? Would this also work? my %readfsgs_flags = map { $_ = $flags-{$_} } @flags_to_copy; and if you feel like really combining lines (not so great in this case): my $readfsgs_flags = { map { $_ = $flags-{$_} } grep { exists( $flags-{$_} } qw(limit) ; or another cleaner way would be to put the exists inside the map: my $readfsgs_flags = { map { ( exists( $flags-{$_} ) ? $_ = $flags-{$_} : () } qw(limit) ; I see what you've done there. I think you're right that it's probably best in two statements -- after all, it's going to be two lines whether it's two statements or one. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Explicit return and anonymous hashes
I am playing with List::MoreUtils::pairwise and noticed some funny behaviour. I'm trying to merge two lists of hashes into one list of hashes: p...@tui:~/tmp$ cat bar.pl use strict; use warnings; use List::MoreUtils qw(pairwise); use Data::Dumper; my @aoh_a = ( { foo = 1, bar = 2 }, {foo = 3, bar = 4} ); my @aoh_b = ( { fred = 5, jim = 23}, {fred = 1089, jim = 1729} ); my @aoh = pairwise { return { %{$a}, %{$b} } } @aoh_a, @aoh_b; print Dumper(\...@aoh); p...@tui:~/tmp$ perl bar.pl Name main::b used only once: possible typo at bar.pl line 10. Name main::a used only once: possible typo at bar.pl line 10. $VAR1 = [ { 'bar' = 2, 'jim' = 23, 'foo' = 1, 'fred' = 5 }, { 'bar' = 4, 'jim' = 1729, 'foo' = 3, 'fred' = 1089 } ]; This works fine. But I noticed that if I remove the explicit return in the pairwise block, I get different results: p...@tui:~/tmp$ cat bar.pl use strict; use warnings; use List::MoreUtils qw(pairwise); use Data::Dumper; my @aoh_a = ( { foo = 1, bar = 2 }, {foo = 3, bar = 4} ); my @aoh_b = ( { fred = 5, jim = 23}, {fred = 1089, jim = 1729} ); my @aoh = pairwise { { %{$a}, %{$b} } } @aoh_a, @aoh_b; print Dumper(\...@aoh); p...@tui:~/tmp$ perl bar.pl Name main::b used only once: possible typo at bar.pl line 10. Name main::a used only once: possible typo at bar.pl line 10. $VAR1 = [ 'bar', 2, 'foo', 1, 'jim', 23, 'fred', 5, 'bar', 4, 'foo', 3, 'jim', 1729, 'fred', 1089 ]; Is this because the curlies are being interpreted as a block rather than an anonymous hash constructor? If so, when are they anonymous hash constructors and when are they blocks? How might I avoid this issue in future? And secondary question: how can I avoid the warnings about $main::a and $main::b? Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: the classic which is the fastest way to sort question
2009/11/9 Michael Alipio daem0n...@yahoo.com: Hi, i'm planning to sort an input file (which was File::Slurp'ed, most likely megabyte-sized file) in various ways. I did some readings and learned several methods that people have come up with in recent years. So to summarize, the default sort is fast (uses quick sort), If default sort is quicksort, I can't find the docs that say so. They seem to imply (but don't state explicitly) that default sort is whatever was measured to be fastest on that platform, be it quicksort or mergesort. See perldoc -f sort (function) and perldoc sort (pragma). explicit (using sub) is a bit slower, What does this mean? You can write anything, including any sorting algorithm, in a sub. other method that uses caching is faster. Then there's Schwartzian Transform and a packed version by Guttman. ST is not a sorting algorithm. It's a technique to improve the efficiency of another sorting algorithm by reducing the cost of the comparison function. Seems like everything is clear. Guttman is the fastest, until I went to cpan. Found Sort::Key, which claims to be the fastest, even faster that ST, GRT. Now, before someone says, why not try each one and see for yourself (which doing such could be another subject for me to learn), my question is this: if such faster sorting algorithms exist, why don't they just replace the default sort function in Perl? The default sort function is based on the classical sorting paradigm of having a comparison function and trying to do as few comparisons as possible. Any replacement for it must be a drop-in replacement in order to not break existing code. In fact as perldoc -f sort shows, there have been changes to the default sort over the years: perl 5.6 sort is quicksort, perl 5.7 sort is mergesort, and perl 5.8 and later has a sort which doesn't seem specified but which can be controlled by the 'use sort' pragma. The perl team are perfectly capable of improving efficiency and functionality if they see a need to. ST works on comparison-based sorting algorithms to make the comparison function cheaper by precaching as much of the work as possible. Whether this is more efficient or not depends on how expensive the comparison is and how much work can be extracted into an ST. If you need an ST, it's not because perl's sort() is inefficient; it is because the comparison function you gave it is inefficient. There is no improvement that the perl authors can ever make to sort() which will make an ST unnecessary. Sort::Key has a different interface: instead of a comparison function, you give it a key-generating function. As a result, it is able to do the precaching work itself, so no ST would be necessary. This means, however, that it is not a drop-in replacement for sort(); to use Sort::Key you have to rewrite code. Perl will always need a sort() function to maintain backward compatibility and because every sort can be expressed in terms of comparison functions. As for whether or why Sort::Key is faster, I can't say because its perldoc doesn't seem to give away its algorithm. One possible algorithm it uses is radix sort, which is O(n) provided the calculated keys are of bounded size -- which works for natural integers, but fails for bignums. And for the classical question, given my situation (in combination with File::Slurp), which is fastest sort method? (I hope somebody includes this in perlfaq in the future). I'm sorry to say, but this really will depend on your system. I don't know how Sort::Key works, but regarding all the other sorts, you'd have to measure it depending on your data and your system. Do you need the fastest possible sort? Philip PS your email client has a very long line length, causing my quoting above to go somewhat haywire. I'd recommend setting it to something like 74. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: the classic which is the fastest way to sort question
2009/11/9 Michael Alipio daem0n...@yahoo.com: Hi, Do you need the fastest possible sort? I'm not even sure if I really need to worry about all these sorting techniques. My program just reads a text file (wordlist). It might be megabyte-sized or probably few gigabytes (i might also add size checking on this to be safe with File::Slurp). Then I will give the user an option of sorting it in various ways, like length, alphabetical, numerical, frequency of letters, etc. I see, so it all boils down to how expensive the comparison you're going to implement to fully benefit from these techniques. Having a cheap comparison is important, but it's just one part of a very large story. Donald Knuth, one of the masters of computer science, wrote an entire textbook on searching and sorting: http://www.amazon.com/Art-Computer-Programming-Sorting-Searching/dp/0201896850 This is a huge subject and we've barely scratched the surface. There is no it all boils down to. Now comes another question for me to find the answer to, how expensive the comparisons in my sorting function would be... I guess there's no other way for me to find this out than to try it out myself. Actually, there is a certain amount of reasoning possible with respect to comparison functions: for example, a Schwartzian Transform will be a win if the key calculation is more expensive than the comparison of keys. What's worse is that there's also a depends on the system factor to consider as well. Sometimes I wish perl's motto is there's only one best way to do it so everyone would just agree on one way of doing something, so everyone would have the same beautiful and efficient code. There is no programming language which will automatically optimise the best possible sort for you. Not Perl, not Python, not Java, not C++. If you want the best, you will have to learn the theory. A compsci course or a thorough read of Knuth will go far. For now, I will probably just stick to using the built-in sort (just for sorting length, numbers, and letters), until I have gained enough knowledge about why it's necessary to use the other techniques, or how to do the benchmark myself. Oh, it's not too hard to do benchmarking, use cmpthese from Benchmark: http://search.cpan.org/~dapm/perl-5.10.1/lib/Benchmark.pm Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: print flips to scientific notation when integer exceeds 15 decimal digits
2009/11/4 David Lee david@ecmwf.int: Many thanks for the reply. Following the initial surprise, my main concern was that attempts to unearth a description or explanation (i.e. documentation) for the observed behaviour was so tricky. For instance, there was nothing obvious in the relevant parts of Programming Perl. I have a feeling this is somewhat deliberate. Scalars just do the Right Thing, mostly. You don't have to care about whether it's actually stored as an integer or a float *most of the time*. You only have to start caring when you need numbers with extreme values -- particularly anything = 2^31. Therefore perl documentation doesn't spend much time telling you about it. If you know or suspect that your number needs are somewhat more specialist, as they are here, then you shouldn't be using normal scalars but a specific type suitable for your needs. If you are unsure, it's better to just use such a datatype rather than check what the exact limits are in the documentation. I wouldn't mind a specific discussion of the issues somewhere in perldoc, but I wouldn't want it polluting perldoc perldata. But one problem with this is that it will always relate to system specific issues. For example, here's exactly the same perl program run on two different machines I have access to: H:\perl -e use integer; my $i = 997; print $i, q[ ], $i+4,qq[\n]; 997 3 p...@tui:~$ perl -e 'use integer; my $i = 997; print $i, q[ ], $i+4,qq[\n];' 997 1001 If you start writing code which depends on 64-bit native integers, like the above code does, you'll write non-portable Perl code. Better is to use bignums (or bigfloats) whenever you're in any doubt. So I'm happy to categorise it as self bug, although I'd like to include an element of documentation weakness in there and I'd be happy to assist someone in trying to improve the latter. Anyway, your explanation was useful and gives us sufficient to decide how to address our local use of these numbers. (In our case, they are human-oriented accumulated byte-counts, for which we don't actually need that significance/precision.) Really? If it's an accumulator, you can get into trouble: p...@tui:~$ perl -e 'my $i = 1e16; for (1..10) { printf %.17g\n,++$i; }' 1 1 1 1 1 1 1 1 1 1 remember even if you don't care about printing the right value, you have to ask if you need to store the right value. Better hope you're always adding large powers of two to that accumulator. And it's so easy to use a datatype which does the Right Thing, why not use it? Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Is Larry Wall member of this mailing list
2009/11/4 Parag Kalra paragka...@gmail.com: Are you this Larry Wall - http://en.wikipedia.org/wiki/Larry_Wall Yes I am and so's my wife. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: How do I pick one random element from an array?
2009/11/3 John W. Krahn jwkr...@shaw.ca: Majian wrote: my @array = ('uriel', 'daniel', 'joel', 'samuel'); Now what I want is create a process so every time I print the array it prints one element from the array . I wrote it like this : #!/usr/bin/perl -w use strict; use List::Util 'shuffle'; my @array = ('uriel', 'daniel', 'joel', 'samuel'); print Array before random: @array\n\n; print Array of random: $array[rand @array]\n; @array = shuffle @array; print Array of random: @array\n\n; That doesn't pick one random element from the array. That shuffles the whole array. I feel it's probably worth saying here that shuffling an array is an easy thing to get wrong -- ie if you try to roll your own subroutine its easy to accidentally make some permutations more likely than others. One example of how it can look right but not be right: sub badshuffle { my @array = @_ for my $i (0..$#array) { # For each element, randomly swap it with another element my $rand = rand @array; ($array[$i], $array[$rand]) = ($array[$rand], $array[$i]); } } This routine is not balanced. Some outcomes are more common than others, though I leave proof as an exercise to the reader. If you are interested in learning about doing shuffling yourself from an academic point of view, see: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle and pay particular attention to: http://en.wikipedia.org/wiki/Fisher%E2%80%93Yates_shuffle#Implementation_errors But if you just want to get it done or are writing production code, use List::Util::shuffle as John W Krahn says above. Don't roll your own in production code. Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Increment Operator (++) question in Perl
2009/11/3 Majian jian...@gmail.com: Hi ,all: When I test the increment operator in Perl and find a question : The question is : #!/usr/bin/perl use warnings; my $i = 1; print ++$i + ++$i, \n; The above code prints out the answer 6 . But in the other language the anser is 5 , So I don't understand the reason why it is Would someone give me some options ? From perldoc perlop: Note that just as in C, Perl doesn't define when the variable is incremented or decremented. You just know it will be done sometime before or after the value is returned. This also means that modifying a variable twice in the same statement will lead to undefined behaviour. Avoid statements like: 1. $i = $i ++; 2. print ++ $i + $i ++; Perl will not guarantee what the result of the above statements is. The problem is that you are trying to modify the same variable twice in the same statement. The solution is: don't do that then! Write it separately, to explicitly state what you mean: my $i = 1; $i++; $i++; print $i + $i, \n; # prints 6 or alternatively my $i = 1; my $j = ++$i; print ++$i + $j, \n; # prints 5 Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: print flips to scientific notation when integer exceeds 15 decimal digits
2009/11/3 David Lee david@ecmwf.int: Although I've used perl for many years, I've just been surprised (in the unpleasant sense) by a recent event. Given a variable, say $int, which is a growing integer, I would expect print $int to print it as a simple integer; indeed it usually does so. But when its size takes it from 15 decimal digits to 16 decimal digits, that print flips the output into scientific notation: 997 998 999 1e+15 1e+15 Ouch. The surprise is especially nasty when this output data is then used as input data for another program that expects an integer. This is consistent across a variety of OSes (although all perl 5.8.8). I eventually managed to track down a way to achieve the desired result with a non-obvious printf format. (I leave that, and its clear user-oriented explanation, as an exercise for the reader!) I'm not aware of any documentation about this surprise. Is this a program bug or a documentation bug? (Or a self bug?) I would guess that these numbers are being stored in floats, and that these floats are 64-bit double precision, with 53 bits of mantissa. That means that there are just under 16 decimal digits of precision in these numbers. print and friends seem to automatically print no more than 15 digits of precision: H:\perl -e my $i = 997; print $i, ' ',$i+4,qq[\n]; 997 1e+015 The first number is 15 digits long so it is printed normally. The second is 16 digits long, so the first 15 digits are printed in scientific notation. You probably want to use bignums instead. If you are dealing with large integers, where precision down to the units place is important, you definitely want bignums and not floats. H:\perl -e use bignum; my $i = 997; print $i, ' ',$i+4,qq[\n]; 997 1001 the bignum pragma makes $i into a Math::BigInt object rather than a floating-point value. Your solution (a non-obvious printf format) is a bad one because while it solves the problem of output, it doesn't solve the problem of representation. As soon as you try to store an integer bigger than 2^53 (approximately 9e15) you will lose significance: Here $i is a 15-digit number: H:\perl -e my $i = 997; printf qq[%.16g %.16g\n],$i, $i+4; 997 1001 So we've successfully printed 16 digits here, but when we try to go further and make $i a 16-digit number the maths starts to fail: H:\perl -e my $i = 9992; printf qq[%.16g %.16g\n],$i, $i+5; 9992 9996 2 + 5 = 6 according to this computer, because we've gone beyond the exactly representable range of the 64-bit floats on this machine. In answer to your question, the behaviour of print is probably the correct behaviour, since there's no point printing more precision than you store. So it's a self bug. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: foreach loop
2009/11/2 Thomas Bätzler t.baetz...@bringe.com: while( my $line = $file ){ snip } Won't this loop terminate early if there is a blank line or a line containing only '0'? If I do a readline loop I always do: while (defined ($line = $file)) Is there something magical happening here I don't know about? I know about the magical: while ($file) which is equivalent to: while (defined($_ = $file)) but I don't know of any magic in the while loop at the top. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Emulating Sort through Perl
2009/10/30 Jim Gibson jimsgib...@gmail.com: Approach 1. is called a stable sort. As you can see, Perl's sort gives you a stable sort, as the last two lines are in the order they appear in the original file. The Unix sort is not stable, so the lines with equal keys get reversed in the output. Whether or not you consider this a bug depends upon your application. You can generate a stable sort with the -s option to Unix sort (may depend upon your version). Note also that unix sort(1) provides this behaviour through the -s switch. So you have already emulated sort -s through perl! Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: compact my wordlist generator
2009/10/26 Michael Alipio daem0n...@yahoo.com: Then as suggested (see far below), a recursive function will do what I want.. After some googling I found this: permutate(0,2); sub permutate($$){ my ($cur,$max) = @_; if ($cur=$max){ print $result\n; return; } for(@word){ substr($result,$cur,1)=$_; perm($cur+1,$max); } } Can anyone tell me how the code above works? Yes, not very well: Undefined subroutine main::perm called at foo.pl line 14. ie you defined 'sub permutate' but you called 'perm()'. Please, please, post *working* code if you want us to help you. I know what's wrong and I can fix it myself, but you do yourself no favours by making those who would help you work harder. My original program must deal with arbitrary length and generate all the possible combinations (even repeating) of a particular set. What could take me gazillions of for loops for that, somebody just came up with less than ten lines. I can just copy and paste the code I found above but I want to learn how it works. How could someone write this code so easily but when I tried even writing just a pseudocode for it, my head almost exploded. I want to be a good programmer, but at this rate, I don't think I can be one if I will just keep copying and pasting someone else's code or downloading modules from CPAN. Can anyone teach me how my own code (the one with gazillion for loops), can be converted into a pseudocode then eventually into a working sub procedure? The code you found will teach you bad habits. Among other things: * it uses the package variable $result to communicate between recursive calls, rather than (in my version posted to the list a few days ago) in an extra argument. This is a Bad Thing. * it uses prototypes. Don't use prototypes; long story http://xrl.us/bffo9k ; short story: prototypes are not like C prototypes. They don't give you type safety. They don't force you to use a scalar where it expects a scalar argument - you can still provide an array or hash. And when you do, it will give you confusing, hard-to-track-down bugs. I would recommend you look instead at the code I posted to the list, and I'll be happy to explain it to you. As for how to think in recursion, I find the best way to think is this way: * How would I do it for a trivial case? * Given I can do it for a case of size N-1, how would I do it for a case of size N? * How do I express this in perl? As an example, the factorial function, the three steps become: * How do I calculate 0!? Answer: it is 1 by definition. * Given that I can calculate (N-1)!, how do I calculate N! ? Answer: I can calculate it thus: N * (N-1)! * How do I express this in perl? sub fact { my ($n) = @_; die Can't calculate factorial of a negative or fractional number if $n0; if ($n == 0) { # Base case return 1; } # Solve fact($n) given we can already do fact($n-1) return $n * fact ($n-1); } If you know your maths, this process is very similar to proof by induction. Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: compact my wordlist generator
2009/10/25 Michael Alipio daem0n...@yahoo.com: I'm trying to write a word list generator which can generate all possible combinations of n characters, within n set of characters. Have you looked into the magical autoincrement operator? http://perldoc.perl.org/perlop.html#Auto-increment-and-Auto-decrement 1. print ++($foo = '99'); # prints '100' 2. print ++($foo = 'a0'); # prints 'a1' 3. print ++($foo = 'Az'); # prints 'Ba' 4. print ++($foo = 'zz'); # prints 'aaa' my $x; my $len = 4; for ($x = 'a' x $len; $x ne 'a' x ($len+1); $x++) { print $x\n; } Or you could specify it as a range: my $len=4; for ('a'x$len..'z'x$len) { print $_\n; } These won't help with arbitrary character sets, unfortunately. Gabor wrote: nd the loops could be replaced by a recursive function call. Something like this: do_something_for_char($k) sub do_something_for_char { my ($k) = @_; return if $k = $n; do_something_for_char($n+1); } I would write it differently: sub print_all_strings_of_length { my ($len, $prefix) = @_; $prefix = defined($prefix) ? $prefix : q{}; # or $prefix //= q{}; under recent perls $len 0 and die qq{print_all_strings_of_length($len) can't be done}; if ($len == 0) { print $prefix\n; return; } for ('a'..'z') { # or for (@set) for an arbitrary set of characters print_all_strings_of_length($len-1, $prefix.$_); } return; } print_all_strings_of_length(3); For me, the idea of recursion is expressing an idea in terms of a slightly simpler idea. Here, I express how to print all strings of length N in terms of an operation which will print all strings of length N-1 prefixed with $prefix. So I use that operation to print all strings starting with 'a' followed by N-1 characters, then all strings starting 'b' followed by N-1 characters, and so on. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: newbie question : about the perl sprintf
2009/10/25 Majian jian...@gmail.com: I found these : perl -e'print 01.234 + 01.234', \n' print (01).(234+01).234, \n; this evaluates to '1'.'235'.'234' perl -e'print 01.234 + 011.234' \n' I didn't get 1235234, I got 1243234. print (01).(234+011).(234),\n evaluates to print '1'.(234+9).'234',\n; evaluates to print '1'.'243'.'234',\n; perl -e'print 01.234.12 + 01.234', \n' I'll let you work this one out as an exercise. The key point is that you can't have decimal octal numbers, so the . operator is interpreted as the string concatenation operator. Phil -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Convert Site Name to Dotted IP Address
2009/10/22 Anant Gupta anantgupta...@gmail.com: I wrote #!usr/bin/perl use Socket; use constant ADDR = 'www.google.com'; my $name=shift || ADDR; $packed=gethostbyname($name); $dotted-inet_ntoa($packed); print DOtted Address is $packed; but it is showing an error Bad argument length for Socket length in inet_ntoa ??? Help When I run your code, I don't get any such error. Are you sure that this is the code that produced the error? Further: $dotted-inet_ntoa($packed); did you mean $dotted = inet_ntoa($packed); (you used a - minus sign instead of a = assignment operator) print DOtted Address is $packed; did you mean print Dotted Address is $dotted\n; you were printing the wrong variable. With these changes, I get a dotted ip address which matches the output of host www.google.com Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Convert Site Name to Dotted IP Address
2009/10/22 Anant Gupta anantgupta...@gmail.com: Nopes, It is the same The '-' minus sign was my mistake. I am using this on Red Hat Linux, but I am not the root user. Will not being the root user make any difference? No, it won't make any difference. This is probably quite frustrating for you, but I'm not sure what I can do if the code works on my machine. I suggest you try the following: * use warnings and use strict * make the code simpler and simpler while still exhibiting the error * post the *simplest* *complete* program which demonstrates your problem * also post, verbatim, your command-line session demonstrating you running the program on your system. For example, here is a command-line session from my machine, where I first show a complete program (using 'cat foo.pl') and then run the program, listing all errors. p...@teach:~/tmp$ cat foo.pl use strict; use warnings; $foo = 34; print $foo\n; p...@teach:~/tmp$ perl foo.pl Global symbol $foo requires explicit package name at foo.pl line 4. Global symbol $foo requires explicit package name at foo.pl line 5. Execution of foo.pl aborted due to compilation errors. p...@teach:~/tmp$ Doing it this way means we know: * EXACTLY what you are running, because you have shown us a complete program and not a snippet from the middle --- Note that if you post a snippet rather than a complete program, you run the risk that the error is not contained within the snippet. * What perl itself thinks of your program, because you have used strict and warnings * The EXACT wording of the error message on your system * What your program does on our systems, because we can copy and paste it onto our own machine If you do this, we will have a much better chance of helping you. Unfortunately, there are still no guarantees :( Philip Thanks On Thu, Oct 22, 2009 at 4:33 PM, Philip Potter philip.g.pot...@gmail.com wrote: 2009/10/22 Anant Gupta anantgupta...@gmail.com: I wrote #!usr/bin/perl use Socket; use constant ADDR = 'www.google.com'; my $name=shift || ADDR; $packed=gethostbyname($name); $dotted-inet_ntoa($packed); print DOtted Address is $packed; but it is showing an error Bad argument length for Socket length in inet_ntoa ??? Help When I run your code, I don't get any such error. Are you sure that this is the code that produced the error? Further: $dotted-inet_ntoa($packed); did you mean $dotted = inet_ntoa($packed); (you used a - minus sign instead of a = assignment operator) print DOtted Address is $packed; did you mean print Dotted Address is $dotted\n; you were printing the wrong variable. With these changes, I get a dotted ip address which matches the output of host www.google.com Philip -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/