Re: ORM for database
Hi. On 31/10/2010, Xiao Lan (小兰) practicalp...@gmail.com wrote: Hello, For me I have been using DBI for all kinds of database handling. OK. But some applications require an ORM for database, like what the DBIx::Class module does. Righty. If I understand what you want you're saying, then all you have to make sure is you use DBIx::Class in your code and also provide existing database schema like so- use lib '/some/exported/db/schema' # use `dbicdump` for this if you have existing database use MyDB::Schema; Under what case shall we use the ORM instead of DBI with original SQL input? I use an ORM (DBIx::Class for Perl) so I can wrap SQL statements nicely without having to write one manually. There's provision in DBIx::Class to write SQL statements if you would want that as well. Also, you are essentially not having to use database driver specific code (it handles them somewhat transparently / automagically for you - PG / MySQL / SQLite for e.g.) If you go through DBIx::Class documentation[1], it pretty much tells you everything you need to know and is documented excellently. If you have existing DB, you can export the Schema nicely into a set of class mappings using `dbicdump` and use the Schema as if it were your usual class. A simple example would be something along these lines: use Foo::Schema; $rs-search({ id = $id, # WHERE id = $id }); return $rs-next; [...] More @ http://search.cpan.org/dist/DBIx-Class/ Enjoy! -- Regards Ishwor Gurung Key id:0xa98db35e Key fingerprint:FBEF 0D69 6DE1 C72B A5A8 35FE 5A9B F3BB 4E5E 17B5 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: ORM for database
[...] id = $id, # WHERE id = $id s/=/=/ [...] -- Regards Ishwor Gurung Key id:0xa98db35e Key fingerprint:FBEF 0D69 6DE1 C72B A5A8 35FE 5A9B F3BB 4E5E 17B5 -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
LWP::Simple::head($url) -- Return value
Hi All I had the following code to test what the head() method of the LWP::Simple module returns. #!/usr/bin/perl use warnings; use strict; use LWP::Simple; my $url = 'http://oreilly.com/store/complete.html'; #my $url = 'http://www.garimela.com/complete.html'; my $testvar = head($url); print head when used in scalar context is: $testvar \n; print Success \n if ($testvar); my @listvar = head($url); print head when used in a list context is: @listvar \n; OUTPUT: --- head when used in scalar context is: HTTP::Response=HASH(0x861cd00) Success Use of uninitialized value $listvar[3] in join or string at catalog.plx line 15. head when used in a list context is: text/html; charset=utf-8 462143 1288520055 Apache My Questions: --- 1. From the module's documentation i can understand that the return value of the head() method in a scalar context is TRUE , but what does the value returned by the server which is HTTP::Response=HASH(0x861cd00) signify ? 2. Why am i getting a warning message in the line 3 of the output ? Thanks Jatin -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: LWP::Simple::head($url) -- Return value
On 10-10-31 07:52 AM, Jatin wrote: 1. From the module's documentation i can understand that the return value of the head() method in a scalar context is TRUE , but what does the value returned by the server which is HTTP::Response=HASH(0x861cd00) signify ? It is a hash reference. See `perldoc perlref`. 2. Why am i getting a warning message in the line 3 of the output ? Because its value is undef. To see what's in your variables, `use Data::Dumper`. use Data::Dumper; print '$testvar ', Dumper $testvar; print '@listvar ', Dumper \...@listvar; -- 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/
anonymous hash reference
Hi, I have an array with anonymous hash references like the following: $foo = [ { name = value, id = value, }, { name = value, id = value, } ]; Iterating through the hash references works with: foreach $item (@$foo) { do something with $item-name; } What I like to do, is to check for the existence of a given key/value pair in any of the hash-references, eg. for name=bar. When I understood it right, the exists function only checks if a given hash element has been initialized, but it doesn't check of a specific value. Any ideas?! -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: anonymous hash reference
于 2010-10-31 21:43, Thorsten Scherf 写道: Hi, I have an array with anonymous hash references like the following: $foo = [ { name = value, id = value, }, { name = value, id = value, } ]; Iterating through the hash references works with: foreach $item (@$foo) { do something with $item-name; } I think what you want is the code like: use strict; my $foo = [ { name = 'foo', id = 1, }, { name = 'bar', id = 2, } ]; print check_for_exists('name','foo'); sub check_for_exists { my $key = shift; my $value = shift; for my $item (@$foo) { if ($item-{$key} eq $value ) { return 1; } } return 0; } -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Removing leading whitespace and removing alternate newlines
Hi guys, long time no working with PERL :-) I have just installed 5.2.12 First, I am trying to remove leading tabs spaces but the following leaves a few blanks at the beginning of each line, could someone be kind enough to point out the error of my ways please? :-) #!/usr/bin/perl local $/=undef; open(FILE, smith3.txt) || die (Error\n); $string = FILE; $string =~ s/\s//; #remove leading spaces print $string; Secondly, I would like to remove newline from alternate lines, ie I would like to remove from lines 1,3,5,7 etc. What would be the simplest way of getting even line numbers to print on the same line as odds? Thanks muchly Brian
Re: Removing leading whitespace and removing alternate newlines
$string =~ s/^\s+//; http://oreilly.com/catalog/9781565922433/ page 43 That book is my bible!!! On Sun, Oct 31, 2010 at 11:01 AM, Brian brian5432...@yahoo.co.uk wrote: Hi guys, long time no working with PERL :-) I have just installed 5.2.12 First, I am trying to remove leading tabs spaces but the following leaves a few blanks at the beginning of each line, could someone be kind enough to point out the error of my ways please? :-) #!/usr/bin/perl local $/=undef; open(FILE, smith3.txt) || die (Error\n); $string = FILE; $string =~ s/\s//; #remove leading spaces print $string; Secondly, I would like to remove newline from alternate lines, ie I would like to remove from lines 1,3,5,7 etc. What would be the simplest way of getting even line numbers to print on the same line as odds? Thanks muchly Brian -- Richard Hug 813-973-3783 cell 813-361-7946
Re: Removing leading whitespace and removing alternate newlines
On Sunday 31 October 2010 17:01:24 Brian wrote: Hi guys, long time no working with PERL :-) See: http://perl.org.il/misc.html#pl_vs_pl I have just installed 5.2.12 You probably mean perl-5.12.2 . A few more comments on your code: First, I am trying to remove leading tabs spaces but the following leaves a few blanks at the beginning of each line, could someone be kind enough to point out the error of my ways please? :-) #!/usr/bin/perl Add strict and warnings. See: http://perl-begin.org/tutorials/bad-elements/ local $/=undef; You probably don't want to read the entire file to memory at once, as it may be very large. open(FILE, smith3.txt) || die (Error\n); Use three-args-open, lexical filehandles, and use a more descriptive error than Error.. $string = FILE; $string =~ s/\s//; #remove leading spaces This will remove only the first whitespace character (and in your case, from the entire file). print $string; Secondly, I would like to remove newline from alternate lines, ie I would like to remove from lines 1,3,5,7 etc. What would be the simplest way of getting even line numbers to print on the same line as odds? Use something like this (untested): [code] #!/usr/bin/perl use strict; use warnings; my $filename = shift(@ARGV) or die No filename specified in command-line; open my $fh, , $filename or die Cannot open '$filename' for reading - $!; my $line_num = 1; while (my $line = $fh) { chomp ($line); $line =~ s{\A\s+}{}; print $line; if ($line_num % 2 == 0) { print \n; } } continue { $line_num++; } close($fh); [/code] Regards, Shlomi Fish Thanks muchly Brian -- - Shlomi Fish http://www.shlomifish.org/ Best Introductory Programming Language - http://shlom.in/intro-lang 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: anonymous hash reference
JP == Jeff Pang jeff_p...@sina.com writes: JP print check_for_exists('name','foo'); JP sub check_for_exists { JPmy $key = shift; JPmy $value = shift; JP for my $item (@$foo) { JP if ($item-{$key} eq $value ) { JPreturn 1; JP } JP } JP return 0; JP } that reduces to just: $item-{'foo'} eq $value ; there is no reason for a full sub for that. and it has a flaw (as does the OP's question). which comparison op do you use? hash values can be numbers too so eq may not work as expected on them. 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: Removing leading whitespace and removing alternate newlines
Thank you Shlomi, I'll test part 2 tomorrow when I have more time. As for part one, what would be the better way of writing the code so that I can read/treat one line at a time? thanks again Brian From: Shlomi Fish shlo...@iglu.org.il To: beginners@perl.org Cc: Brian brian5432...@yahoo.co.uk Sent: Sun, October 31, 2010 4:23:52 PM Subject: Re: Removing leading whitespace and removing alternate newlines On Sunday 31 October 2010 17:01:24 Brian wrote: Hi guys, long time no working with PERL :-) I have just installed 5.2.12 You probably mean perl-5.12.2 . A few more comments on your code: First, I am trying to remove leading tabs spaces but the following leaves a few blanks at the beginning of each line, could someone be kind enough to point out the error of my ways please? :-) #!/usr/bin/perl local $/=undef; You probably don't want to read the entire file to memory at once, as it may be very large. open(FILE, smith3.txt) || die (Error\n); Use three-args-open, lexical filehandles, and use a more descriptive error than Error.. $string = FILE; $string =~ s/\s//; #remove leading spaces This will remove only the first whitespace character (and in your case, from the entire file). print $string; Secondly, I would like to remove newline from alternate lines, ie I would like to remove from lines 1,3,5,7 etc. What would be the simplest way of getting even line numbers to print on the same line as odds? Use something like this (untested): [code] #!/usr/bin/perl use strict; use warnings; my $filename = shift(@ARGV) or die No filename specified in command-line; open my $fh, , $filename or die Cannot open '$filename' for reading - $!; my $line_num = 1; while (my $line = $fh) { chomp ($line); $line =~ s{\A\s+}{}; print $line; if ($line_num % 2 == 0) { print \n; } } continue { $line_num++; } close($fh); [/code] Regards, Shlomi Fish Thanks muchly Brian --
Re: Removing leading whitespace and removing alternate newlines
Thanks Shlomi, your untested code worked first time. :-) From: Shlomi Fish shlo...@iglu.org.il To: beginners@perl.org Cc: Brian brian5432...@yahoo.co.uk Sent: Sun, October 31, 2010 4:23:52 PM Subject: Re: Removing leading whitespace and removing alternate newlines On Sunday 31 October 2010 17:01:24 Brian wrote: Hi guys, long time no working with PERL :-) Secondly, I would like to remove newline from alternate lines, ie I would like to remove from lines 1,3,5,7 etc. What would be the simplest way of getting even line numbers to print on the same line as odds? Use something like this (untested): [code] #!/usr/bin/perl use strict; use warnings; my $filename = shift(@ARGV) or die No filename specified in command-line; open my $fh, , $filename or die Cannot open '$filename' for reading - $!; my $line_num = 1; while (my $line = $fh) { chomp ($line); $line =~ s{\A\s+}{}; print $line; if ($line_num % 2 == 0) { print \n; } } continue { $line_num++; } close($fh); [/code] Regards, Shlomi Fish Thanks muchly Brian -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Removing leading whitespace and removing alternate newlines
Brian wrote: Hi guys, long time no working with PERL :-) Hello, I have just installed 5.2.12 First, I am trying to remove leading tabs spaces Just tabs and spaces? Your code says all whitespace. but the following leaves a few blanks at the beginning of each line, You code only removes one whitespace character. could someone be kind enough to point out the error of my ways please? :-) #!/usr/bin/perl use warnings; use strict; local $/=undef; Or just: local $/; open(FILE, smith3.txt) || die (Error\n); Better as: open FILE, '', 'smith3.txt' or die Cannot open 'smith3.txt' $!; $string =FILE; my $string = FILE; $string =~ s/\s//; #remove leading spaces $string =~ s/^[ \t]+//mg; #remove leading spaces and tabs from every line print $string; No need to stringify something that is alraedy a string: print $string; perldoc -q quoting Secondly, I would like to remove newline from alternate lines, ie I would like to remove from lines 1,3,5,7 etc. What would be the simplest way of getting even line numbers to print on the same line as odds? my $count; $string =~ s/(\n)/ $count++ % 2 ? $1 : '' /eg; John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Removing leading whitespace and removing alternate newlines
Thank you John From: John W. Krahn jwkr...@shaw.ca To: Perl Beginners beginners@perl.org Sent: Sun, October 31, 2010 8:59:43 PM Subject: Re: Removing leading whitespace and removing alternate newlines Brian wrote: Hi guys, long time no working with PERL :-) Hello, I have just installed 5.2.12 First, I am trying to remove leading tabs spaces Just tabs and spaces? Your code says all whitespace. but the following leaves a few blanks at the beginning of each line, You code only removes one whitespace character. could someone be kind enough to point out the error of my ways please? :-) #!/usr/bin/perl use warnings; use strict; local $/=undef; Or just: local $/; open(FILE, smith3.txt) || die (Error\n); Better as: open FILE, '', 'smith3.txt' or die Cannot open 'smith3.txt' $!; $string =FILE; my $string = FILE; $string =~ s/\s//; #remove leading spaces $string =~ s/^[ \t]+//mg; #remove leading spaces and tabs from every line print $string; No need to stringify something that is alraedy a string: print $string; perldoc -q quoting Secondly, I would like to remove newline from alternate lines, ie I would like to remove from lines 1,3,5,7 etc. What would be the simplest way of getting even line numbers to print on the same line as odds? my $count; $string =~ s/(\n)/ $count++ % 2 ? $1 : '' /eg; John -- Any intelligent fool can make things bigger and more complex... It takes a touch of genius - and a lot of courage to move in the opposite direction. -- Albert Einstein -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Compare files
Thanks for the previous help, that triggered a few dormant grey cells :-) and leads me to another question. I would like to compare 2 (unsorted) csv files.. file 1 contains fredbloggs,0 joebloggs,3 joeblow,6 file 2 fredbloggs,1 joebloggs,4 replace the value in file 2 with the value in file 1. if the item in file 1 doesn't exist in file 2, insert/append the line so that file 2 becomes fredbloggs,0 joebloggs,4 joeblow,6 Again, thanks for helping, it's much appreciated. Brian
Re: anonymous hash reference
Thanks Uri and Jepp, that was exactly the information I was looking for. - Original Message - From: Uri Guttman u...@stemsystems.com To: Jeff Pang jeff_p...@sina.com Cc: beginners@perl.org Sent: Sunday, October 31, 2010 6:39:50 PM GMT +01:00 Amsterdam / Berlin / Bern / Rome / Stockholm / Vienna Subject: Re: anonymous hash reference JP == Jeff Pang jeff_p...@sina.com writes: JP print check_for_exists('name','foo'); JP sub check_for_exists { JPmy $key = shift; JPmy $value = shift; JP for my $item (@$foo) { JP if ($item-{$key} eq $value ) { JPreturn 1; JP } JP } JP return 0; JP } that reduces to just: $item-{'foo'} eq $value ; there is no reason for a full sub for that. and it has a flaw (as does the OP's question). which comparison op do you use? hash values can be numbers too so eq may not work as expected on them. 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/ -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/