Re: Syntax "||" before sub
> On Nov 24, 2018, at 12:49 PM, David Precious wrote: > > I went to perldoc perlop expecting to be able to find a > section to point the OP at as a "here's the documentation for it", and > couldn't find anything particularly useful. I was able to find this in “perldoc perlop”: Assignment Operators "=" is the ordinary assignment operator. Assignment operators work as in C. That is, $x += 2; is equivalent to $x = $x + 2; although without duplicating any side effects that dereferencing the lvalue might trigger, such as from "tie()". Other assignment operators work similarly. The following are recognized: **=+=*=&=&.=<<=&&= -=/= |=|.=>>=||= .=%=^=^.= //= x= -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Syntax "||" before sub
On Thu, 22 Nov 2018 12:33:25 -0800 "John W. Krahn" wrote: > On 2018-11-22 8:08 a.m., David Precious wrote: > > > > > > You'll often see these operators used to provide default values. > > > > e.g. > > > >sub hello { > >my $name = shift; > >$name ||= 'Anonymous Person'; > > Which is usually written as: > > sub hello { > my $name = shift || 'Anonymous Person'; Sure - but that doesn't provide a simple, useful example of ||= which the OP was asking about. > > I do notice that there isn't actually a very useful section on ||= > > and //= - I may try to raise a pull requests to add more > > documentation on them. > > $var ||= 'VALUE'; > > Is just shorthand for: > > $var = $var || 'VALUE'; > > The syntax is borrowed from the C programming language and it is > slightly more efficient when compiled to machine code. Indeed - *I* know that, but it strikes me that it could be better documented, to make it easier for people new to Perl to find the documentation. I went to perldoc perlop expecting to be able to find a section to point the OP at as a "here's the documentation for it", and couldn't find anything particularly useful. That strikes me as sub-optimal :) -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Syntax "||" before sub
On Thu, Nov 22, 2018 at 12:33:25PM -0800, John W. Krahn wrote: > On 2018-11-22 8:08 a.m., David Precious wrote: > > > > You'll often see these operators used to provide default values. > > > > e.g. > > > >sub hello { > >my $name = shift; > >$name ||= 'Anonymous Person'; > > Which is usually written as: > >sub hello { >my $name = shift || 'Anonymous Person'; Or, nowadays, and if your perl version(s) support it, as: sub hello ($name = "Anonymous Person") { -- Paul Johnson - p...@pjcj.net http://www.pjcj.net -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Syntax "||" before sub
On 2018-11-22 8:08 a.m., David Precious wrote: You'll often see these operators used to provide default values. e.g. sub hello { my $name = shift; $name ||= 'Anonymous Person'; Which is usually written as: sub hello { my $name = shift || 'Anonymous Person'; I do notice that there isn't actually a very useful section on ||= and //= - I may try to raise a pull requests to add more documentation on them. $var ||= 'VALUE'; Is just shorthand for: $var = $var || 'VALUE'; The syntax is borrowed from the C programming language and it is slightly more efficient when compiled to machine code. John -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Syntax "||" before sub
On Thu, 22 Nov 2018 13:48:18 + James Kerwin wrote: > Hi All, > > I'm looking through some Perl files for software we use and I noticed > this in one of the config files: > > $c->{guess_doc_type} ||= sub { > > All other similar config files show a similar structure, but without > the "||" before the equals sign: > > $c->{validate_document} = sub { > > My question is, what is the "||" doing in the first example? "||=" is the conditional assignment operator - it assigns the value on the right-hand side to the thing on the left hand side, *but* only if the thing on the left hand side doesn't evaluate to a true value. For instance, my $foo; $foo ||= "Bar"; # $foo is now "Bar" - because it wasn't true before $foo ||= "Baz"; # $foo is still "Bar" - it wasn't changed In your example, the value on the right hand side is a coderef = so, if $c->{guess_doc_type} didn't already contain something truthy, then it will be set to a coderef defined by the sub { ... } block. See also "//=", the defined-or operator, which works in pretty much the same way, but checking for definedness - the difference being that if the left hand side contained something that wasn't a truthy value, ||= would overwrite it, whereas //= wouldn't. You'll often see these operators used to provide default values. e.g. sub hello { my $name = shift; $name ||= 'Anonymous Person'; return "Hi there, $name!"; } > I've attempted to Google this, but found no solid answer. Yeah, Googling for operators when you don't know their name is rarely useful :) Perl's operators are all documented quite well at: https://perldoc.perl.org/perlop.html I do notice that there isn't actually a very useful section on ||= and //= - I may try to raise a pull requests to add more documentation on them. Cheers Dave P -- To unsubscribe, e-mail: beginners-unsubscr...@perl.org For additional commands, e-mail: beginners-h...@perl.org http://learn.perl.org/
Re: Syntax "||" before sub
Hi Sebastian, Thanks for that info! It's much more thorough than what I discovered about this. I'll keep it in mind as I delve deeper... Thanks, James On Thu, Nov 22, 2018 at 1:58 PM James Kerwin wrote: > Ahhh sorrry to reply to my own post but I found this: > > https://stackoverflow.com/questions/29302181/what-is-in-perl-for > > I assume then that if " guess_doc_type" evaluates to false it moves on to > the defined subroutine. My problem is that I can't find where " > guess_doc_type" is defined... which is not a problem for this list to solve! > > Thanks, > James > > On Thu, Nov 22, 2018 at 1:48 PM James Kerwin > wrote: > >> Hi All, >> >> I'm looking through some Perl files for software we use and I noticed >> this in one of the config files: >> >> $c->{guess_doc_type} ||= sub { >> >> All other similar config files show a similar structure, but without the >> "||" before the equals sign: >> >> $c->{validate_document} = sub { >> >> My question is, what is the "||" doing in the first example? >> >> It would be useful to know before I go changing anything. My instinct is >> that it's an "or" but that really doesn't make sense in this context. >> >> I've attempted to Google this, but found no solid answer. >> >> Thanks, >> James >> >
Re: Syntax "||" before sub
Ahhh sorrry to reply to my own post but I found this: https://stackoverflow.com/questions/29302181/what-is-in-perl-for I assume then that if " guess_doc_type" evaluates to false it moves on to the defined subroutine. My problem is that I can't find where " guess_doc_type" is defined... which is not a problem for this list to solve! Thanks, James On Thu, Nov 22, 2018 at 1:48 PM James Kerwin wrote: > Hi All, > > I'm looking through some Perl files for software we use and I noticed this > in one of the config files: > > $c->{guess_doc_type} ||= sub { > > All other similar config files show a similar structure, but without the > "||" before the equals sign: > > $c->{validate_document} = sub { > > My question is, what is the "||" doing in the first example? > > It would be useful to know before I go changing anything. My instinct is > that it's an "or" but that really doesn't make sense in this context. > > I've attempted to Google this, but found no solid answer. > > Thanks, > James >
Syntax "||" before sub
Hi All, I'm looking through some Perl files for software we use and I noticed this in one of the config files: $c->{guess_doc_type} ||= sub { All other similar config files show a similar structure, but without the "||" before the equals sign: $c->{validate_document} = sub { My question is, what is the "||" doing in the first example? It would be useful to know before I go changing anything. My instinct is that it's an "or" but that really doesn't make sense in this context. I've attempted to Google this, but found no solid answer. Thanks, James