CRM as a service with Perl module
Hi guys, Can anyone recommend a hosted CRM solution (except for Salesforce) that has a CPAN module in order to easily access the API? Thanks, Andy
Re: Getting the latest related record from a SQL DB
On Thu, 2014-10-09 at 13:28 +0100, Andrew Beverley wrote: I have a table (say artist, couldn't resist...) that has a one-to-many relationship to another table (say album). The album table has a field which references the artist table's ID. So one artist can have many albums. ... But what if I want to fetch an artist's details and his latest album? I can select the artist from the artists table and then join the albums table. But to get the latest album I'd have to use a max function (say on the album's date), with which it isn't possible to get the related fields in the same row. Thanks for all the replies. Two good options from several people: limit the result to one row, or use a sub-select. What I failed to say was that as well as doing this for a single artist as per my question, I'd like to also be able to do it for several artists. Therefore (I think) I can't use either of those options without doing several database queries. I also like to avoid sub-selects as MySQL has a tendency to optimise them badly (as Gareth said). What I also failed to say was that I'm using DBIC and want to keep things database agnostic. So, I used Bill's suggestion, which seems to work well (http://london.pm.org/pipermail/london.pm/Week-of-Mon-20141006/025481.html) To get it to work with DBIC I had to create a custom join condition. Something like: $class-might_have( album_later = 'Album', sub { my $args = shift; return { $args-{foreign_alias}.artist_id = { -ident = $args-{self_alias}.artist_id }, $args-{foreign_alias}.released = { '' = \$args-{self_alias}.released }, }; } ); And then add the relevant search criteria when using it: $search = { {'album' = 'album_later'}, ... } Thanks again for all the replies. Andy
Re: Getting the latest related record from a SQL DB
On Sun, 2014-10-12 at 17:00 +0100, Andrew Beverley wrote: And then add the relevant search criteria when using it: $search = { {'album' = 'album_later'}, ... } Oops, copy and paste error. More like: $rs-search({ 'album_later.artist_id' = undef });
Re: Getting the latest related record from a SQL DB
On Sun, 2014-10-12 at 17:25 +0100, Andrew Beverley wrote: On Sun, 2014-10-12 at 17:00 +0100, Andrew Beverley wrote: And then add the relevant search criteria when using it: $search = { {'album' = 'album_later'}, ... } Oops, copy and paste error. More like: $rs-search({ 'album_later.artist_id' = undef }); And add the required join of course... join = {'album' = 'album_later'}
Getting the latest related record from a SQL DB
Hi guys, I'm after some best-practice advice regarding SQL database design. I have a table (say artist, couldn't resist...) that has a one-to-many relationship to another table (say album). The album table has a field which references the artist table's ID. So one artist can have many albums. So, if I want to know all of an artist's albums, that's easy. But what if I want to fetch an artist's details and his latest album? I can select the artist from the artists table and then join the albums table. But to get the latest album I'd have to use a max function (say on the album's date), with which it isn't possible to get the related fields in the same row. I see 2 ways of solving this: - Run multiple queries to get the relevant album's ID (if even possible) and then retrieve its row in entirety. - Have a reference from the artist table back to the album table, specifying which is the latest album, which I update each time the albums table is updated. Neither seem particularly tidy to me, so am I missing something completely obvious? Thanks, Andy
Ansible (was: Deploying perl code)
On Fri, 2014-07-25 at 12:08 +0200, James Laver wrote: On 25 Jul 2014, at 11:54, Andrew Beverley a...@andybev.com wrote: The main problem is that it seems to be a victim of its own success: there is a huge backlog of merge requests. I'd like to provide some simple patches to a couple of modules to make them work better for me, but have little hope that they'd be merged this side of Christmas. I provided a really simple patch a while ago - it's not been touched and now no longer merges cleanly. I won’t even try and submit a patch after my experience reporting a documentation bug. [...] So, I'm now fed-up with the way Ansible is being developed. However, I do like it as a tool, so wondered whether anybody has any interest in developing a Perl equivalent? Probably a layer on top of Tak to create a higher level playbook-type environment. Or does something similar already exist? Andy
Re: Ansible (was: Deploying perl code)
On Mon, 2014-08-11 at 10:43 -0500, Eric Johnson wrote: Have you looked at Rex? They have pretty good docs: http://rexify.org Thanks Eric (and Pierre) - no, I hadn't come across that. I'll have a play with it and see how it is - looks very promising though. Its a deployment type tool vaguely like ansible/puppet/chef/salt/whatever and its written in Perl and it looks pretty nice. I've personally never used Rex for real anywhere though. I wrote up a little intro to using rex (the comments might be interesting too): http://blog.kablamo.org/2013/11/22/rex/ Great article - thanks for that. Andy
Re: Deploying perl code
On Fri, 2014-07-25 at 10:11 +0200, James Laver wrote: Ansible I do like for the most part I'm a fan of Ansible, and am in the process of using it to deploy code (although more by accident than design). The main problem is that it seems to be a victim of its own success: there is a huge backlog of merge requests. I'd like to provide some simple patches to a couple of modules to make them work better for me, but have little hope that they'd be merged this side of Christmas. I provided a really simple patch a while ago - it's not been touched and now no longer merges cleanly.
Re: Deploying perl code
On Fri, 2014-07-25 at 12:08 +0200, James Laver wrote: On 25 Jul 2014, at 11:54, Andrew Beverley a...@andybev.com wrote: The main problem is that it seems to be a victim of its own success: there is a huge backlog of merge requests. I'd like to provide some simple patches to a couple of modules to make them work better for me, but have little hope that they'd be merged this side of Christmas. I provided a really simple patch a while ago - it's not been touched and now no longer merges cleanly. I won’t even try and submit a patch after my experience reporting a documentation bug. I was at the time regularly tweeting with their CTO, so I let him know on twitter. He then pointed me at the bug tracker, where I proceeded to file my bug. Immediately, ‘ansibot’ informed me that my bug would be closed in a few days if I didn’t reformat my bug to correspond with their designated format for bugs, which included such things as “steps to reproduce” (because you know, we all have difficulty reproducing documentation bugs). I think the only way I actually got that doc bug fixed was by complaining loudly and repetitively on twitter about how ridiculous their processes are, but they showed absolutely no willing to make it easier to contribute towards their projects, so I’m showing absolutely no willing to help them grow their company. And therein proves my point. I'd love to fork it, but that's not going to help of course. They'd do themselves a massive favour by splitting out the modules à la CPAN.
Re: [ANNOUNCE] Reminder: Tech Meet this Thursday 7pm at Conway Hall
On Wed, 2014-07-23 at 09:09 +0100, Vytautas D wrote: All the trafficparking aside, there are quite a few of us coming from southampton.pm, hence it's much cheaper option.. sigh. A real shame if this is the case. It makes me so sad to see people driving into central London. Hamburg anyone?
Re: Interview - a Dancer in London:)
On Thu, 2014-06-26 at 07:30 +0100, james.la...@gmail.com wrote: Be careful of yaks. I went to write some blogging software and I've made 10 modules releasable since, but still no blog. Well yes, it's not /actually/ on CPAN yet. I was thinking I could talk about the Email::Signature module I asked about on here a while ago: https://github.com/ctrlo/libemail-signature-perl The story is that I created it as a result of a user requirement of a company I'm doing some work for: because I've released it as open source, the company is not tied-into some bespoke proprietary software. But I don't need to explain the beauty of open-source software to you lot as you already know, which is why I wasn't sure whether it's an appropriate talk for LPW... Andy
Re: Interview - a Dancer in London:)
On Thu, 2014-06-26 at 08:56 +0100, Sue Spence wrote: 1. 5 things I wish I'd known as a Perl beginner. I think this one would make a good first talk at our next tech meet. It could be very low stress with a minimum of preparation time. 5 slides, a bit of chat about each one, and optionally interacting with the rabble a bit on each point. :-) Happy with that, but I was actually thinking I could talk about 2 projects I've been working on recently: 1. The Email::Signature thing as per previous email. 2. Some web-based software I've written recently that makes managing tabulated data easy. A bit like a spreadsheet, but with version-control on each item of data, and better input validation/selection. It also does basic graphs. It's aimed as a replacement for all those situations where people use a spreadsheet to record basic lists of data. Andy
Re: Interview - a Dancer in London:)
On Thu, 2014-06-26 at 10:36 +0100, mascip wrote: You spreadsheet-y software sounds interesting. I hope I'll get to hear you talk about this one. It's pretty basic, but more than happy to talk about it sometime. With vim-like keybindings perhaps? ;-) I'm afraid my target audience wouldn't even know what vim is, but more than happy to accept patches ;-)
Re: Interview - a Dancer in London:)
On Wed, 2014-06-25 at 14:21 +, Tom Hukins wrote: There's only one part that worries me: Is there any chance you'll give a presentation at the next LPW? Possibly, but I don't feel I've got to that level yet! Ah, peer pressure ;-) Hmmm. Anybody interested in either of these? 1. 5 things I wish I'd known as a Perl beginner. 2. From user requirement to open source project: the birth of a CPAN module. Andy
Re: Evaluating user-defined conditions
On Tue, 2014-06-10 at 13:43 +0200, Abigail wrote: I'm going to need to allow strings to be matched. E.g: [age] 10 [name] eq jon Allow any string? Including strings that may potentially excute code? In that case, eval will be wrong. Or is the string just a list of alphanumerics? In which case, just add another clause to the definition of term. The string could potentially be anything. So yes, eval's a bad idea, and I need to spend some time looking at the various parser options that people have kindly suggested. Thanks, Andy
Re: Evaluating user-defined conditions
On Mon, 2014-06-09 at 11:36 +0100, Andrew Beverley wrote: Dear all, I'd like to take a condition specified by a user and use it to perform a set of tests on a data set. Is there a module to do this? Thanks for all the replies. Indeed, I can't trust the user input, but nonetheless I wondered whether I could still use eval, but heavily sanitise the input. It seems a lot easier than than using a parser. Can anyone see anything wrong with the following? The user-supplied variables are specified in square brackets, e.g. [age] # Sub in the variable values foreach my $var (@variables) { my $value = ... # Could be a string in quotes $code =~ s/\[$var\]/$value/gi; } # Sanitise $_ = $code; return unless /^[ \S]+$/; # Only allow normal spaces return if /[\[\]]+/;# No brackets should remain return if /\\/; # No escapes please s/[^]+//g; # Remove quoted strings m!^([-()*+/0-9 ]||eq)+$! or return; # Allowed expression chars Thanks, Andy
Re: Evaluating user-defined conditions
On Tue, 2014-06-10 at 09:20 +0200, Abigail wrote: # Sanitise $_ = $code; return unless /^[ \S]+$/; # Only allow normal spaces return if /[\[\]]+/;# No brackets should remain return if /\\/; # No escapes please s/[^]+//g; # Remove quoted strings m!^([-()*+/0-9 ]||eq)+$! or return; # Allowed expression chars So, you excluding having any alpha char (except 'eq') in the resulting expression? Because that's what the last line does. Perhaps that's your intention, because I've no idea what $value is going to be, other than it could be a string in quotes. Yes, $value is either an integer or a string in quotes. The 2 lines previous to the last line should remove any string in quotes, therefore just leaving operators and integers? Now, if you do allow for alpha characters to be present, you have to make sure things like system qw xrm -rf foox are filtered out. (As you can see, the remove quoted strings isn't much of a filter -- q, qq, qw, qx, qr, s, m, and y can take any delimiter). I'm happy to be restrictive to the user, and only allow straightforward strings in double quotes. So anything else is removed or not allowed, and the strings in quotes are checked as above. I would not be surprised if I've missed something though! Andy
Re: Evaluating user-defined conditions
On Tue, 2014-06-10 at 09:20 +0200, Abigail wrote: # Sanitise $_ = $code; return unless /^[ \S]+$/; # Only allow normal spaces return if /[\[\]]+/;# No brackets should remain return if /\\/; # No escapes please s/[^]+//g; # Remove quoted strings m!^([-()*+/0-9 ]||eq)+$! or return; # Allowed expression chars So, you excluding having any alpha char (except 'eq') in the resulting expression? Sorry, just realised that I didn't explain myself properly. It's not what is remaining in $_ that is run, it's the original code that is in $code. The sequence above just checks that the code is safe to run. Andy
Re: Evaluating user-defined conditions
On Tue, 2014-06-10 at 10:05 +0100, James Laver wrote: I was sort of hoping that the not too subtle hints that using eval is a bad idea would pay off. Apparently not. D'oh, I thought someone might say that... But it's so easy ;-) Got the message, will play with a parser.
Re: Evaluating user-defined conditions
On Tue, 2014-06-10 at 11:37 +0200, Abigail wrote: On Tue, Jun 10, 2014 at 09:26:17AM +0100, Andrew Beverley wrote: On Tue, 2014-06-10 at 09:20 +0200, Abigail wrote: # Sanitise $_ = $code; return unless /^[ \S]+$/; # Only allow normal spaces return if /[\[\]]+/;# No brackets should remain return if /\\/; # No escapes please s/[^]+//g; # Remove quoted strings m!^([-()*+/0-9 ]||eq)+$! or return; # Allowed expression chars So, you excluding having any alpha char (except 'eq') in the resulting expression? Because that's what the last line does. Perhaps that's your intention, because I've no idea what $value is going to be, other than it could be a string in quotes. Yes, $value is either an integer or a string in quotes. The 2 lines previous to the last line should remove any string in quotes, therefore just leaving operators and integers? So, your loop replaces [$var] with a string in quotes, only for s/[^]+//g; to remove it? I'm a bit confused what purpose that serves. The code that's run is what is in-between those 2 statements. The latter is a sanity check. I didn't explain that properly. Considering that you want to be really restrictive, a parser is going to be quite simple. Why not just write a parser instead of hoping you've catched anything potentially bad? Because it was 2am and I needed something for a demo today ;-) But yes, I agree that's the best way. Thanks, Andy
Re: Evaluating user-defined conditions
On Tue, 2014-06-10 at 12:23 +0200, Abigail wrote: Note that all you need is a *validating* parser. You don't have to bother with building a parse tree, and evaluating the results -- *that* can be left to Perl. Ah, okay, thanks. Here's a pattern that accepts expressions of the form you initially posted (and I've replaced [var] with var): #!/usr/bin/perl use 5.010; use strict; use warnings; no warnings 'syntax'; use Test::Regexp; use Test::More; my $pat = qr { (?(DEFINE) (?expression (?term) (?: \ * (?: [-+*] | | eq ) \ * (?expression))?) (?term\( (?expression) \) | [a-z]+| [0-9]+)) ^(?expression)$ }x; my $test = Test::Regexp:: - new - init ( keep_pattern= $pat, no_keep_message = 1, name= Expression ); # # Using (?namePAT) leaves traces in %- behind, even if they don't # actually capture... # my $captures = [[expression = undef], [term = undef]]; $test - match ('age 10 price (age + 5) * 10', captures = $captures); $test - no_match ('@{qx (rm -rf)} 1'); done_testing; __END__ Brilliant, thanks for that, much appreciated. However, isn't that similar to what I was trying to do, in that you're relying on the regex to match (or otherwise) accordingly? And you'd still need to eval it afterwards? I'm going to need to allow strings to be matched. E.g: [age] 10 [name] eq jon which sounds dangerous for the very good reasons you outlined previously (thanks for that - glad that I asked!) Andy
Evaluating user-defined conditions
Dear all, I'd like to take a condition specified by a user and use it to perform a set of tests on a data set. Is there a module to do this? For example, I might have an array of hashes containing name, price and age. I would like a user to be able to define their own condition, such as age 10 price (age + 5) * 10, and then to get the result (true or false) for each of the array items. Is there a nice easy way to do this without trying to parse the condition myself? Presumably I could do some sort of eval, but that sounds highly dangerous with user input ;-) Andy
Libreoffice/Microsoft office compatibility and pagination
Hi guys, Does anyone have any advice to help ensure that Microsoft Office files open (and save) correctly in Libreoffice (on Linux), in particular in terms of pagination? From what I have read the advice seems to be: 1. Install Microsoft fonts 2. Use a recent version of Libreoffice However, the pagination for almost every DOC file that I open with Libreoffice is wrong (there are always more pages in the import than the original document). Happy to file bug reports etc, but just wanted to check I hadn't missed anything obvious first. Andy
Re: Module namespace for projects
On Fri, 2014-05-23 at 09:42 +, Tom Hukins wrote: On Thu, May 22, 2014 at 09:59:00AM +0100, Andrew Beverley wrote: Following on from the above, should I then be uploading the modules to CPAN? In addition to James's helpful reply, one of my colleagues reminded me of PrePAN this morning: http://prepan.org/ Great, thanks, I hadn't come across that. Let's try it: http://prepan.org/module/nXWJ8Y9sBFA Also, consider putting your code in a Git repository somewhere and posting a link to it here. https://github.com/ctrlo/libemail-signature-perl If you do so before a social meeting, you might find a few people in the pub who would like to talk through your work over a drink. :) Hopefully I'll be at the next meeting. My first new module - honest feedback gratefully received ;-) The aim is to follow this module with a Postfix filter to sign all outgoing emails with a signature (more than a simple disclaimer). Not something I'd want personally, but I work with companies that want a consistent company signature with HTML, graphics etc. Variables for the signature (name etc) can be taken from a database. Andy
Module namespace for projects
Hi guys, I'm creating a Perl project that I hope to eventually release (a Postfix filter to add signatures to outgoing emails). My aim is to eventually release it as a Debian package. A stupid question, but I'm a bit unsure about how I should define the namespace for my project's modules. Do I need to slot any program modules into the CPAN namespace? So, for example, I'll have a module to manage all the database aspects. Let's say the program will be called emailsig and I use Emailsig::DB for the database queries. Presumably the module Emailsig::DB would eventually need to be installed under /usr/share, so should I actually be naming the module something like Email::Emailsig::DB? Following on from the above, should I then be uploading the modules to CPAN? Grateful for any advice! Andy
Re: Module namespace for projects
On Thu, 2014-05-22 at 11:20 +0100, James Laver wrote: First thing I’d say is give your project a short-ish name so that if you do release it to the cpan, you won’t die of RSI. Second thing is to pick a root Module name that doesn’t clash with something in cpan. Other than that, don’t worry. So you’d still be able to have Emailsig::DB. Besides, I wouldn’t worry about eventually now, and when you do, I’d expect you’ll decide there are better approaches than what you describe. If the modules are something you think would be useful to someone else, then please do upload them. Or at least make them easily available on github. Great, thanks for the reply James - good advice. I will get coding and hopefully release something soon! Andy
Show your love for OpenSSL
Show your love for OpenSSL with this tee shirt: http://teespring.com/iheartbleedopenssl I Heartbleed OpenSSL Some money from each tee goes to the OpenSSL foundation.
Releasing a module with additional dependencies
Hi guys, A bit of a newbie question on publishing a module. I've just taken over as maintainer of Device::VantagePro and released a new version with some additional features. One of the features (get_timezone) requires DateTime::TimeZone, which was not previously needed. I need some advice as to whether I should: 1. Make DateTime::TimeZone a new mandatory dependency for the whole module, even though people might not need the functionality that relies on it. This is my preference, but is that approach considered to be a Bad Thing? 2. Add DateTime::Timezone as an optional dependency. In this case could someone please point me in the direction of instructions on how to do this (Google is failing me). Currently the module is failing build tests as I have not implemented either of these options. Thanks, Andy
Re: Releasing a module with additional dependencies
Yes. Make it a mandatory dependency. Great, thanks for the quick replies guys - I'll do that. Or if the new dependency is enormous, difficult to install, In my case I'm guessing that most people will have TimeZone installed anyway (or it will be easy to install), so I'll just add it as mandatory. Thanks, Andy
Re: XP-Replacement for Parents
On Thu, 2014-03-27 at 11:48 +, Smylers wrote: Options that I can see: • Ubuntu: I moved my parents onto Ubuntu (now Mint) after getting fed up with many support calls with things not working in Windows. They took to Ubuntu with no problems at all, and I now got far fewer requests to help them out. The OS will install, but I don't know if Mum's scanner will work with it, and I'm pretty sure it won't work in the same way, where pressing a physical button on the scanner causes some HP application to open on the desktop with the scanned document. I can't comment on that particular example, but I've found recent hardware support to be excellent. It always Just Works (except for my Dad's GPS...) Andy
Re: [ANNOUNCE] Damian Conway Speaking at London.pm: Monday, 10th March
On Fri, 2014-03-07 at 11:34 +, Damian Conway wrote: Or maybe people just don't believe they need to learn anything more about regexes. ;-) I have to confess that I didn't read the original invitation properly, and thought that it was aimed at people who have no regex experience. Of course, now that I've read it properly, I realise that is not the case, and I would be very interested if the course is rescheduled. I am certainly of the copy and paste a regex ilk! (although I can't make the originally scheduled date anyway) Andy
[ANNOUNCE] London Perl Mongers March Social - 2014-03-06 - The Antelope SW1W 8EZ
Dear all, The March London.pm Social will be on Thursday 6th March, at The Antelope, just off Sloane Square. The pub was previously used by another group that I'm a member of; at that event the beer scored 8.5/10 (a strong score for that particular group!) http://www.openstreetmap.org/node/954730336 We have the downstairs snug booked from 18:00 (on the left as you enter the pub). Pub food is available until 21:30. 22 Eaton Terrace London SW1W 8EZ http://antelope-eaton-terrace.co.uk/ Nearest tube is Sloane Square, but Victoria is also close. Standard blurb: Social meets are a chance for the various members of the group to meet up face to face and chat with each other about things - both Perl and (mostly) non-Perl - and newcomers are more than welcome. The monthly meets tend to be bigger than the other ad hoc meetings that take place at other times, and we make sure that they're in easy to get to locations and the pub serves food (meaning that people can eat in the bar if they want to). They normally start around 6.30pm (or whenever people get there after work) and a group tends to be left come closing time. If you're a newcomer or other first timer (even if you've been lurking on the mailing list or on IRC) then please seek our Glorious Leader Tom out - we have a tradition that the leader of this motley crew buys the new people a drink and introduces them to people. Andy
Re: FOSDEM trip / accomodation / etc...
On Mon, 2014-01-20 at 23:30 +, Paul LeoNerd Evans wrote: So it seems at somewhat last-minute I'm going to FOSDEM to do a Perl talk. Anyone been before? I'm looking for recommendations on travel and where to stay. Previously I've actually just gone for one day, out and back on the same day using the Eurostar. I missed the first hour or so, so depends what time your talk is, but if you're pushed for time/money it's an option. The first/last trains still look relatively cheap, at about £60 each way. Be warned that the queues for the tram/metro in Brussels can be horrendous, so best buying your tickets on the Eurostar if possible!
Re: Using grep on undefined array
On Wed, 2013-08-14 at 00:09 +0100, Andrew Beverley wrote: Hi, Could someone please explain to me why the following outputs an empty string rather than *? [...] Thanks for all the replies - a very interesting read. I had never even heard of autovivification, so very useful to know what it is. I will tweak my code accordingly! Thanks, Andy
Using grep on undefined array
Hi, Could someone please explain to me why the following outputs an empty string rather than *? get(); sub get($) { my $fields = shift; my @fields = grep $_ ne 'domain', @$fields; my $select_fields = $fields ? join(',', map { 'users.' . $_ } @fields) : '*'; print $select_fields\n; } I would have expected $fields to remain undefined, but it seems to be turning into an empty array during the grep. I've discovered that I can make it work by conditionally declaring @fields (with if $fields), but I'd still like to know what's going on here. Thanks, Andy
Installing WWW::Webkit on Debian/Mint
Hi, Quick question: has anybody had any success installing WWW::Webkit on Debian (v7.1) or Linux Mint (v12)? I won't list all the problems that I am having just now (suffice to say they are various recursive dependency failures that I don't want to spend lots of time on). I'm more interested to know if anyone's installed it without a problem and if there is an easy way to do so (there isn't a Debian package). Alternatively, what are people's recommendations for interacting with a website using a Perl (or indeed other) script? I am trying to automate interaction with a rather clunky HR website at work (to perform a repetitive task), and don't have the luxuries of an API or similar. Thanks, Andy
Re: Installing WWW::Webkit on Debian/Mint
Otherwise WWW::Mechanize. Wow, thanks for that, that was easy. Works well on a test website, just need to try on said clunky HR system tomorrow :)
Re: Installing WWW::Webkit on Debian/Mint
On Mon, 2013-07-22 at 17:35 +, dave.lamb...@gmail.com wrote: I have had good results in the past on resistive websites with WWW::Selenium. Thanks. That will be my fallback if the website is more complicated than I originally thought (it's several years old, so hoping it doesn't have much/any active content).
Scope of variables in a function
Could somebody explain why the following code prints barbar rather than bar please? I am trying to understand why the $result variable in the search function retains its value the second time the function is called. Up until now I had thought that variables in a function defined with my would be empty each time the function was called, so this has caught me out. The code is just proof of concept: I realise that it could be written more efficiently! sub search($) { my $in = shift; my $result = FOO if $in =~ /foo/; $result = $1 if ($in =~ /^(bar)/); return $result; } print search(bar); print search(none); Thanks, Andy
Re: Scope of variables in a function
On Sat, 2013-06-01 at 18:23 +0100, Dirk Koopman wrote: On 01/06/13 18:03, Hakim Cassimally wrote: Andy, I believe your problem is: my $x = 'FOO' if $condition; This only declares the new variable if $condition, so it ends up having surprising, static-like behaviour, which you probably shouldn't rely on. Rewriting to: my $result; $result = 'FOO' if ... gives your expected result. Thanks for all the replies. I assume the above that everyone suggested is the cleanest way of writing it? I try to avoid separately declaring variables where possible, as IMHO it clutters the code. It is very annoying. It certainly cost me a couple of hours of debugging time, especially as it was an intermittent problem! Andy
Re: Scope of variables in a function
On Sat, 2013-06-01 at 19:13 +0200, Paul Johnson wrote: And the official line from perlsyn: NOTE: The behaviour of a my, state, or our modified with a statement modifier conditional or loop construct (for example, my $x if ...) is undefined. The value of the my variable may be undef, any previously assigned value, or possibly anything else. Don't rely on it. Future versions of perl might do something different from the version of perl you try it out on. Here be dragons. Thanks for that. Obviously I should have RTM, but it does seem strange that something that is not recommended does not produce any sort of warnings. Andy
Re: Scope of variables in a function
On Sat, 2013-06-01 at 19:15 +0100, Anthony Lucas wrote: Thanks for that. Obviously I should have RTM, but it does seem strange that something that is not recommended does not produce any sort of warnings. It's not that it's not recommended, it's just that it's usually not what you meant. There's nothing objectively wrong with not reinitialising a variable on every call into your function. From the previously quoted perlsyn text The behaviour ... is undefined... Future versions of perl might do something different from the version of perl you try it out on. That says to me that it should never be used? Andy
Re: Scope of variables in a function
On Sat, 2013-06-01 at 20:33 +0100, Anthony Lucas wrote: Relying on its _contents_ is what you shouldn't be doing. There' a difference. Okay, in which case I reckon there should be a warning when trying to use the contents, when the declaration hasn't happened because of the condition ;-)
Re: Scope of variables in a function
On Sat, 2013-06-01 at 21:08 +0100, Dominic Thoreau wrote: On 1 June 2013 20:59, Andrew Beverley a...@andybev.com wrote: On Sat, 2013-06-01 at 20:33 +0100, Anthony Lucas wrote: Relying on its _contents_ is what you shouldn't be doing. There' a difference. Okay, in which case I reckon there should be a warning when trying to use the contents, when the declaration hasn't happened because of the condition ;-) Although the code you published didn't have 'use warnings;' on it, so I'm suspecting most people thought it wouldn't be there (silly, I know) Yes, I should have explicitly stated that. I've already learnt the lesson of not putting that in my code ;-)
Re: Perl School on Saturday
On Tue, 2013-04-02 at 10:03 +0100, Dave Cross wrote: Hi all, There are still plenty of tickets available for my Perl School on Moose at Google Campus this coming Saturday (6th April). I realise that this course is a repeat in itself, but are there any plans to repeat it again? I'd like to attend, but am due to be away this weekend, so another opportunity would be welcome. Thanks, Andy
Re: Perl School on Saturday
On Wed, 2013-04-03 at 12:27 +0100, David Cantrell wrote: On Wed, Apr 03, 2013 at 09:29:17AM +0100, Dave Cross wrote: Quoting Andrew Beverley a...@andybev.com: On Tue, 2013-04-02 at 10:03 +0100, Dave Cross wrote: There are still plenty of tickets available for my Perl School on Moose at Google Campus this coming Saturday (6th April). I realise that this course is a repeat in itself, but are there any plans to repeat it again? I'd like to attend, but am due to be away this weekend, so another opportunity would be welcome. There are currently no plans to repeat it. Which doesn't mean that it won't be repeated - just that I don't plan very far into the future. If I knew there was a demand then I'd definitely consider it. I demand! +1 from me!
Re: Updating lots of database fields in a single row
On Tue, 2013-01-22 at 22:57 +, Andrew Beverley wrote: I've not been developing with Perl for long, so I'd like to know if there is a better way of writing the following database query (or is there a better place to ask?): Thanks for the many excellent replies. I'll have a play around with them at the weekend and make my decision... It's certainly true that TMTOWTDI! Andy
Updating lots of database fields in a single row
I've not been developing with Perl for long, so I'd like to know if there is a better way of writing the following database query (or is there a better place to ask?): my @fields = qw(field1 field2 field3 field4 field5 field6 field7 ... ); my @updates; foreach my $field (@fields) { push @updates, $field = '$hash-{$field}' if $hash-{$field}; } my $values = join ',', @updates; my $sth = $self-dbh-prepare(UPDATE table SET $values WHERE id = ?); $sth-execute($opdefs_id); Basically, I'd like to update lots of fields in a single database row. Obviously I could write out each updated field individually, but I wondered whether the above is considered tidier, or whether there is a better way altogether? The problem with the above code is that I'm not using placeholders and bind values (which I assume is to be preferred) so I'll also need to escape values as required. Thanks, Andy
Re: cpan you have to see
On Wed, 2012-12-12 at 17:45 +, Gareth Harper wrote: Without commenting on the function of the modules (I personally wouldn't use them, but I can see what you're trying to accomplish). Style/function/speed wise there certainly are a few areas which you may want to address. I'll explain some of the more obvious ones here, though there are several other things you may want to look at. I see several other people have volunteered help as well. [...] Wow, thanks for that Gareth. A very well written and comprehensive email. Andy