Re: Bit testing
Hi Charles, On Sunday 14 November 2010 01:47:36 C.DeRykus wrote: On Nov 11, 11:27 pm, c...@pobox.com (Chap Harrison) wrote: Not lots shorter but you could use a closure to hide the calculation: my $mask; for my $flags ( ... ) { $mask = sub { return ($flags $_[0]) == $_[0] } unless $mask; given( $flags ) { when ( $mask-($one_and_three) ) { ... } when ( $mask-($zero_and_four) ) { ... } ... } } This won't work properly because the closure traps the initial value of $flags. For example: [code] #!/usr/bin/perl use strict; use warnings; my $closure; foreach my $name (qw(Sophie Jack Charles Dan Rachel)) { $closure = sub { print Hello $name!\n ; } unless $closure; $closure-(); } [/code] This prints Hello Sophie! five times. Either redeclare the closure on every iteration, or declare it once while using a more outer lexical variable. Regards, Shlomi Fish -- Charles DeRykus -- - Shlomi Fish http://www.shlomifish.org/ List of Portability Libraries - http://shlom.in/port-libs rindolf She's a hot chick. But she smokes. go|dfish She can smoke as long as she's smokin'. Please reply to list if it's a mailing list post - http://shlom.in/reply . -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Bit testing
On Nov 13, 3:47 pm, dery...@gmail.com (C.DeRykus) wrote: On Nov 11, 11:27 pm, c...@pobox.com (Chap Harrison) wrote: I'm almost embarrassed to ask this, but I can't figure out a simple way to construct a switch ('given') statement where the 'when' clauses involve bit-testing. Here's the only way I've figured out to build a switch statement that does the trick. It seems unusually wordy, which makes me think there must be a simpler way to test for certain bit combinations. Any suggestions? Thanks, Chap #!/usr/bin/perl use strict; use warnings; use feature :5.10; # Here are masks for various bit combos of interest: my $one_three = 0b1010; # bits 1 and 3 (counting from 0, right to left) my $zero_four = 0b00010001; # bits 0 and 4 my $five = 0b0010; # bit 5 # Here we will test several bit fields for bit combos of interest: for my $flags ( 0b10111010, 0b10111000, 0b10010010) { my $asbits = sprintf(0b%08b, $flags); # prepare bits for pretty-printing given ( $flags ) { when ( ($_ $one_three) == $one_three ) { # bits one and three are on say $asbits has bits 1 and 3; } when ( ($_ $zero_four) == $zero_four ) { # bits zero and four are on say $asbits has bits 0 and 4; } when ( ($_ $five) == $five ) { # bit five is on say $asbits has bit 5; } default { say $asbits has no interesting bit patterns.; } } } Not lots shorter but you could use a closure to hide the calculation: my $mask; for my $flags ( ... ) { $mask = sub { return ($flags $_[0]) == $_[0] } unless $mask; given( $flags ) { when ( $mask-($one_and_three) ) { ... } when ( $mask-($zero_and_four) ) { ... } ... } } Oops, right. The closure could've/should've been declared w/o a statement qualifier. And now it seems a little bit inelegant to redefine the closure each time through the loop. for my $flags ( ... ) { my $mask = sub { return ($flags $_[0]) == $_[0] }; given( $flags ) { when ( $mask-($one_and_three) ) { ... } when ( $mask-($zero_and_four) ) { ... } ... } ... -- Charles DeRykus
license setup for a Perl program
Dear fellow members, I'm developing a Perl program that can be used on Linux hosts to perform certain tasks. Planning to release a premium version of the program that will be run on servers with public IP address for a low price. How can I setup a license system incorporated to the software? I understand I have to setup the following: 1. A licensing server with a Database that contains user info, host IP - and what else is needed there? 2. setup a program (a CGI script) that runs on the license server to show the status of the license for a particular IP. 3. Do I need a license key for each user? If so, why do I need one and which module can be used to verify the license key - Crypt::RSA? 4. Now I need to call that script from my Perl program. As the program is currently open source, what is the best way to integrate the license system. If it needs to be closed source, what is best way to encrypt a perl program? Any help or tips would be highly appreciated. Thank you in advance!
Re: Bit testing
On Nov 14, 2010, at 4:36 AM, C.DeRykus wrote: And now it seems a little bit inelegant to redefine the closure each time through the loop. for my $flags ( ... ) { my $mask = sub { return ($flags $_[0]) == $_[0] }; given( $flags ) { when ( $mask-($one_and_three) ) { ... } when ( $mask-($zero_and_four) ) { ... } ... } ... First, thanks for (($flags $mask) == $mask). What I eventually did was just call a subroutine on($flags, $mask) to do the calculation and determine whether all the specified bits were on. Seems simpler than the closure technique, even if having to repeatedly write '$flags' is less than ideal. However, now I'm determined to get an understanding of closures, and I see another thread on this list that provides several links to help in that regard. Thanks, one and all! Regards, Chap -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: license setup for a Perl program
On Sun, Nov 14, 2010 at 9:39 AM, vajjra 007 vaj...@gmail.com wrote: Dear fellow members, I'm developing a Perl program that can be used on Linux hosts to perform certain tasks. Planning to release a premium version of the program that will be run on servers with public IP address for a low price. How can I setup a license system incorporated to the software? I understand I have to setup the following: it sounds like you're asking about the technical aspects, so... 1. A licensing server with a Database that contains user info, host IP - and what else is needed there? it depends on how you setup your license. i mean, you could just give a license key of a real long checksum based on some scheme (or use pgp but look into their license if you go that route) and have the client check in with a key on an interval or use policy. 2. setup a program (a CGI script) that runs on the license server to show the status of the license for a particular IP. why? 3. Do I need a license key for each user? If so, why do I need one and which module can be used to verify the license key - Crypt::RSA? haven't looked into this. i'm sure there are tons of ways depending on whether you want to use a checksum or pki#. just make sure your clients open a secure tunnel to the server for license exchange. also, don't try to reinvent the wheel here - one of the worst things that proprietary projects try to do is create their own encryption thinking they'll be more secure - trust me, you're not that good. 4. Now I need to call that script from my Perl program. As the program is currently open source, what is the best way to integrate the license system. If it needs to be closed source, what is best way to encrypt a perl program? google 'obfuscating perl code' Any help or tips would be highly appreciated. Thank you in advance!
Perl, pattern matching, substitution, replacing
Hello again, Yesterday I had a question on pattern matching. A couple of people responded with very useful information. After some finagling, I got my rudimentary code to work. I'm a PhD student studying computational linguistics without any formal programming training. While there are various modules that can be applied to my questions, our professor wants us to manually code things so we understand the wider problems of computational linguistics. With that, here is what I'm trying to do. In a given file, I believe it was XML originally, insert s at the beginning of every sentence and /s at the end of every sentence. So far, I've got the following. The output is in *bold*. $hello = This is some sample text.; $hello =~ s/^../s/gi; $hello =~ s/..$/\/s/gi; print $hello\n; *sis is some sample tex/s* * * I can see why this is happening. I'm telling the program to do exactly what it did. But what I want the output to look like is this. *s This is some sample text./s* * * Any comments are very appreciated. This is a very helpful crowd. Cheers. Zach -- -- Zachary S. Brooks PhD Student in Second Language Acquisition and Teaching (SLAT) The University of Arizona - http://www.coh.arizona.edu/slat/ Graduate Associate in Teaching - Department of English M.A. Applied Linguistics - University of Massachusetts Boston ---
taint issue
so, i'm guessing that i have to mess with ARGV when i use -T on my code? i'm getting this error: Insecure dependency in open while running with -T switch at /usr/lib/perl/5.10/IO/File.pm line 66 now, i didn't get this before spreadsheet::writeexcel. so i'm thinking that i can't use ARGV when i define a 'new' package? here's my code: #!/usr/bin/perl -T use strict; use warnings; use DBI; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; use Carp::Assert; my ( @xldata, $i ); my $parser = Spreadsheet::ParseExcel-new(); my $workbookin = $parser-parse($ARGV[ 0 ]); my $workbookout = Spreadsheet::WriteExcel-new( $ARGV[ 1 ]); my $dbh = DBI-connect('DBI:mysql:ais;host=localhost', 'shawn', 'Pa55W0rd') or die Database connection: $!; if ( !defined $workbookin ) { die Can\'t read spreadsheet: , $parser-error(), .\n; } if ( !defined $workbookout ) { die Can\'t write spreadsheet: $!\n } my $worksheetin = $workbookin-worksheet(0); my ( $row_min, $row_max ) = $worksheetin-row_range(); my ( $col_min, $col_max ) = $worksheetin-col_range(); for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheetin-get_cell( $row, $col ); next unless $cell; $xldata[ $row ][ $col ] = $cell-unformatted() ; } } my $worksheetout = $workbookout-add_worksheet( 'Data' ); $worksheetout-write_row( 'A1', \...@xldata ); #for my $row ( 0 .. $#xldata ) { # print ROW $row :\t; # for my $col ( 0 .. $#{ $xldata[ $row ] } ) { # print $xldata[ $row ][ 13 ], if defined( $xldata[ $row ][ 13 ] ); # } # print \n; #}
Re: Perl, pattern matching, substitution, replacing
On Sun, Nov 14, 2010 at 11:42 AM, Zachary Brooks zbro...@email.arizona.eduwrote: Hello again, Yesterday I had a question on pattern matching. A couple of people responded with very useful information. After some finagling, I got my rudimentary code to work. I'm a PhD student studying computational linguistics without any formal programming training. While there are various modules that can be applied to my questions, our professor wants us to manually code things so we understand the wider problems of computational linguistics. With that, here is what I'm trying to do. In a given file, I believe it was XML originally, insert s at the beginning of every sentence and /s at the end of every sentence. So far, I've got the following. The output is in *bold*. first, forget about about testing tons of regex in programs. if you're trying to learn, it'll make you go nuts. try something like http://regexpal.com/ or google for other 'regex tester' sites. there are also programs (ymmv). btw, i don't see your bold... $hello = This is some sample text.; $hello =~ s/^../s/gi; $hello =~ s/..$/\/s/gi; second, why not use a place holder like someone recommended yesterday? something like: s/^(.+)$/s\1\/s/g print $hello\n; *sis is some sample tex/s* * * I can see why this is happening. I'm telling the program to do exactly what it did. But what I want the output to look like is this. *s This is some sample text./s* * * Any comments are very appreciated. This is a very helpful crowd. Cheers. Zach -- -- Zachary S. Brooks PhD Student in Second Language Acquisition and Teaching (SLAT) The University of Arizona - http://www.coh.arizona.edu/slat/ Graduate Associate in Teaching - Department of English M.A. Applied Linguistics - University of Massachusetts Boston ---
Re: Perl, pattern matching, substitution, replacing
On 10-11-14 11:42 AM, Zachary Brooks wrote: $hello = This is some sample text.; $hello =~ s/^../s/gi; $hello =~ s/..$/\/s/gi; print $hello\n; *sis is some sample tex/s* The meta-character '.' matches every character except a newline. The first substitution replaces 'Th' with 's'. The second, 't.' with '/s'. When faced with a problem like this, it is best to write down everything that is a sentence and determine what is common in all cases. That will tell you how to create your matching patterns. -- Just my 0.0002 million dollars worth, Shawn Programming is as much about organization and communication as it is about coding. The secret to great software: Fail early often. Eliminate software piracy: use only FLOSS. -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: taint issue
hummm, never mind. i decided to stop being stupid in multiple ways. i need to read more on writeexcel, and i've modified my crappy pass and moved that connector to another file so that i don't get stupid again: #!/usr/bin/perl -T use strict; use warnings; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; use Carp::Assert; require './dbconnect.pl'; my( $infile ) = $ARGV[0] =~ m/^([A-Z0-9_.-]+)$/ig; my( $outfile ) = $ARGV[0] =~ m/^([A-Z0-9_.-]+)$/ig; my ( @xldata, $i ); my $parser = Spreadsheet::ParseExcel-new(); my $workbookin = $parser-parse( $infile ); my $workbookout = Spreadsheet::WriteExcel-new( $outfile ); On Sun, Nov 14, 2010 at 11:44 AM, shawn wilson ag4ve...@gmail.com wrote: so, i'm guessing that i have to mess with ARGV when i use -T on my code? i'm getting this error: Insecure dependency in open while running with -T switch at /usr/lib/perl/5.10/IO/File.pm line 66 now, i didn't get this before spreadsheet::writeexcel. so i'm thinking that i can't use ARGV when i define a 'new' package? here's my code: #!/usr/bin/perl -T use strict; use warnings; use DBI; use Spreadsheet::ParseExcel; use Spreadsheet::WriteExcel; use Carp::Assert; my ( @xldata, $i ); my $parser = Spreadsheet::ParseExcel-new(); my $workbookin = $parser-parse($ARGV[ 0 ]); my $workbookout = Spreadsheet::WriteExcel-new( $ARGV[ 1 ]); my $dbh = DBI-connect('DBI:mysql:ais;host=localhost', 'shawn', 'Pa55W0rd') or die Database connection: $!; if ( !defined $workbookin ) { die Can\'t read spreadsheet: , $parser-error(), .\n; } if ( !defined $workbookout ) { die Can\'t write spreadsheet: $!\n } my $worksheetin = $workbookin-worksheet(0); my ( $row_min, $row_max ) = $worksheetin-row_range(); my ( $col_min, $col_max ) = $worksheetin-col_range(); for my $row ( $row_min .. $row_max ) { for my $col ( $col_min .. $col_max ) { my $cell = $worksheetin-get_cell( $row, $col ); next unless $cell; $xldata[ $row ][ $col ] = $cell-unformatted() ; } } my $worksheetout = $workbookout-add_worksheet( 'Data' ); $worksheetout-write_row( 'A1', \...@xldata ); #for my $row ( 0 .. $#xldata ) { # print ROW $row :\t; # for my $col ( 0 .. $#{ $xldata[ $row ] } ) { # print $xldata[ $row ][ 13 ], if defined( $xldata[ $row ][ 13 ] ); # } # print \n; #}
Re: Perl, pattern matching, substitution, replacing
sw == shawn wilson ag4ve...@gmail.com writes: sw second, why not use a place holder like someone recommended yesterday? sw something like: sw s/^(.+)$/s\1\/s/g what is a placeholder? nothing like that in regexes. what you have there is a backreference and used in the wrong place. \1 is meant to be used ONLY in the regex part, not the replacement section. use $1 to get the first grabbed part when in the replacement part. your code will generate warnings: perl -wle '$x = a ; $x =~ s/(a)/\1\1/' \1 better written as $1 at -e line 1. \1 better written as $1 at -e line 1. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com -- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com - -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Perl, pattern matching, substitution, replacing
On Sun, Nov 14, 2010 at 1:53 PM, Uri Guttman u...@stemsystems.com wrote: sw == shawn wilson ag4ve...@gmail.com writes: sw second, why not use a place holder like someone recommended yesterday? sw something like: sw s/^(.+)$/s\1\/s/g what is a placeholder? nothing like that in regexes. what you have there is a backreference and used in the wrong place. \1 is meant to be used ONLY in the regex part, not the replacement section. use $1 to get the first grabbed part when in the replacement part. your code will generate warnings: yep, got confused with sed perl -wle '$x = a ; $x =~ s/(a)/\1\1/' \1 better written as $1 at -e line 1. \1 better written as $1 at -e line 1. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com-- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com-
Re: Perl, pattern matching, substitution
On 14/11/2010 13:53, Zachary Brooks wrote: Hey Rob, Of all the feedback. yours was the one I was able to drop into my code and make it work, no matter how rudimentary my understanding of Perl is. Thanks. You're welcome. I'm glad to be able to help. As far as the XML libraries, we are supposed to learn to understand how libraries work by manually going through the process. OK, but bear in mind that regexes aren't good at matching nested data like XML. It can be easy to write something that works with a specific data set, but a solution that will work for any valid XML data will be much more complex and difficult to implement. As much as I appreciate the help, I want to understand what I did. g=global match, i=ignores case, and s=allows the code to jump lines? I thought the code was naturally greedy. It seems odd. Almost right. /./ matches any character except newline /n, so a pattern like /.*/ will match up to the first newline or the end of the string, whichever is first. The /s modifier allows /./ to match newlines as well, so /.*/s will always match right to the end of the string. Regarding the quotes, I hope the problem disappears but I'll use the q or qq if needed. As I said, the problem exists only because you are declaring test data within your program. Once you are reading from a file the situation will not exist. Cheers, - Rob -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Perl, pattern matching, substitution, replacing
so, if you've got a file, do something like: while ($line = FH ) { $line =~ m/^(.+)$/ig; print s$1\/s\n; } On Sun, Nov 14, 2010 at 1:53 PM, Uri Guttman u...@stemsystems.com wrote: sw == shawn wilson ag4ve...@gmail.com writes: sw second, why not use a place holder like someone recommended yesterday? sw something like: sw s/^(.+)$/s\1\/s/g what is a placeholder? nothing like that in regexes. what you have there is a backreference and used in the wrong place. \1 is meant to be used ONLY in the regex part, not the replacement section. use $1 to get the first grabbed part when in the replacement part. your code will generate warnings: perl -wle '$x = a ; $x =~ s/(a)/\1\1/' \1 better written as $1 at -e line 1. \1 better written as $1 at -e line 1. uri -- Uri Guttman -- u...@stemsystems.com http://www.sysarch.com-- - Perl Code Review , Architecture, Development, Training, Support -- - Gourmet Hot Cocoa Mix http://bestfriendscocoa.com-
Re: Perl, pattern matching, substitution, replacing
On 14/11/2010 19:04, Zachary Brooks wrote: What happened when I used the code -- $hello =~ s/^(.+)$/s\1\/s/gis; -- is that is properly markeds and the beginning of the sentence and/s at the end of the sentence, but then it only worked for one sentence. Any suggestions on gettings to appear at the beginning of every sentence and/s to appear at the end of every sentence for more than one sentence? You must think carefully about what constitutes a 'sentence'. A string starting with a capital letter and ending with a full stop is the most basic definition, but is unlikely to be sufficient for your purposes unless your data is very simple. The program below uses this definition to enclose all 'sentences' in a multi-line string in s tags. I hope it helps you to get started. - Rob use strict; use warnings; my $text = This is some sample text. It has three sentences, all beginning with a capital letter and ending with a full stop. Proper recognition of a 'sentence' could get extremely complicated.; $text =~ s|([A-Z].*?\.)|s$1/s|gs; print $text; __END__ **OUTPUT** sThis is some sample text./s sIt has three sentences, all beginning with a capital letter and ending with a full stop./s sProper recognition of a 'sentence' could get extremely complicated./s -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Perl, pattern matching, substitution, replacing
At 2:16 PM -0500 11/14/10, shawn wilson wrote: so, if you've got a file, do something like: while ($line = FH ) { $line =~ m/^(.+)$/ig; print s$1\/s\n; } If all you want to do is print each line in the file surrounded bys tags, you don't need regular expressions, and you don't need to escape forward slash characters in double-quotes: while ($line = FH ) { chomp($line); print s$line/s\n; } As Rob said, the hard thing with this task is finding out where sentences begin and end. -- Jim Gibson j...@gibson.org -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Bit testing
On Nov 14, 1:11 am, shlo...@iglu.org.il (Shlomi Fish) wrote: Hi Charles, On Sunday 14 November 2010 01:47:36 C.DeRykus wrote: On Nov 11, 11:27 pm, c...@pobox.com (Chap Harrison) wrote: Not lots shorter but you could use a closure to hide the calculation: my $mask; for my $flags ( ... ) { $mask = sub { return ($flags $_[0]) == $_[0] } unless $mask; given( $flags ) { when ( $mask-($one_and_three) ) { ... } when ( $mask-($zero_and_four) ) { ... } ... } } This won't work properly because the closure traps the initial value of $flags. For example: [code] #!/usr/bin/perl use strict; use warnings; my $closure; foreach my $name (qw(Sophie Jack Charles Dan Rachel)) { $closure = sub { print Hello $name!\n ; } unless $closure; $closure-();} [/code] This prints Hello Sophie! five times. Either redeclare the closure on every iteration, or declare it once while using a more outer lexical variable. Right... or simply get rid of the statement qualifier 'unless $closure' which'll work too and is what you'll have to do in any case. (technically, you could declare 'my $closure' both in/outside the loop leaving the statement qualifier as is but that's horrible ) You could just 'my $closure' solely inside the loop too. Declaring once outside the loop with an outer lexical seems less satisfactory since it loosens the 'tightest lexical scope' best practice. -- Charles DeRykus -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: print map question
On Sat, Nov 13, 2010 at 05:43:21AM -0500, Uri Guttman wrote: MM == Mike McClain mike.j...@nethere.com writes: MM Could someone tell me why there is a comma printed after the newline? because you put it there. the \n is input to the map, not the print! map's last arg is a list and it takes @list AND anything else after it. you need to either put the map in parens or make it a func like call like this: map( $_,, @list ) so it can't eat the \n. uri Slapping forehead as the light dawns I have to say, Thanks, Uri. -- Satisfied user of Linux since 1997. O ascii ribbon campaign - stop html mail - www.asciiribbon.org -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/