Re: Free PERL Editor
Suja Emmanuel wrote: Hi all, Could you please suggest me any good PERL editor which is available in open source tools. I have tried 'EngineSite Perl Editor -LITE' and 'PerlExpress' which I am not comfortable with. Kindly refer me some good bug free tools. Eclipse IDE with the EPIC plugin. It's a little rough around the edges, but it's better than most. For more polish, see ActiveState's Komodo ... though it's not free. Dan -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Sorting from subroutine call
Hi all! I would like to know if it's possible to make an array sorting with a subroutine call. Usually, a sort is made as, for example: sort {$a = $b} @array; But my intention is something like: sort subroutine_call @array; sub subroutine { $a = $b; } How could I do it? Thanks. Sergio.
Re: Sorting from subroutine call
Sergio Escalada wrote: Hi all! I would like to know if it's possible to make an array sorting with a subroutine call. Usually, a sort is made as, for example: sort {$a = $b} @array; But my intention is something like: sort subroutine_call @array; If you really want to call it like this (without assignment to an array), you'll need to use an array reference: # declare prototype *before* calling it sub sort_numerical ($); sort_numerical [EMAIL PROTECTED]; print @array; sub sort_numerical ($) { my $arrayref = shift; @{ $arrayref } = sort {$a = $b} @{ $arrayref }; } I think this is ugly; it looks like the sub takes a scalar argument (a reference is a scalar, but not all scalars are arrayrefs). This will also work: my @array = qw(3 2 1); sub sort_numerical (@); print sort_numerical @array; sub sort_numerical (@) { return sort {$a = $b} @_; } Personally, I'd drop the prototype and go for: my @array = qw(3 2 1); print sort_numerical( @array ); sub sort_numerical { return sort {$a = $b} @_; } HTH WayPay -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Sorting from subroutine call
Hi all! I would like to know if it's possible to make an array sorting with a subroutine call. Usually, a sort is made as, for example: sort {$a = $b} @array; But my intention is something like: sort subroutine_call @array; sub subroutine { $a = $b; } How could I do it? perldoc -f sort. Look at the very first example. -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g Computer software consists of only two components: ones and zeros, in roughly equal proportions. All that is required is to sort them into the correct order. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Sorting from subroutine call
Sergio Escalada wrote: Hi all! I would like to know if it's possible to make an array sorting with a subroutine call. Usually, a sort is made as, for example: sort {$a = $b} @array; But my intention is something like: sort subroutine_call @array; sub subroutine { $a = $b; } How could I do it? Hello Sergio It will work almost exactly as you have written it. Just use the name of the subroutine in the call to sort: sort subroutine @array; sub subroutine { $a = $b; } HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Sorting from subroutine call
Sergio Escalada wrote: Hi all! I would like to know if it's possible to make an array sorting with a subroutine call. Usually, a sort is made as, for example: sort {$a = $b} @array; But my intention is something like: sort subroutine_call @array; sub subroutine { $a = $b; } How could I do it? Hello Sergio It will work almost exactly as you have written it. Just use the name of the subroutine in the call to sort: sort subroutine @array; sub subroutine { $a = $b; } HTH, Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Sorting from subroutine call
Thanks for replies. The purpouse of this mini-script is to list the rows from a database loaded in memory ($ref_db is the reference to hashtable that cotains the DB). So I want to order the fields by different sort rules, and make the proccess as abstract as it's possible with a subrutine (sub cmpRule). This sub must recieve the rules (by array argument, for example), and create the body that will be called by sort. This is what I've done since I wrote the message (now, it only works with one rule, but I think it's easy to do multiple-rules sort from here) The comparation subroutine: sub cmpRule { return '$$ref_db{$a}{$$ref_fields{$opt}[0]}'. ' cmp $$ref_db{$b}{$$ref_fields{$opt}[0]}'; } ...#some code my $func = cmpRule; foreach my $row (sort {eval($func)}keys %{$ref_db}){ ...#some code } It works but, do you think it's a good solution? Thanks for your time. Sergio.
Re: Sorting from subroutine call
On 12/2/06, Sergio Escalada [EMAIL PROTECTED] wrote: The purpouse of this mini-script is to list the rows from a database loaded in memory ($ref_db is the reference to hashtable Another idea - sub sortrows { my $sorted = @_; $sorted = -(($a-{ahash} eq 'x') = ($b-{ahash} eq 'x')) if $sorted == 0; $sorted = (lc($a-{string}) cmp lc($b-{string})) if $sort == 0; $sorted; } foreach $row (sort sortrows @$rows) { ... blah blah ... -- WC (Bill) Jones -- http://youve-reached-the.endoftheinternet.org/ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Sorting from subroutine call
The purpouse of this mini-script is to list the rows from a database loaded in memory ($ref_db is the reference to hashtable that cotains the DB). So I want to order the fields by different sort rules, and make the proccess as abstract as it's possible with a subrutine (sub cmpRule). This sub must recieve the rules (by array argument, for example), and create the body that will be called by sort. [deletia...] It works but, do you think it's a good solution? I think that was a highly suboptimum solution. #!/usr/bin/perl use strict; use warnings; # # hashref? Why in the WORLD is the database being kept in a hashref? # my $data_for = { apple = { color = 'verde', name = 'manzana', texture = 'crujiente' }, banana = { color = 'amarillo', name = 'platano', texture = 'blando' }, strawberry = { color = 'roja', name = 'fresa', texture = 'blanda' }, }; # # if you have a small number of columns you want to sort by, build a # simple subroutine to sort by that column -- this is *FAR SUPERIOR* # to building a throw-away subroutine and eval-ing it at run time. # sub by_color { return $a-{color} cmp $b-{color} }; sub by_name { return $a-{name} cmp $b-{name} }; sub by_texture { return $b-{texture} cmp $b-{texture} }; # # now, pass into sort a subref for the sort function to use # foreach my $sortkey (\by_color, \by_name, \by_texture) { print \n\n; foreach my $row (sort $sortkey values %$data_for ) { print $row-{color} - $row-{name} - $row-{texture}\n; } } # # if you have hundreds of fields you want to sort by, then you should # do this slightly uglier version (uglier because it uses a # package-global to hold the current sort column # our $current_sort_field; sub by_something { return $a-{$current_sort_field} cmp $b-{$current_sort_field}; } foreach $current_sort_field (qw/ color name texture /) { print \n\nSorting by $current_sort_field\n; foreach my $row (sort by_something values %$data_for ) { print $row-{color} - $row-{name} - $row-{texture}\n; } } -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- -- Lawrence Statton - [EMAIL PROTECTED] s/aba/c/g Computer software consists of only two components: ones and zeros, in roughly equal proportions. All that is required is to sort them into the correct order. -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Limit of number of files that can be opened in perl
On 12/1/06, Ravi Malghan [EMAIL PROTECTED] wrote: Hi: is there a limit on number of files that can be open within perl. Some systems use ulimit to put an upper limit on the number of open files. And I believe that at least some perl releases have an overall limit of 128 files at once. I am opening about 194 files and am seeing some weird behaviour. When i write to the filehandles, I see it writes ok to some files and not to the others. But I donot see any errors either. How odd. Did you check for errors upon every open()? Perl should (if warnings are enabled) warn you if you're using a broken filehandle. You probably want the FileCache module, which lets your Perl code pretend many files are open; it opens and closes filehandles as needed so that your OS sees only a few files in use at any one time. use FileCache maxopen = 16; Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Net::EasyTCP
--- zentara [EMAIL PROTECTED] wrote: On Fri, 1 Dec 2006 20:31:11 -0800 (PST), [EMAIL PROTECTED] (Derek B. Smith) wrote: I was hoping for socket data transfers to mimic an sftp/ftp get without having to deploy code to the clients and or deploying this module on the clients. Think about it, how could that work? You need some sort of code on the clients, whether it's your custom script, or existing server code. Do the clients run a web server? You could place the files in a htaccess password protected directory, and get them thru https? There are many ways to go, http, ftp, ssh2, etc. You don't have to install the Net::EasyTCP module on the clients. There is no xs component, it's pure perl. So you could actually include the EasyTCP code, as a package right into your script. zentara ok thanks 4 the advise, but I have thought about it. All the clients do not have the same access routes. For example, some have ssh turned on while others do not. Those that do not, have ftp and the majority of all the clients do not allow root login over ssh. So now u see my dilemma... I have begun to use an scp script, but I knew there was a way to use sockets to xfer files so I thought I would learn something new while I was getting all the files together. I dont understand there is no xs component and I dont understand So you could actually include the EasyTCP code, as a package right into your script. Will u explain? derek -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Sorting from subroutine call
On 12/02/2006 06:22 AM, Sergio Escalada wrote: Hi all! I would like to know if it's possible to make an array sorting with a subroutine call. Usually, a sort is made as, for example: sort {$a = $b} @array; But my intention is something like: sort subroutine_call @array; sub subroutine { $a = $b; } How could I do it? Thanks. Sergio. You can do that. The Perl documentation on your system probably tells you how. If you have ActiveState Perl, do this: Start-Run-perldoc -f sort Under a un*x perl, just do this at a terminal: perldoc -f sort -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Modifying a PNG File
Hello, Is there a (relatively simple) way to modify an image in Perl? Ideally, this would be a PNG or GIF image. I just need to be able to modify the colors of specific pixels. Thanks in advance! -- Leonid Grinberg [EMAIL PROTECTED] -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Limit of number of files that can be opened in perl
Hi all: thanks for the responses. I should have done this in the beginning. I checked the status open returns. I try to open 388 files, but it returned true(1) only 249 times (for the first 249 opens). So I guess thats the limit. Also to make sure I am doing the right way, the following is how I am opening the files $AgentFH{$id} = *$name; open($AgentFH{$id}, $filename); I am speaking to the system admin if he can bump up the limit to 1024. Any other thoughts? TIA Ravi - Original Message From: Tom Phoenix [EMAIL PROTECTED] To: Ravi Malghan [EMAIL PROTECTED] Cc: beginners@perl.org Sent: Saturday, December 2, 2006 10:54:05 AM Subject: Re: Limit of number of files that can be opened in perl On 12/1/06, Ravi Malghan [EMAIL PROTECTED] wrote: Hi: is there a limit on number of files that can be open within perl. Some systems use ulimit to put an upper limit on the number of open files. And I believe that at least some perl releases have an overall limit of 128 files at once. I am opening about 194 files and am seeing some weird behaviour. When i write to the filehandles, I see it writes ok to some files and not to the others. But I donot see any errors either. How odd. Did you check for errors upon every open()? Perl should (if warnings are enabled) warn you if you're using a broken filehandle. You probably want the FileCache module, which lets your Perl code pretend many files are open; it opens and closes filehandles as needed so that your OS sees only a few files in use at any one time. use FileCache maxopen = 16; Hope this helps! --Tom Phoenix Stonehenge Perl Training Want to start your own business? Learn how on Yahoo! Small Business. http://smallbusiness.yahoo.com/r-index -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Sorting from subroutine call
# # hashref? Why in the WORLD is the database being kept in a hashref? # Oh, it's an exercise for class, and I must keep data in a hashtable, it's not my fault ^_^ Thanks for your code :) # # if you have a small number of columns you want to sort by, build a # simple subroutine to sort by that column -- this is *FAR SUPERIOR* # to building a throw-away subroutine and eval-ing it at run time. # Sorry if I'm wrong, but it seems that you sort by single columns, and I want to sort by multiple columns. When I run the script, I want to be free to do personal sorts, like first order by color, then by texture, and then by name at run time. I want an abstract solution, because I want to cover all the possibilities... And if I've got lots of columns, I would like not to be forced to do a subroutine for each column, adding other lots of subroutines covering the different possibilities between each column . I have 14 columns in my database... if I do like you said, I have to code... 14! = 87,000 million subs... That's a lot of subs to code xD. This is why I've proposed that solution, I want a sort subroutine independent of number of columns that covers ALL the possibilities of sort using ANY SET of columns in ANY order. That's my problem ;-) So, Is there an alternative and more elegant way than mine to do it? (Be patient, I'm so noob :P) Thanks. Sergio.
Re: Sorting from subroutine call
Sergio Escalada am Samstag, 2. Dezember 2006 15:41: Thanks for replies. The purpouse of this mini-script is to list the rows from a database loaded in memory ($ref_db is the reference to hashtable that cotains the DB). So I want to order the fields by different sort rules, and make the proccess as abstract as it's possible with a subrutine (sub cmpRule). This sub must recieve the rules (by array argument, for example), and create the body that will be called by sort. You don't specify the exact requirements or data structures; one way to do it in an abstract way is presented below. I wrote this script from scratch; it *seems* to do what it should. The idea is as follows: 1. According to perldoc -f sort, it's possible to give a code block (delivering a subroutine reference) as argument to sort that does the actual sort. 2. We don't code these different possible sorting subroutines explicitly, since their number may be high (sort by one ore more fields, in different order, ascending/descending, numeric/string sort - in different combinations. Instead, we code an abstract subroutine (sort_sub_factory) that returns sorting subroutines created according to some rules. The rules can be formulated in an easy way. 3. Every sorting routine craeated assumes the following data structure of the db data (compare with the test data in the script): $ref_db is a reference to an array (each array represents a database record). The elements of the array contain hashrefs with (fieldname, fieldvalue) pairs. You can say for example: Sort first by field2 (ascending, numerical sort), then by field1 (descending, string sort). All combinations are possible. You express above rules by my @rules=( ['field2', 0, 0], ['field1, 1, 1] ); I hope this helps :-) Dani This is what I've done since I wrote the message (now, it only works with one rule, but I think it's easy to do multiple-rules sort from here) The comparation subroutine: sub cmpRule { return '$$ref_db{$a}{$$ref_fields{$opt}[0]}'. ' cmp $$ref_db{$b}{$$ref_fields{$opt}[0]}'; } ...#some code my $func = cmpRule; foreach my $row (sort {eval($func)}keys %{$ref_db}){ ...#some code } It works but, do you think it's a good solution? Thanks for your time. Sergio. #!/usr/bin/perl ### THIS IS BETA SOFTWARE ### use strict; use warnings; # @rules is a list of arrayrefs. Each array element contains the # informations for one db field: # # [fieldname, desc_sort_bool, string_sort_bool]. # - sort descending if desc_sort_bool is true # (else ascending) # - sort via string comparison if string_sort_bool is true # (else numerically) # # If @rules contains more than one element, the sorting is nested. # # ***BEWARE***: Sanitize all arguments before using in this sub! # sub sort_sub_factory { my @[EMAIL PROTECTED]; my @sub_code_parts; foreach my $rule (@rules) { my ($field, $desc, $string)[EMAIL PROTECTED]; # handle boolean sort options # my $comp_op=$string ? 'cmp' : '='; my ($a_var, $b_var)=$desc ? (qw/$b $a/) : (qw/$a $b/); # create subroutine code parts # push @sub_code_parts, '('.$a_var.'-{'.$field.} $comp_op .$b_var.'-{'.$field.})\n; } # put all parts together, producing source code for sorting subroutine # my $sub_code= sub { return @{[join ' || ', @sub_code_parts]} }; # just for debugging, output created source code: # warn \n\n\nGenerated code:\n, $sub_code, \n; return eval $sub_code; } ### ### TEST of above code ### # helper sub to display sorted data # sub debug_print { my ($title, $sorted_ref_db)[EMAIL PROTECTED]; print $title:\n; foreach my $record_hr (@$sorted_ref_db) { print join ', ', map {$_ = $record_hr-{$_}} sort keys %$record_hr; print \n; } } # our test data # my $ref_db=[ {f1='x', f2=2, f3='Q'}, {f1='x', f2=1, f3='A'}, {f1='x', f2=2, f3='A'}, {f1='x', f2=1, f3='Q'}, {f1='b', f2=2, f3='Q'}, {f1='a', f2=10,f3='A'}, {f1='a', f2=40,f3='C'}, {f1='x', f2=1, f3='X'}, ]; # Several test sortings: # my ($title, $sort_sub); $title ='by (f1, ascending, string comparison)'; $sort_sub=sort_sub_factory(['f1', 0, 1]); debug_print ($title, [sort $sort_sub @$ref_db]); $title ='by (f1, ascending, string comparison)' . '(f2, ascending, numeric comparison)'; $sort_sub=sort_sub_factory(['f1', 0, 1], ['f2', 0, 0]); debug_print ($title, [sort $sort_sub @$ref_db]); $title ='by (f3, ascending, string comparison)' . '(f2, descending, numeric comparison)'; $sort_sub=sort_sub_factory(['f3', 0, 1], ['f2', 1, 0]); debug_print ($title, [sort $sort_sub @$ref_db]); $title ='1st invalid request'; $sort_sub=sort_sub_factory(['f3', 1, 0]); debug_print ($title, [sort $sort_sub @$ref_db]); $title ='2nd invalid request'; { local $SIG{__WARN__}=sub {print @_; die INVALID sort sub '$title'!};
Re: Re: Limit of number of files that can be opened in perl
Ravi, Please don't top post. On 12/2/06, Ravi Malghan [EMAIL PROTECTED] wrote: Hi all: thanks for the responses. I should have done this in the beginning. I checked the status open returns. I try to open 388 files, but it returned true(1) only 249 times (for the first 249 opens). So I guess thats the limit. Don't count on it. The limit (as reported by ulimit) is 256. You can open 249 because you already have 6 files/file descriptors open. That may not always be the case. Modules, for instance, may open files, descriptors, or pipes that you aren't aware of. Also to make sure I am doing the right way, the following is how I am opening the files $AgentFH{$id} = *$name; open($AgentFH{$id}, $filename); If you're using a recent version of Perl, open will accept a scalar as an indirect filehandle: open($name, , $filename); Then you can take a reference to the handle and store it in your hash. That's probably better than globbing first, because you won't need too do the assignment if the open fails. Also, open accepts an undefined scalar as a filehandle ref, so your code isn't doing quite what you think it is. It's being parsed as something equivalent to open(ref($AgentFH{$id}) ...); Which appears to work because $AgentFH{$id} will always be unique, but it isn't what you think it is, and may behave unexpectedly. finally, always check the return value of open, and behave appropritely if it fails: open($name, , $filename) or die $!\n; # or open($name, , $filename) or your_error_sub($!); # or something like that I am speaking to the system admin if he can bump up the limit to 1024. Any other thoughts? Yes. See Tom's suggestion about the FileCache CPAN module. you might also be able to fool the system by forking a number of child processes to handle the IO. Each one could handle, say, 100 files. FileChache is simpler, though. HTH, -- jay -- This email and attachment(s): [ ] blogable; [ x ] ask first; [ ] private and confidential daggerquill [at] gmail [dot] com http://www.tuaw.com http://www.downloadsquad.com http://www.engatiki.org values of β will give rise to dom!
[OT] Re: Net::EasyTCP
Derek B. Smith am Samstag, 2. Dezember 2006 17:08: --- zentara [EMAIL PROTECTED] wrote: On Fri, 1 Dec 2006 20:31:11 -0800 (PST), [EMAIL PROTECTED] (Derek B. Smith) wrote: I was hoping for socket data transfers to mimic an sftp/ftp get without having to deploy code to the clients and or deploying this module on the clients. Think about it, how could that work? You need some sort of code on the clients, whether it's your custom script, or existing server code. Do the clients run a web server? You could place the files in a htaccess password protected directory, and get them thru https? There are many ways to go, http, ftp, ssh2, etc. You don't have to install the Net::EasyTCP module on the clients. There is no xs component, it's pure perl. So you could actually include the EasyTCP code, as a package right into your script. zentara Hello Derek (and I hope it's ok for you zentara when I answer [too]) ok thanks 4 the advise, but I have thought about it. All the clients do not have the same access routes. For example, some have ssh turned on while others do not. Is it possible that you mean sshd (ssh *server*) by ssh turned on? Those that do not, have ftp and the majority of all the clients do not allow root login over ssh. Do they, on the other side, have installed an ssh *client*? So now u see my dilemma... I have begun to use an scp script, but I knew there was a way to use sockets to xfer files so I thought I would learn something new while I was getting all the files together. As far as I could follow this thread, you have to install something *anyway* on some (or even all - Net::EasyTCP) client boxes. I dont understand there is no xs component and I dont understand So you could actually include the EasyTCP code, as a package right into your script. I think zentara meant that it's sufficient to 'copy over' perl script/modules not involving compiling/installing/using software parts based on C. Some perl modules implement their functionality in C. The glue between perl and the C code is called 'XS' (hm, more or less at least). Have a look at XML::LibXML for example, that uses the libxml2 library. === My advice to you is to present your requirements to the secureshell and/or a security ML, and not yet thinking about which perl module to use. Assume that a box does not allow remote logins (could be, according to your descriptions). Now you want to bypass these restrictions only to transfer a file? I doubt this being a good idea. A more secure plan (in my eyes not belonging to a security guru) could be to let the clients initiate the file transfer. You'd have to run an sshd server on your main box. There are several possibilities to customize and secure ssh(d). Referring to another answer to one of my posts: Did you consider permissions of parent directories, the presence of a sniffer in your multifirewalled network, and other worst case scenarios? Did you analyse the risks involved throughly? These are all important non-perl-related questions that have earnestly to be taken into accound before anything else. Please somebody correct me if I'm wrong. Dani -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Sorting from subroutine call
Yes! It was exactly what I was trying to do. I wasn't so wrong after all. Thanks you, Dani, your code helps me a lot :D 2006/12/2, D. Bolliger [EMAIL PROTECTED]: Sergio Escalada am Samstag, 2. Dezember 2006 15:41: Thanks for replies. The purpouse of this mini-script is to list the rows from a database loaded in memory ($ref_db is the reference to hashtable that cotains the DB). So I want to order the fields by different sort rules, and make the proccess as abstract as it's possible with a subrutine (sub cmpRule). This sub must recieve the rules (by array argument, for example), and create the body that will be called by sort. You don't specify the exact requirements or data structures; one way to do it in an abstract way is presented below. I wrote this script from scratch; it *seems* to do what it should. The idea is as follows: 1. According to perldoc -f sort, it's possible to give a code block (delivering a subroutine reference) as argument to sort that does the actual sort. 2. We don't code these different possible sorting subroutines explicitly, since their number may be high (sort by one ore more fields, in different order, ascending/descending, numeric/string sort - in different combinations. Instead, we code an abstract subroutine (sort_sub_factory) that returns sorting subroutines created according to some rules. The rules can be formulated in an easy way. 3. Every sorting routine craeated assumes the following data structure of the db data (compare with the test data in the script): $ref_db is a reference to an array (each array represents a database record). The elements of the array contain hashrefs with (fieldname, fieldvalue) pairs. You can say for example: Sort first by field2 (ascending, numerical sort), then by field1 (descending, string sort). All combinations are possible. You express above rules by my @rules=( ['field2', 0, 0], ['field1, 1, 1] ); I hope this helps :-) Dani This is what I've done since I wrote the message (now, it only works with one rule, but I think it's easy to do multiple-rules sort from here) The comparation subroutine: sub cmpRule { return '$$ref_db{$a}{$$ref_fields{$opt}[0]}'. ' cmp $$ref_db{$b}{$$ref_fields{$opt}[0]}'; } ...#some code my $func = cmpRule; foreach my $row (sort {eval($func)}keys %{$ref_db}){ ...#some code } It works but, do you think it's a good solution? Thanks for your time. Sergio. #!/usr/bin/perl ### THIS IS BETA SOFTWARE ### use strict; use warnings; # @rules is a list of arrayrefs. Each array element contains the # informations for one db field: # # [fieldname, desc_sort_bool, string_sort_bool]. # - sort descending if desc_sort_bool is true # (else ascending) # - sort via string comparison if string_sort_bool is true # (else numerically) # # If @rules contains more than one element, the sorting is nested. # # ***BEWARE***: Sanitize all arguments before using in this sub! # sub sort_sub_factory { my @[EMAIL PROTECTED]; my @sub_code_parts; foreach my $rule (@rules) { my ($field, $desc, $string)[EMAIL PROTECTED]; # handle boolean sort options # my $comp_op=$string ? 'cmp' : '='; my ($a_var, $b_var)=$desc ? (qw/$b $a/) : (qw/$a $b/); # create subroutine code parts # push @sub_code_parts, '('.$a_var.'-{'.$field.} $comp_op .$b_var.'-{'.$field.})\n; } # put all parts together, producing source code for sorting subroutine # my $sub_code= sub { return @{[join ' || ', @sub_code_parts]} }; # just for debugging, output created source code: # warn \n\n\nGenerated code:\n, $sub_code, \n; return eval $sub_code; } ### ### TEST of above code ### # helper sub to display sorted data # sub debug_print { my ($title, $sorted_ref_db)[EMAIL PROTECTED]; print $title:\n; foreach my $record_hr (@$sorted_ref_db) { print join ', ', map {$_ = $record_hr-{$_}} sort keys %$record_hr; print \n; } } # our test data # my $ref_db=[ {f1='x', f2=2, f3='Q'}, {f1='x', f2=1, f3='A'}, {f1='x', f2=2, f3='A'}, {f1='x', f2=1, f3='Q'}, {f1='b', f2=2, f3='Q'}, {f1='a', f2=10,f3='A'}, {f1='a', f2=40,f3='C'}, {f1='x', f2=1, f3='X'}, ]; # Several test sortings: # my ($title, $sort_sub); $title ='by (f1, ascending, string comparison)'; $sort_sub=sort_sub_factory(['f1', 0, 1]); debug_print ($title, [sort $sort_sub @$ref_db]); $title ='by (f1, ascending, string comparison)' . '(f2, ascending, numeric comparison)'; $sort_sub=sort_sub_factory(['f1', 0, 1], ['f2', 0, 0]); debug_print ($title, [sort $sort_sub @$ref_db]); $title ='by (f3, ascending, string comparison)' . '(f2, descending, numeric comparison)'; $sort_sub=sort_sub_factory(['f3', 0, 1], ['f2', 1, 0]); debug_print ($title, [sort $sort_sub @$ref_db]); $title ='1st invalid request'; $sort_sub=sort_sub_factory(['f3', 1, 0]); debug_print
Re: Modifying a PNG File
On 12/2/06, Leonid Grinberg [EMAIL PROTECTED] wrote: Is there a (relatively simple) way to modify an image in Perl? Sure; use a module from CPAN. Ideally, this would be a PNG or GIF image. I just need to be able to modify the colors of specific pixels. Probably GD can do what you want; Image::Magick seems like overkill. http://search.cpan.org/modlist/Graphics Hope this helps! --Tom Phoenix Stonehenge Perl Training -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Modifying a PNG File
Leonid Grinberg wrote: Hello, Is there a (relatively simple) way to modify an image in Perl? Ideally, this would be a PNG or GIF image. I just need to be able to modify the colors of specific pixels. Thanks in advance! Hello Leonid The GD module will do what you want. It will do a lot more besides, but you will find that with all the graphics modules that I know about. It's an interface to the GD graphics library, and so needs that installed as well. Come back here if you need any help installing it or using it. Rob -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Limit of number of files that can be opened in perl
On 12/01/2006 08:46 PM, Ravi Malghan wrote: Hi: is there a limit on number of files that can be open within perl. I am opening about 194 files and am seeing some weird behaviour. When i write to the filehandles, I see it writes ok to some files and not to the others. But I donot see any errors either. Just that somefiles donot have what I am expecting. If there is a limit, how do I work around it? Thanks Ravi Do you Yahoo!? Everyone is raving about the all-new Yahoo! Mail beta. http://new.mail.yahoo.com Many others have given you solutions to your problem: increasing the ulimit and using the FileCache module. I'm curious; why do you need to open so many files at the same time? -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
RE: Limit of number of files that can be opened in perl
From: Ravi Malghan bash-2.05$ ulimit -a core file size (blocks) unlimited data seg size (kbytes) unlimited file size (blocks) unlimited open files 256 pipe size (512 bytes) 10 stack size (kbytes) 8192 cpu time (seconds) unlimited max user processes 29995 virtual memory (kbytes) unlimited Looks like I can open only 256 files. Anyway I can get around within perl without changing the actual limit on the operating system. No, the OS controls this. Depending on what the hard limit is, you may be able to increase this yourself. If the hard limit is also 256 then ask your SA to increase that. As mentioned before: man ulimit. -- This email is confidential and may be legally privileged. It is intended solely for the addressee. Access to this email by anyone else, unless expressly approved by the sender or an authorized addressee, is unauthorized. If you are not the intended recipient, any disclosure, copying, distribution or any action omitted or taken in reliance on it, is prohibited and may be unlawful. If you believe that you have received this email in error, please contact the sender, delete this e-mail and destroy all copies. == -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response
Re: Checking for infinite loops
Thanks, I suppose I understand that code example from 'perldoc -f alarm' a little better. But much of it remains mysterious. e.g. the very first thing within eval. The only brackets I've ever seen with variables are [] for list elements. What's going on with {}? And what a strange thing to set a variable to - seems to be neither string nor number, but a subroutine? And why would you have a subroutine with just one line? And how can you have a subroutine without a name? And without a call to it? Where is the thing being timed? I understand something is being given 5 seconds, but what? Why is the variable $SIG{ALRM} not used again? Is there some significance to the name of that variable? In 'perldoc -f alarm' there's mention of a SIGALRM, but I don't know what that is. But I think we can ignore all those questions, because I don't see a need to work with this example. I'm just looking for someone to tell me how alarm works. A few sentences in English will be fine. No code really need be written. Fred D. Bolliger [EMAIL PROTECTED] wrote: hOURS am Donnerstag, 30. November 2006 21:09: Jen Spinney wrote: On 11/20/06, hOURS wrote: Recently I posed a question on here regarding a program I have that runs other programs (through require statements and what not). My problem was that the programs getting run might have syntax errors and I wanted to skip over each of those and go onto the next one. We figured out a way to handle that. It turns out however, that these programs sometimes have an even more troublesome problem: infinite loops. I knew about this possibility, but figured I would just use the time function, and if a program was taking to long, skip over it. Yeah, that wasn't so smart. I can't have the main program check the elapsed time while the required program is running its infinite loop. Or can I somehow? Any ideas anybody? Thank you. Fred Kittelmann Fred, Have you checked out the alarm function? I'm a beginner myself and I had a similar problem earlier today. alarm seemed to do it for me. Good luck! - Jen Thanks Jen, I've checked out alarm as much as I can. My PERL textbook scarcely mentions it. Trying perldoc -f alarm was a little more informative, but I still don't understand how to use this. Can anyone explain it to me? Fred Does the following modified code example from 'perldoc -f alarm' helps? Dani #!/usr/bin/perl use strict; use warnings; my $timeout=5; # secs eval { # Assign a signal handler subroutine which is invoked in case # the alarm signal is sent # local $SIG{ALRM} = sub { die alarm\n }; # NB: \n required # send an alarm signal after $timeout seconds! # alarm $timeout; # to test a non-timeout die, uncomment following line: #die; # the problem: # endless_loop(); # reset alarm timer: Don't send alarm signal any more # alarm 0; }; # Check if the code within eval died because of an alarm signal # or something else. We check the die message for that. # if ($@) { if ($@ eq alarm\n) { warn endless_loop() interrupted after timeout\n; } else { warn code in eval died!\n; die; } } warn program continues...\n; sub endless_loop { {} while 1 } __END__ -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] - Need a quick answer? Get one in minutes from people who know. Ask your question on Yahoo! Answers.
Help with WWW::Mechanize - Next Question
With all the help I've received I've been able to get this working. This is my text: #!/usr/bin/perl use warnings; use strict; use WWW::Mechanize; use HTML::TokeParser; my $username = 'msnyder'; my $password = 'xxx'; my $status = 'open'; my $agent = WWW::Mechanize-new(); $agent-get('https://rt.ops.xxx.com/'); $agent-submit_form( form_name = 'login', fields= { 'user' = $username, 'pass' = $password, } ); $agent-follow_link(text = Tickets); $agent-submit_form( form_name = 'BuildQuery', fields= { 'ValueOfStatus' = $status, 'ValueOfActor' = $username, }, button= 'DoSearch' ); my $data = $agent-content(); print $data; What this will do is return to me HTML source with a list of work tickets and all pertinent, associated data. The purpose of setting this up is to allow me to pull out email addresses of any work ticket created as a result of spam. For anyone not familiar with Request Tracker from Best Practical Solutions, the 'from' email address on any incoming email received by Request Tracker is automatically turned into a user account. With the amount of spam flying around the the Net these days those user accounts add up. All those spam tickets are assigned to me so I can eliminate them and the users created as a result of them from our database. My goal is to parse $data to pull out all the email addresses which I will then sift through to remove any legitimate addresses. You'll notice I declare the use of HTML::TokeParser. This leads to my next question. Do I need to use that? Would it be simpler to just parse the data matching against a regex and put any matches into a file? I imagine I don't need to sift through all the HTML tags just to get to the email addresses since they are fairly easy to spot. Mathew -- To unsubscribe, e-mail: [EMAIL PROTECTED] For additional commands, e-mail: [EMAIL PROTECTED] http://learn.perl.org/ http://learn.perl.org/first-response