problem with passing variables

2011-12-30 Thread Mark Haney
I'm not sure if this is the right list for this, so bear with me.  If it 
isn't I'll be glad to post it on the correct one.


I've got a problem with passing variables to a SQL server inside a CGI 
script.  My code is like this:


my $begin_time = 2011-11-16 11:00:00;
my $end_time = 2011-11-16 12:00:00;

my $dbh = DBI-connect('dbi:mysql:database=embdev', 'user', 'password');

my $sql = q/SELECT * FROM events WHERE date BETWEEN $begin_time and 
$end_time/;


my $sth = $dbh-prepare($sql);
$sth-execute();

I'm not sure why it's not using the the variables, can someone point out 
what I'm doing wrong?


-
Mark Haney


--
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/




Re: problem with passing variables

2011-12-30 Thread nat
Mark,
I'm kind of new with perl, but from what I see, you're using a single 
quote when defining $sql, and it should be qq for the interpolated string.  
With the single quote (q) it is a literal.   Hope this helps.


On Friday, December 30, 2011 11:17:30 AM Mark Haney wrote:
 I'm not sure if this is the right list for this, so bear with me.  If it
 isn't I'll be glad to post it on the correct one.
 
 I've got a problem with passing variables to a SQL server inside a CGI
 script.  My code is like this:
 
 my $begin_time = 2011-11-16 11:00:00;
 my $end_time = 2011-11-16 12:00:00;
 
 my $dbh = DBI-connect('dbi:mysql:database=embdev', 'user', 'password');
 
 my $sql = q/SELECT * FROM events WHERE date BETWEEN $begin_time and
 $end_time/;
 
 my $sth = $dbh-prepare($sql);
 $sth-execute();
 
 I'm not sure why it's not using the the variables, can someone point out
 what I'm doing wrong?
 
 -
 Mark Haney

-- 
nat
enrgeeman.com

-- 
To unsubscribe, e-mail: beginners-cgi-unsubscr...@perl.org
For additional commands, e-mail: beginners-cgi-h...@perl.org
http://learn.perl.org/




Re: File Size Script Help - Working Version

2011-12-30 Thread Igor Dovgiy
Hi Jonathan,

Argh, really stupid mistake by me. ) But let's use it to explain some
points a bit further, shall we?
A skilled craftsman knows his tools well, and Perl programmer (with CPAN as
THE collection of tools of all sizes and meanings) has an advantage here: even
if documentation is a bit vague about what's going on, we are (usually)
able to check the code itself to find the answers. )

By browsing File::Spec source (found via 'Source' link within the
'File::Spec' page at CPAN)...
http://cpansearch.perl.org/src/SMUELLER/PathTools-3.33/lib/File/Spec.pm
...we soon discover that this module is essentially an adapter for modules
like File::Spec::Unix, File::Spec::Mac, File::Spec::Win32 etc.
So our search goes on (as your mention of .DS_Store file implies) over
there:
http://cpansearch.perl.org/src/SMUELLER/PathTools-3.33/lib/File/Spec/Mac.pm

Now we may either check the documentation (which clearly states that only
the last argument to catfile is considered a filename, and all the others
will be concatenated with catdir), or look right into the code - and come
to the same conclusion:

sub catfile {
my $self = shift;
return '' unless @_;
my $file = pop @_;
return $file unless @_;
my $dir = $self-catdir(@_);
$file =~ s/^://s;
return $dir.$file;
}

So what should we do now? ) Of course, give milk to our cat... and
arguments to File::Spec's catfile! ) Like this:

File::Spec-catfile($path, $dircontents . '.md5')
... or this...
File::Spec-catfile($path, $dircontents.md5)
(check 'variable interpolation in Perl' to see why it's possible - and why
this is essentially the same as previous codeline)
... or even this ...
File::Spec-catfile($path, join '.', $dircontents, 'md5')
(but that would be a bit overkill, of course :)

Speaking of overkills: you used regex (=~ /^\./) to check whether the line
begins with a dot - or not. )
It's ok for this task, but you probably should know that these checks may
be also done with (substr($line, 0, 1) eq '.') code,
which will be a bit (up to 30% at my PC when Benchmark'ed) faster.

-- iD

2011/12/30 Jonathan Harris jtnhar...@googlemail.com

 I tried to use your suggestion
 open my $wr_fh, '', File::Spec-catfile($path, $dircontents, '.md5') or
 die $!, $/
 but it returned an error on the command line:
  'Not a directory'
  At which point the program dies (which is what it is supposed to do!)
  I used it inside the loop - sorry to bug you for clarification


 
 if ($dircontents=~/^\./ || -d $dircontents) {
  next;
 }

 This is also to avoid the file .DS_Store




Re: File Size Script Help - Working Version

2011-12-30 Thread Igor Dovgiy
Hi John, yes, good point! Totally forgot this. ) Adding new files to a
directory as you browse it is just not right, of course. Possible, but not
right. )

I'd solve this by using hash with filenames as keys and collected 'result'
strings (with md5 and filesizes) as values, filled by File::Find target
routine.
After the whole directory is processed, this hash should be 'written out'
into the target directory.

Another way to do it is to collect all the filenames instead into a list
(with glob operator, for example), and process this list after.

BTW (to Jonathan), I wonder do you really need to store this kind of data
in different files? No offence... but I can hardly imagine how this data
will be used later unless gathered into some array or hash. )

-- iD

2011/12/30 John W. Krahn jwkr...@shaw.ca

 Jonathan Harris wrote:


  Hi John

 Thanks for your 2 cents

 I hadn't considered that the module wouldn't be portable


 That is not what I was implying.  I was saying that when you add new files
 to a directory that you are traversing you _may_ get irregular results.  It
 depends on how your operating system updates directory entries.




Re: File Size Script Help - Working Version

2011-12-30 Thread Jonathan Harris
On Fri, Dec 30, 2011 at 11:58 AM, Igor Dovgiy ivd.pri...@gmail.com wrote:

 Hi John, yes, good point! Totally forgot this. ) Adding new files to a
 directory as you browse it is just not right, of course. Possible, but not
 right. )

 I'd solve this by using hash with filenames as keys and collected 'result'
 strings (with md5 and filesizes) as values, filled by File::Find target
 routine.
 After the whole directory is processed, this hash should be 'written out'
 into the target directory.

 Another way to do it is to collect all the filenames instead into a list
 (with glob operator, for example), and process this list after.

 BTW (to Jonathan), I wonder do you really need to store this kind of data
 in different files? No offence... but I can hardly imagine how this data
 will be used later unless gathered into some array or hash. )

 -- iD

 2011/12/30 John W. Krahn jwkr...@shaw.ca

  Jonathan Harris wrote:
 
 
   Hi John
 
  Thanks for your 2 cents
 
  I hadn't considered that the module wouldn't be portable
 
 
  That is not what I was implying.  I was saying that when you add new
 files
  to a directory that you are traversing you _may_ get irregular results.
  It
  depends on how your operating system updates directory entries.
 
 



Hi All

John -
Thanks for the clarification
In this instance the script has been run on OSX - it seems that adding the
files into the directory that is being traversed works ok this time

However for best practice, I would certainly look into writing to a
separate directory, and then moving the files back, as I appreciate that
this fortune may not necessarily be repeated in a different environment!

Igor -
Firstly -  File::Spec
Thanks for your insight and well explained investigation - I have been
learning a lot from this
File::Spec has proven a most useful tool in joining and 'stringifying' the
paths

In the original post about this script, I had spoken about considering
using a hash for the file data
I'm still convinced that ultimately, this would be the way forwards

I have found some scripts online concerning finding duplicate files
They use md5 and/or file sizes to compare the files
These are written into hashes
Fully understanding some of these scripts is a little beyond my level at
the moment

I have attached an interesting one for you to look at (you may be aware of
it already!)
However, it has proved quite inspiring!

 (substr($line, 0, 1) eq '.')

Haven't learned this yet!
It looks like a good solution if it is so much more efficient - thanks for
the introduction - I'll be reading up asap!

BTW (to Jonathan), I wonder do you really need to store this kind of data
in different files? No offence... but I can hardly imagine how this data
will be used later unless gathered into some array or hash. )

There is a good reason for this!
Talking to guys who work in video on demand, it seems that it is standard
practice to do this for file delivery requirements
As each video file must be identical upon receipt as it was upon delivery (
and that the files are all treated as unique delivery instances )
a separate accompanying file is required
I thought that Perl would be a good choice for accomplishing this
requirement as it is renowned for file handling

#

Thanks to everyone for your help and contributions - particularly Jim,
Shlomi, John and Igor
I have learned crazy amounts already!

Happy New Year to you all!

Jonathan


finddupes3.plx
Description: Binary data
-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/


Re: What does = means?

2011-12-30 Thread Xi Chen
Yes, I agree the code looks strange. Do you have any idea to do this
with a clear code? I mean to find two same letters, p in @a?

Xi

On Thu, Dec 29, 2011 at 10:17 PM, John W. Krahn jwkr...@shaw.ca wrote:
 Xi Chen wrote:

 Hello everyone,

 I saw a code below to get two same letters p in @a.

 @a = qw (D D p O H p A O);
 foreach $b (@a){
 $n =~ /$b/i;
 if($n= 2){
      $m = $b;
     }
 }

 But I don't know what does = mean. Thank you!


 It means greater than or equal to.  The expression $n = 2 is true if
 the value in $n is equal to 2 or is any value greater than 2, 6 for example.
  If the value in $n is less than 2 then the expression is false.

 Your algorithm looks weird though because you are testing $n for the
 presence of alphabetic characters (and then not using that information) and
 then using $n in a numerical context.



 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/



--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: What does = means?

2011-12-30 Thread Igor Dovgiy
Hi Xi,

You're looking only for 'p' letters, not D and O? Why?

Anyway, generic solution will be something like...

my %seen;
my @repeated = grep { /some regex here/  $seen{$_}  N }  @source_array;

... where N is how many times the symbols should appear in the source array
to be counted as duplicate.
and 'some regex' is, well, some regex to filter the symbols if needed. :)

-- iD

2011/12/30 Xi Chen cxde...@gmail.com

 Yes, I agree the code looks strange. Do you have any idea to do this
 with a clear code? I mean to find two same letters, p in @a?

 Xi

 On Thu, Dec 29, 2011 at 10:17 PM, John W. Krahn jwkr...@shaw.ca wrote:
  Xi Chen wrote:
 
  Hello everyone,
 
  I saw a code below to get two same letters p in @a.
 
  @a = qw (D D p O H p A O);
  foreach $b (@a){
  $n =~ /$b/i;
  if($n= 2){
   $m = $b;
  }
  }
 
  But I don't know what does = mean. Thank you!
 
 
  It means greater than or equal to.  The expression $n = 2 is true if
  the value in $n is equal to 2 or is any value greater than 2, 6 for
 example.
   If the value in $n is less than 2 then the expression is false.
 
  Your algorithm looks weird though because you are testing $n for the
  presence of alphabetic characters (and then not using that information)
 and
  then using $n in a numerical context.
 
 
 
  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/
 
 

 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/





Re: What does = means?

2011-12-30 Thread Igor Dovgiy
Oh my, of course it should be...
my @repeated = grep { /some regex here/  ++$seen{$_}  N }  @source_array;
... to work properly.

-- iD

2011/12/30 Igor Dovgiy ivd.pri...@gmail.com

 Hi Xi,

 You're looking only for 'p' letters, not D and O? Why?

 Anyway, generic solution will be something like...

 my %seen;
 my @repeated = grep { /some regex here/  $seen{$_}  N }  @source_array;

 ... where N is how many times the symbols should appear in the source
 array to be counted as duplicate.
 and 'some regex' is, well, some regex to filter the symbols if needed. :)

 -- iD

 2011/12/30 Xi Chen cxde...@gmail.com

 Yes, I agree the code looks strange. Do you have any idea to do this
 with a clear code? I mean to find two same letters, p in @a?

 Xi

 On Thu, Dec 29, 2011 at 10:17 PM, John W. Krahn jwkr...@shaw.ca wrote:
  Xi Chen wrote:
 
  Hello everyone,
 
  I saw a code below to get two same letters p in @a.
 
  @a = qw (D D p O H p A O);
  foreach $b (@a){
  $n =~ /$b/i;
  if($n= 2){
   $m = $b;
  }
  }
 
  But I don't know what does = mean. Thank you!
 
 
  It means greater than or equal to.  The expression $n = 2 is true
 if
  the value in $n is equal to 2 or is any value greater than 2, 6 for
 example.
   If the value in $n is less than 2 then the expression is false.
 
  Your algorithm looks weird though because you are testing $n for the
  presence of alphabetic characters (and then not using that information)
 and
  then using $n in a numerical context.
 
 
 
  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/
 
 

 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/






Re: What does = means?

2011-12-30 Thread John Riselvato
in this code what is the
$n =~ /$b/i;

On Fri, Dec 30, 2011 at 11:53 AM, John Riselvato jdriselv...@gmail.comwrote:

 in this code what is the
 $n =~ /$b/i;


 On Fri, Dec 30, 2011 at 10:51 AM, Igor Dovgiy ivd.pri...@gmail.comwrote:

 Oh my, of course it should be...
 my @repeated = grep { /some regex here/  ++$seen{$_}  N }
  @source_array;
 ... to work properly.

 -- iD

 2011/12/30 Igor Dovgiy ivd.pri...@gmail.com

  Hi Xi,
 
  You're looking only for 'p' letters, not D and O? Why?
 
  Anyway, generic solution will be something like...
 
  my %seen;
  my @repeated = grep { /some regex here/  $seen{$_}  N }
  @source_array;
 
  ... where N is how many times the symbols should appear in the source
  array to be counted as duplicate.
  and 'some regex' is, well, some regex to filter the symbols if needed.
 :)
 
  -- iD
 
  2011/12/30 Xi Chen cxde...@gmail.com
 
  Yes, I agree the code looks strange. Do you have any idea to do this
  with a clear code? I mean to find two same letters, p in @a?
 
  Xi
 
  On Thu, Dec 29, 2011 at 10:17 PM, John W. Krahn jwkr...@shaw.ca
 wrote:
   Xi Chen wrote:
  
   Hello everyone,
  
   I saw a code below to get two same letters p in @a.
  
   @a = qw (D D p O H p A O);
   foreach $b (@a){
   $n =~ /$b/i;
   if($n= 2){
$m = $b;
   }
   }
  
   But I don't know what does = mean. Thank you!
  
  
   It means greater than or equal to.  The expression $n = 2 is
 true
  if
   the value in $n is equal to 2 or is any value greater than 2, 6 for
  example.
If the value in $n is less than 2 then the expression is false.
  
   Your algorithm looks weird though because you are testing $n for the
   presence of alphabetic characters (and then not using that
 information)
  and
   then using $n in a numerical context.
  
  
  
   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/
  
  
 
  --
  To unsubscribe, e-mail: beginners-unsubscr...@perl.org
  For additional commands, e-mail: beginners-h...@perl.org
  http://learn.perl.org/
 
 
 
 




 --
 Its the Othello of programming languages: a minute to learn, a lifetime
 to master - mwn3d (RosettaCode irc)






-- 
Its the Othello of programming languages: a minute to learn, a lifetime to
master - mwn3d (RosettaCode irc)


problem with passing variables

2011-12-30 Thread Mark Haney
I'm not sure if this is the right list for this, so bear with me.  If it 
isn't I'll be glad to post it on the correct one.


I've got a problem with passing variables to a SQL server inside a CGI 
script.  My code is like this:


my $begin_time = 2011-11-16 11:00:00;
my $end_time = 2011-11-16 12:00:00;

my $dbh = DBI-connect('dbi:mysql:database=embdev', 'user', 'password');

my $sql = q/SELECT * FROM events WHERE date BETWEEN $begin_time and 
$end_time/;


my $sth = $dbh-prepare($sql);
$sth-execute();

I'm not sure why it's not using the the variables, can someone point out 
what I'm doing wrong?


-
Mark Haney


--
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Review: Perl for Perl Newbies Part 5 - Good Programming Practices

2011-12-30 Thread Shlomi Fish
Hi all,

I've finally finished writing the 5th part of Perl for Perl Newbies:

http://www.shlomifish.org/lecture/Perl/Newbies/lecture5/

I'd like people on this list to review it, and comment. Note that the series of
presentations was also directed at absolute beginners (though I may not have
been successful in it being suitable for them), and its philosophy is explained
here and here:

* http://www.shlomifish.org/lecture/Perl/Newbies/

* http://perl-begin.org/tutorials/#perl_for_newbies

The material covered in the talks has suffered from a lot of bit rot during
the many years since I've first written them, with progress in both my
understanding of Perl, and in the state-of-the-art in Perl best practices, and
one can refer to the books Modern Perl by chromatic and recent editions
of Beginning Perl and of the Learning Perl/Intermediate Perl/Mastering Perl
trio for alternatives that are possibly better.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
Star Trek: We, the Living Dead - http://shlom.in/st-wtld

Staring at XSLT code for one minute has a 67% chance of making one permanently
blind.

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: problem with passing variables

2011-12-30 Thread Igor Dovgiy
Hi Mark,

If your variables are strictly internal and by no means might be ever
tainted (read: user input), what you're doing is mostly ok.
But you need to quote the dates passed within query itself, like this:

my $sql = qq/SELECT * FROM `events` WHERE `date` BETWEEN '$begin_time' AND
'$end_time'/;
*(qq, of course, not q: you'd like your variables to be interpolated, would
you? :)*

But there's another (and in my opinion, usually better) way: using prepared
sql statement:
my $sth = $dbh-prepare(q/
  SELECT * FROM `events` WHERE `date` BETWEEN ? AND ?
/);
$sth-execute($begin_time, $end_time);

This method is safer, but a little (or not, depending on driver and DB
used) bit slower than direct queries.

Have to say that I usually prefer even simpler DBI methods, like
selectall_arrayref, combining the power of `prepare`, `execute` and `fetch`
methods in one statement. But that's a matter of taste, I guess. )

-- iD

P.S. BTW, if you want to know the reason why particular SQL query fails,
just call errstr method of your DBI object (like $dbh-errstr) - and print
the result. )

2011/12/30 Mark Haney ma...@abemblem.com

 I'm not sure if this is the right list for this, so bear with me.  If it
 isn't I'll be glad to post it on the correct one.

 I've got a problem with passing variables to a SQL server inside a CGI
 script.  My code is like this:

 my $begin_time = 2011-11-16 11:00:00;
 my $end_time = 2011-11-16 12:00:00;

 my $dbh = DBI-connect('dbi:mysql:**database=embdev', 'user', 'password');

 my $sql = q/SELECT * FROM events WHERE date BETWEEN $begin_time and
 $end_time/;

 my $sth = $dbh-prepare($sql);
 $sth-execute();

 I'm not sure why it's not using the the variables, can someone point out
 what I'm doing wrong?

 -
 Mark Haney


 --
 To unsubscribe, e-mail: beginners-unsubscr...@perl.org
 For additional commands, e-mail: beginners-h...@perl.org
 http://learn.perl.org/





Re: problem with passing variables

2011-12-30 Thread Shlomi Fish
On Fri, 30 Dec 2011 12:08:50 -0500
Mark Haney ma...@abemblem.com wrote:

 I'm not sure if this is the right list for this, so bear with me.  If it 
 isn't I'll be glad to post it on the correct one.
 
 I've got a problem with passing variables to a SQL server inside a CGI 
 script.  My code is like this:
 
 my $begin_time = 2011-11-16 11:00:00;
 my $end_time = 2011-11-16 12:00:00;
 
 my $dbh = DBI-connect('dbi:mysql:database=embdev', 'user', 'password');
 
 my $sql = q/SELECT * FROM events WHERE date BETWEEN $begin_time and 
 $end_time/;
 
 my $sth = $dbh-prepare($sql);
 $sth-execute();
 
 I'm not sure why it's not using the the variables, can someone point out 
 what I'm doing wrong?
 

Please use placeholders:

* http://en.wikipedia.org/wiki/SQL_injection

* http://bobby-tables.com/

The problem with your code is that Perl only expands variables inside strings
(called string-interpolation) only when these strings are notated - not later
on. The best way to solve it is using placeholders, which will also help
mitigate SQL injection problems.

Regards,

Shlomi Fish 

-- 
-
Shlomi Fish   http://www.shlomifish.org/
The Human Hacking Field Guide - http://shlom.in/hhfg

I’d love to change the world, but they won’t give me the source code.
— Unknown

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: problem with passing variables

2011-12-30 Thread Mark Haney

On 12/30/2011 12:30 PM, Igor Dovgiy wrote:

Hi Mark,

If your variables are strictly internal and by no means might be ever 
tainted (read: user input), what you're doing is mostly ok.

But you need to quote the dates passed within query itself, like this:

my $sql = qq/SELECT * FROM `events` WHERE `date` BETWEEN '$begin_time' 
AND '$end_time'/;
/(qq, of course, not q: you'd like your variables to be interpolated, 
would you? :)/


Yeah, true. I missed that part.


But there's another (and in my opinion, usually better) way: using 
prepared sql statement:

my $sth = $dbh-prepare(q/
  SELECT * FROM `events` WHERE `date` BETWEEN ? AND ?
/);
$sth-execute($begin_time, $end_time);


I can certainly do it this way, however, my ultimate goal is to have 
these variables passed via a web form and since I'm still getting my 
feet wet with using perl to a MySQL database exclusively (I can do SQL 
very well, but never inside perl) I am taking baby steps.




This method is safer, but a little (or not, depending on driver and DB 
used) bit slower than direct queries.


Have to say that I usually prefer even simpler DBI methods, like 
selectall_arrayref, combining the power of `prepare`, `execute` and 
`fetch` methods in one statement. But that's a matter of taste, I guess. )


-- iD

P.S. BTW, if you want to know the reason why particular SQL query 
fails, just call errstr method of your DBI object (like $dbh-errstr) 
- and print the result. )







Re: problem with passing variables

2011-12-30 Thread Shlomi Fish
Hi Mark,

On Fri, 30 Dec 2011 12:39:04 -0500
Mark Haney ma...@abemblem.com wrote:

 On 12/30/2011 12:30 PM, Igor Dovgiy wrote:
  Hi Mark,
 
  If your variables are strictly internal and by no means might be ever 
  tainted (read: user input), what you're doing is mostly ok.
  But you need to quote the dates passed within query itself, like this:
 
  my $sql = qq/SELECT * FROM `events` WHERE `date` BETWEEN '$begin_time' 
  AND '$end_time'/;
  /(qq, of course, not q: you'd like your variables to be interpolated, 
  would you? :)/
 
 Yeah, true. I missed that part.
 
  But there's another (and in my opinion, usually better) way: using 
  prepared sql statement:
  my $sth = $dbh-prepare(q/
SELECT * FROM `events` WHERE `date` BETWEEN ? AND ?
  /);
  $sth-execute($begin_time, $end_time);
 
 I can certainly do it this way, however, my ultimate goal is to have 
 these variables passed via a web form and since I'm still getting my 
 feet wet with using perl to a MySQL database exclusively (I can do SQL 
 very well, but never inside perl) I am taking baby steps.
 

If you're going to pass variables from a web form into Perl, then *definitely*
use placeholders (unless you want an SQL injection problem on your hand). If
you want something more higher-level, then you can look at Object-Relational
Mappers such as http://search.cpan.org/dist/DBIx-Class/ (not necessarily the
best, but the elephant in the room) or http://search.cpan.org/dist/KiokuDB/
(which is an Object-Graph storage engine), and a form handler such as
http://search.cpan.org/dist/HTML-FormFu/ .

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
What does Zionism mean? - http://shlom.in/def-zionism

Chuck Norris is the greatest man in history. He killed all the great men who
could ever pose a competition.

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: problem with passing variables

2011-12-30 Thread Igor Dovgiy
If you pass into SQL query something assigned by user, use placeholders by
all means. ) It's not that hard, but it'll save you a lot of headaches,
believe me. )

2011/12/30 Mark Haney ma...@abemblem.com

 But there's another (and in my opinion, usually better) way: using
 prepared sql statement:
 my $sth = $dbh-prepare(q/
   SELECT * FROM `events` WHERE `date` BETWEEN ? AND ?
 /);
 $sth-execute($begin_time, $end_time);


 I can certainly do it this way, however, my ultimate goal is to have these
 variables passed via a web form and since I'm still getting my feet wet
 with using perl to a MySQL database exclusively (I can do SQL very well,
 but never inside perl) I am taking baby steps.




What is the $_ variable?

2011-12-30 Thread Harry
I am confused what this variable does: $_

If someone could give an example or explanation of what this variable $_ does 
in perl, that would be great!

Thanx.


-- 
To unsubscribe, e-mail: beginners-unsubscr...@perl.org
For additional commands, e-mail: beginners-h...@perl.org
http://learn.perl.org/




Re: File Size Script Help - Working Version

2011-12-30 Thread Brandon McCaig
On Thu, Dec 29, 2011 at 03:43:19PM +, Jonathan Harris wrote:
 Hi All

Hello Jonathan:

(Disclaimer: I stayed up all night playing Skyrim and am running
on about 4.5 hours of sleep.. ^_^)

I think most things have already been addressed, but I think Igor
might have had a bit of trouble making it clear.

 opendir (my $in, $path) or die Cannot open $dir: $!\n;
 find (\wanted, $path);
 close $in;
 
 opendir (my $totalin, $path) or die Cannot open $dir: $!\n;
 find (\cleanup, $path);
 close $totalin;

AFAICT, it's completely nonsensical to open a directory file
handle surrounding File::Find::find. I tried to /search the
perldoc (just in case there's some kind of magical optimization
or something) and saw no mention of 'opendir' or 'handle' (except
for the special _ file handle created for stat, lstat, etc..). So
it seems $in and $totalin are completely unnecessary here:
File::Find will worry about opening and processing the
directories for you.

 sub wanted {
  while ($dircontents = readdir($in)) {

I guess this is why you are opening directory handles above, but
it doesn't really make sense. You're basically only using
File::Find to loop at this point, and very obscurely. :)
File::Find's role in life is precisely to find you all the files
within a directory tree. You're reinventing the square wheel with
your use of opendir and readdir. :)

The wanted subroutine is typically used to either process the
file system tree outright, or store applicable files in data
structures for later processing. E.g.,

use strict;
use warnings;
use File::Find;

my @files;

sub wanted
{
# Skip dot files and directories.
return if substr($_, 0, 1) eq '.' || -d $_;

# If current file is a normal file, push into array for
# later.
push @files, $File::Find::name if -f $_;
}

my $path = '.';

find \wanted, $path;

# Now @files should be filled with a recursive list of files to
# process. E.g.,

for my $file (@files)
{
my $md5name = $file.'md5';
# Etc...
}

 my $hex = Digest::MD5-new-addfile($fh)-hex digest;

I assume you meant `hexdigest' here, not 'hex digest'.

 $newname =~ s/\ //;

Ideally if you're going to do something as obscure as this, you
should comment it in both places so future readers and
maintainers understand why it's done, even if they only read one
half of the program.

I think Igor else has already explained how to eliminate this
obscurity though. :)

Regards,


-- 
Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org
Castopulence Software https://www.castopulence.org/
Blog http://www.bamccaig.com/
perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'



signature.asc
Description: Digital signature


Re: What is the $_ variable?

2011-12-30 Thread Shlomi Fish
Hello Harry,

On Fri, 30 Dec 2011 10:03:30 -0800 (PST)
Harry kaichow...@gmail.com wrote:

 I am confused what this variable does: $_
 
 If someone could give an example or explanation of what this variable $_ does 
 in perl, that would be great!

$_ is the default variable, which gets assigned to and used in various
contexts. You can think of it as the pronoun it in English.

Personally, I feel that overusing $_ is not recommended in most serious
programs, because it is easily devastated, but:

1. You sometimes need to use it, like in
http://perldoc.perl.org/functions/map.html , 
http://perldoc.perl.org/functions/grep.html , 
as well as some other functions from List::Util , List::MoreUtils ,
List::UtilsBy and similar modules (and even then, it is sometimes useful to
assign it to a lexical my variable.). 

2. It is sometimes useful in one-off programs and scripts, especially perl
-pe/perl -ne/perl -pale/perl -lane/etc. scripts (where people also allow for
omitting use strict; and use warnings;.

Regards,

Shlomi Fish  

-- 
-
Shlomi Fish   http://www.shlomifish.org/
My Public Domain Photos - http://www.flickr.com/photos/shlomif/

Larry Wall gets the colon.

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: problem with passing variables

2011-12-30 Thread Mark Haney

On 12/30/2011 12:50 PM, Igor Dovgiy wrote:
If you pass into SQL query something assigned by user, use 
placeholders by all means. ) It's not that hard, but it'll save you a 
lot of headaches, believe me. )


2011/12/30 Mark Haney ma...@abemblem.com mailto:ma...@abemblem.com


But there's another (and in my opinion, usually better) way:
using prepared sql statement:
my $sth = $dbh-prepare(q/
  SELECT * FROM `events` WHERE `date` BETWEEN ? AND ?
/);
$sth-execute($begin_time, $end_time);


I can certainly do it this way, however, my ultimate goal is to
have these variables passed via a web form and since I'm still
getting my feet wet with using perl to a MySQL database
exclusively (I can do SQL very well, but never inside perl) I am
taking baby steps.


I'm definitely going to do it that way, now that I've had a chance to 
read the replies and do some more googling on the subject.  My biggest 
issue now is parsing the form data from STDIN from a POST in a HTML 
form.   (Which is  a whole other issue and not one I'm going to address 
in this reply.


Thanks for all the replies.  They were all very helpful.



Re: problem with passing variables

2011-12-30 Thread Shlomi Fish
Hi Mark,

On Fri, 30 Dec 2011 14:19:04 -0500
Mark Haney ma...@abemblem.com wrote:

 On 12/30/2011 12:50 PM, Igor Dovgiy wrote:
  If you pass into SQL query something assigned by user, use 
  placeholders by all means. ) It's not that hard, but it'll save you a 
  lot of headaches, believe me. )
 
  2011/12/30 Mark Haney ma...@abemblem.com mailto:ma...@abemblem.com
 
  But there's another (and in my opinion, usually better) way:
  using prepared sql statement:
  my $sth = $dbh-prepare(q/
SELECT * FROM `events` WHERE `date` BETWEEN ? AND ?
  /);
  $sth-execute($begin_time, $end_time);
 
  I can certainly do it this way, however, my ultimate goal is to
  have these variables passed via a web form and since I'm still
  getting my feet wet with using perl to a MySQL database
  exclusively (I can do SQL very well, but never inside perl) I am
  taking baby steps.
 
 
 I'm definitely going to do it that way, now that I've had a chance to 
 read the replies and do some more googling on the subject.  

I'm glad you will.

 My biggest 
 issue now is parsing the form data from STDIN from a POST in a HTML 
 form.   (Which is  a whole other issue and not one I'm going to address 
 in this reply.

For this you should use http://plackperl.org/ or at the very least the CGI.pm
module from the Perl core (also available on CPAN - it's dual-life.), or one of
its CPAN alternatives. Doing it by hand is error-prone, and not recommended.

Regards,

Shlomi Fish

-- 
-
Shlomi Fish   http://www.shlomifish.org/
My Aphorisms - http://www.shlomifish.org/humour.html

Microsoft — making it all make sense. Ours.

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: File Size Script Help - Working Version

2011-12-30 Thread Jonathan Harris
On Fri, Dec 30, 2011 at 7:11 PM, Brandon McCaig bamcc...@gmail.com wrote:

 On Thu, Dec 29, 2011 at 03:43:19PM +, Jonathan Harris wrote:
  Hi All

 Hello Jonathan:

 (Disclaimer: I stayed up all night playing Skyrim and am running
 on about 4.5 hours of sleep.. ^_^)

 I think most things have already been addressed, but I think Igor
 might have had a bit of trouble making it clear.

  opendir (my $in, $path) or die Cannot open $dir: $!\n;
  find (\wanted, $path);
  close $in;
 
  opendir (my $totalin, $path) or die Cannot open $dir: $!\n;
  find (\cleanup, $path);
  close $totalin;

 AFAICT, it's completely nonsensical to open a directory file
 handle surrounding File::Find::find. I tried to /search the
 perldoc (just in case there's some kind of magical optimization
 or something) and saw no mention of 'opendir' or 'handle' (except
 for the special _ file handle created for stat, lstat, etc..). So
 it seems $in and $totalin are completely unnecessary here:
 File::Find will worry about opening and processing the
 directories for you.

  sub wanted {
   while ($dircontents = readdir($in)) {

 I guess this is why you are opening directory handles above, but
 it doesn't really make sense. You're basically only using
 File::Find to loop at this point, and very obscurely. :)
 File::Find's role in life is precisely to find you all the files
 within a directory tree. You're reinventing the square wheel with
 your use of opendir and readdir. :)

 The wanted subroutine is typically used to either process the
 file system tree outright, or store applicable files in data
 structures for later processing. E.g.,

 use strict;
 use warnings;
 use File::Find;

 my @files;

 sub wanted
 {
# Skip dot files and directories.
return if substr($_, 0, 1) eq '.' || -d $_;

# If current file is a normal file, push into array for
# later.
push @files, $File::Find::name if -f $_;
 }

 my $path = '.';

 find \wanted, $path;

 # Now @files should be filled with a recursive list of files to
 # process. E.g.,

 for my $file (@files)
 {
my $md5name = $file.'md5';
# Etc...
 }

  my $hex = Digest::MD5-new-addfile($fh)-hex digest;

 I assume you meant `hexdigest' here, not 'hex digest'.

  $newname =~ s/\ //;

 Ideally if you're going to do something as obscure as this, you
 should comment it in both places so future readers and
 maintainers understand why it's done, even if they only read one
 half of the program.

 I think Igor else has already explained how to eliminate this
 obscurity though. :)

 Regards,


 --
 Brandon McCaig bamcc...@gmail.com bamcc...@castopulence.org
 Castopulence Software https://www.castopulence.org/
 Blog http://www.bamccaig.com/
 perl -E '$_=q{V zrna gur orfg jvgu jung V fnl. }.
 q{Vg qbrfa'\''g nyjnlf fbhaq gung jnl.};
 tr/A-Ma-mN-Zn-z/N-Zn-zA-Ma-m/;say'


 -BEGIN PGP SIGNATURE-
 Version: GnuPG v1.4.10 (GNU/Linux)

 iQIcBAEBAgAGBQJO/gzlAAoJEN2n1gIi5ZPyMJoP/15LI4aelHowMiNQYFzLB2E1
 nEAjPHvvHX7uTxLV8BOht9HtoQPpOwyQ/fJm2EIe59NGwjjvlKCdpCrfkfxz3cvV
 td0wDuUKrjzQPaACl4cNrGITZLVXe6KtZUKG2o3TjA5dlyqbes5d5F40Mh3j/fB5
 L0VZpWNvp0cTrJ6x5QfTkvyQPdxKx0ARaFDQYpvR3uKfgeD28ZNatNCQQuuymLkj
 ABVqLzQgVVMinaj/4xXii5vedgYFI58DPWF7r0nmhUaiVvDAEFzfd1MlSvkuI8Jr
 EKTQdz3EjMZufRaGxX96rdZvVMEiSTcA/IXNkhis48dOwa4ebfSYm8QpQhp1E6UF
 QuJBRXzF9cHvD095Aw+MSqoSR+2LZS3SFBfdB9I5rdxJEoS7LMSJdJLY95dXftqR
 KdlcU7ds4kdqaJrnxrxB05SIhgZNq1JcCk9xZyuk7NxsPwl/6ZStr/E/V2mA+bdD
 bGWCuP7IKRfZ8cNd01tEYnUppYkfxRaNtYhlNmPVh6TX7rd+2Z4aZLn0anR3Q/J4
 2OsPmtIOakk1jW6f41vTOqZ5cpxSv/R1H6fjZAOVACf7UlrtCgVWVLKYofcO/ryS
 HMsX1T3m3h9H3heSd76FiXMsgDBaMBkti31FzwnS3pOaZEpHk6eHF/dbR2p0sAbA
 gTvG4xDp2MLEtY03Ofhw
 =RFiR
 -END PGP SIGNATURE-



HI Brandon

Thanks for your response


I totally agree with your first point
Having now used File::Find a little more, I have seen that using opendir
was totally unneccessary and have removed them from the script
And guess what.it works fine without them!
I think that my initial confusion arose from fundamentally misunderstanding
File::Find - thinking that it required a handle, not just a path.

I have also now exchanged the while loop with a foreach loop - much better!

 I assume you meant `hexdigest' here, not 'hex digest'.

You assume correctly! Gmail has started to do annoying auto text complete -
I must turn it off!!

 push @files, $File::Find::name if -f $_;

This is nice and clean

Your approach is different to what we have been discussing
You seem to gather the files with File::Find and then leave that sub alone
asap
The processing is then done in the results of that gathering

My script left the processing within sub wanted
This could possible be a reason that complications arose so quickly

To get file names and sizes at the same time, I am also considering

my %files;

sub wanted {
my $filesize = (stat($_))[7];
push @{$files{$filesize}}, $File::Find::name;
}

find(\wanted, $path);

to hash files and file size results together - then process after

And yep, Igor 

Re: File Size Script Help - Working Version

2011-12-30 Thread Igor Dovgiy
Great work, Jonathan!
Notice how simple your script has become - and that's a good sign as well
in Perl. :) We can make it even simpler, however.

As you probably know, Perl has two fundamental types of collections: arrays
(where data is stored as a sequence of elements, data chunks) and hashes
(where data chunks are unordered, but stored with some unique key used to
retrieve it). Sometimes hashes are used just to sort out (non-)unique data,
but that's another story.

Now look at this line:
   push @{$files{$filesize}}, $File::Find::name;

Don't you see something... weird? You're using hash where filesizes are the
keys - and because, yes, they may well be non-unique, you have to store
arrays of filenames in your hash instead...

But much more natural (at least, for me) is to organize your hash (let's
call it %filedata) so that filenames (which are unique by their nature)
become the keys. And some info about these files - sizes and md5-hashes -
become the values.

For example, our `wanted` (btw, its name is misleading a bit, no? may be
'process' will sound better?) sub may look as follows:

find(\wanted, $path);

my %filedata;
sub wanted {
  return if substr($_, 0, 1) eq '.' || -d $_;
  my $filesize = -s _;
  open my $fh, '', $_ or die $!, $/;
  my $filemd5 = Digest::MD5-new-addfile($fh)-hexdigest;
  close $fh;
  $filedata{$_} = [$filesize, $filemd5];
}

(*Notice how you don't have to declare the global filedata hash before the
callback function is called in `find`? It's really interesting topic*)

Then you'll just have to iterate over the %filedata - and it's as easy as
writing...

for my $filename (keys %filedata) {
  my ($size, $md5) = @{ $filedata{$filename} };
  open my $fh, '', File::Spec-catfile($path, $filename.md5)
or die $!, $/;
  print $fh $filename\t$size bytes\t$md5\n;
  close $fh;
}

... yep, that easy. )

-- iD

P.S. Ordering the output should be an easy task for you; hint - look up
'sort' documentation - or just use sort system routine. :)

2011/12/31 Jonathan Harris jtnhar...@googlemail.com

 HI Brandon

 Thanks for your response


 I totally agree with your first point
 Having now used File::Find a little more, I have seen that using opendir
 was totally unneccessary and have removed them from the script
 And guess what.it works fine without them!
 I think that my initial confusion arose from fundamentally misunderstanding
 File::Find - thinking that it required a handle, not just a path.

 I have also now exchanged the while loop with a foreach loop - much better!

  I assume you meant `hexdigest' here, not 'hex digest'.

 You assume correctly! Gmail has started to do annoying auto text complete -
 I must turn it off!!

  push @files, $File::Find::name if -f $_;

 This is nice and clean

 Your approach is different to what we have been discussing
 You seem to gather the files with File::Find and then leave that sub alone
 asap
 The processing is then done in the results of that gathering

 My script left the processing within sub wanted
 This could possible be a reason that complications arose so quickly

 To get file names and sizes at the same time, I am also considering

 my %files;

 sub wanted {
my $filesize = (stat($_))[7];
push @{$files{$filesize}}, $File::Find::name;
 }

 find(\wanted, $path);

 to hash files and file size results together - then process after

 And yep, Igor has been thorough and very helpful

 Thanks again for your input on this - hope you manage to get some sleep!

 All the best

 Jonathan



Re: File Size Script Help - Working Version

2011-12-30 Thread John W. Krahn

Igor Dovgiy wrote:

Great work, Jonathan!
Notice how simple your script has become - and that's a good sign as well
in Perl. :) We can make it even simpler, however.

As you probably know, Perl has two fundamental types of collections: arrays
(where data is stored as a sequence of elements, data chunks) and hashes
(where data chunks are unordered, but stored with some unique key used to
retrieve it). Sometimes hashes are used just to sort out (non-)unique data,
but that's another story.

Now look at this line:

   push @{$files{$filesize}}, $File::Find::name;


Don't you see something... weird? You're using hash where filesizes are the
keys - and because, yes, they may well be non-unique, you have to store
arrays of filenames in your hash instead...

But much more natural (at least, for me) is to organize your hash (let's
call it %filedata) so that filenames (which are unique by their nature)
become the keys. And some info about these files - sizes and md5-hashes -
become the values.


Yes, file names in a given directory _have_ to be unique, however...



For example, our `wanted` (btw, its name is misleading a bit, no? may be
'process' will sound better?) sub may look as follows:

find(\wanted, $path);

my %filedata;
sub wanted {
   return if substr($_, 0, 1) eq '.' || -d $_;
   my $filesize = -s _;
   open my $fh, '', $_ or die $!, $/;
   my $filemd5 = Digest::MD5-new-addfile($fh)-hexdigest;
   close $fh;
   $filedata{$_} = [$filesize, $filemd5];


You are traversing a directory tree, so using $_ as the key may cause 
collisions across different directories.  Better to use 
$File::Find::name which contains the full absolute path name.




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/