Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

double slash operator syntax question

1 view
Skip to first unread message

Lars Eighner

unread,
Aug 10, 2008, 9:35:10 PM8/10/08
to
I may have asked this in another form recently, but I'm senile, so indulge
me.

What I want to do: if $lb_lang is undefined, I want to set it.

What I wrote:

$lb_lang // {$lb_lang = 'en'};

What perl -w yelled:

Useless use of single ref constructor in void context at etc.
But it seems to work in spite of the yelling.

If I omit the braces, it dies with:
Can't modify defined or (//) at etc.


My understanding of what // is supposed to be: It is like
|| but tests the left side for definedness instead of for value,
so this should be like a || command list.

--
Lars Eighner <http://larseighner.com/> use...@larseighner.com
War on Terrorism: Okay, Unleash OUR Extreme Fundamentalists
"... all of them who have tried to secularize America, I point the finger in
their face and say, 'You helped this happen.'" --Jerry Falwell

Matt

unread,
Aug 10, 2008, 9:52:54 PM8/10/08
to
I don't know about // being some kind of operator other than a pattern
match operator...but you could accomplish that like this

$lb_lang = $lb_lang ? $lb_lang : "en";
or like this
$lb_lang = $lb_lang || "en";

I think either of those should work.

Ben Morrow

unread,
Aug 10, 2008, 10:55:10 PM8/10/08
to

Quoth Lars Eighner <use...@larseighner.com>:

> I may have asked this in another form recently, but I'm senile, so indulge
> me.
>
> What I want to do: if $lb_lang is undefined, I want to set it.
>
> What I wrote:
>
> $lb_lang // {$lb_lang = 'en'};

$lb_lang //= 'en';

> What perl -w yelled:
>
> Useless use of single ref constructor in void context at etc.
> But it seems to work in spite of the yelling.

Well, sort-of. You are actually constructing an anon hashref out of the
single-element list ('en') (and I'm slightly surprised -w didn't warn
about that, as well), and assigning to $lb_lang as a side-effect. Using
the array constructor

$lb_lang // [$lb_lang = 'en'];

would work just as well; or, since you don't need to build a useless
data structure,

$lb_lang // do { $lb_lang = 'en' };

'do' makes the block into a block, rather than an anon hash.
(Annoyingly, the 'err' operator which was the low-precedence form of //
was removed just before 5.10.0. I never really understood why...)

> My understanding of what // is supposed to be: It is like
> || but tests the left side for definedness instead of for value,
> so this should be like a || command list.

Yes, exactly.

$lb_lang || {$lb_lang = 'en'};

behaves exactly the same way (except for the test performed).

Ben

--
All persons, living or dead, are entirely coincidental.
b...@morrow.me.uk Kurt Vonnegut

Michael Carman

unread,
Aug 10, 2008, 11:17:30 PM8/10/08
to
Matt wrote:
> I don't know about // being some kind of operator other than a pattern
> match operator...but you could accomplish that like this

It's the defined-or operator that was added in Perl 5.10. It's like ||
but tests for definedness instead of truth.

> $lb_lang = $lb_lang ? $lb_lang : "en";

> $lb_lang = $lb_lang || "en";
>
> I think either of those should work.

Not quite; those both test for boolean truth. The OP stated that he
wanted to set the variable if it wasn't _defined_. The important
difference is the behavior if the initial value of $lb_lang is defined
but false (0, '0', or '')

The typical ways of doing this prior to Perl 5.10 are:

$lb_lang = defined($lb_lang) ? $lb_lang : 'en';

or

$lb_lang = 'en' unless defined $lb_lang;

-mjc

Lars Eighner

unread,
Aug 10, 2008, 11:55:18 PM8/10/08
to
In our last episode,
<ebOnk.292718$yE1.188967@attbi_s21>,
the lovely and talented Michael Carman
broadcast on comp.lang.perl.misc:

> Matt wrote:
>> I don't know about // being some kind of operator other than a pattern
>> match operator...but you could accomplish that like this

> It's the defined-or operator that was added in Perl 5.10. It's like ||
> but tests for definedness instead of truth.

It's in 5.8.8 or my perl is lying.

If it wasn't for muscle spasms, I wouldn't get any exercise at all.

Peter J. Holzer

unread,
Aug 11, 2008, 3:47:30 AM8/11/08
to
On 2008-08-11 02:55, Ben Morrow <b...@morrow.me.uk> wrote:
> Quoth Lars Eighner <use...@larseighner.com>:

>> What I want to do: if $lb_lang is undefined, I want to set it.
>>
>> What I wrote:
>>
>> $lb_lang // {$lb_lang = 'en'};
>
> $lb_lang //= 'en';
>
>> What perl -w yelled:
>>
>> Useless use of single ref constructor in void context at etc.
>> But it seems to work in spite of the yelling.
>
> Well, sort-of. You are actually constructing an anon hashref out of the
> single-element list ('en') (and I'm slightly surprised -w didn't warn
> about that, as well), and assigning to $lb_lang as a side-effect.
[...]

> since you don't need to build a useless data structure,
>
> $lb_lang // do { $lb_lang = 'en' };

Or just

$lb_lang // ($lb_lang = 'en');

hp

Peter J. Holzer

unread,
Aug 11, 2008, 3:49:13 AM8/11/08
to
On 2008-08-11 03:55, Lars Eighner <use...@larseighner.com> wrote:
> Michael Carman:

>> Matt wrote:
>>> I don't know about // being some kind of operator other than a pattern
>>> match operator...but you could accomplish that like this
>
>> It's the defined-or operator that was added in Perl 5.10. It's like ||
>> but tests for definedness instead of truth.
>
> It's in 5.8.8 or my perl is lying.
>
I think your perl is lying. My perl 5.8.8 complains:

Search pattern not terminated at ./foo line 6.

hp

Matt

unread,
Aug 11, 2008, 4:04:33 AM8/11/08
to

Thanks for catching that "defined" thing Michael.

Lars Eighner

unread,
Aug 11, 2008, 4:33:31 AM8/11/08
to
In our last episode, <slrng9vrnp.2g...@hrunkner.hjp.at>, the
lovely and talented Peter J. Holzer broadcast on comp.lang.perl.misc:

Curious. As I said, it works on mine, and perl -V has this
to say for itself:

Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
Platform:
osname=freebsd, osvers=7.0-stable, archname=i386-freebsd-64int
uname='freebsd debranded.6dollardialup.com 7.0-stable freebsd \
7.0-stable #0: mon apr 21 11:15:06 cdt 2008 \
to...@debranded.6dollardialup.com:usrobjusrsrcsysapr08 i386 '
config_args='-sde -Dprefix=/usr/local \
-Darchlib=/usr/local/lib/perl5/5.8.8/mach \
-Dprivlib=/usr/local/lib/perl5/5.8.8 \
-Dman3dir=/usr/local/lib/perl5/5.8.8/perl/man/man3 \
-Dman1dir=/usr/local/man/man1 \
-Dsitearch=/usr/local/lib/perl5/site_perl/5.8.8/mach \
-Dsitelib=/usr/local/lib/perl5/site_perl/5.8.8 \
-Dscriptdir=/usr/local/bin \
-Dsiteman3dir=/usr/local/lib/perl5/5.8.8/man/man3 \
-Dsiteman1dir=/usr/local/man/man1 \
-Ui_malloc -Ui_iconv -Uinstallusrbinperl \
-Dcc=cc -Duseshrplib \
-Dccflags=-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.8/BSDPAN" \
-Doptimize=-O2 -fno-strict-aliasing -pipe -Ud_dosuid -Ui_gdbm \
-Dusethreads=n -Dusemymalloc=y -Duse64bitint'
hint=recommended, useposix=true, d_sigaction=define
usethreads=undef use5005threads=undef useithreads=undef \
usemultiplicity=undef
useperlio=define d_sfio=undef uselargefiles=define usesocks=undef
use64bitint=define use64bitall=undef uselongdouble=undef
usemymalloc=y, bincompat5005=undef
Compiler:
cc='cc', ccflags ='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.8/BSDPAN" \
-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe \
-Wdeclaration-after-statement -I/usr/local/include',
optimize='-O2 -fno-strict-aliasing -pipe ',
cppflags='-DAPPLLIB_EXP="/usr/local/lib/perl5/5.8.8/BSDPAN" \
-DHAS_FPSETMASK -DHAS_FLOATINGPOINT_H -fno-strict-aliasing -pipe \
-Wdeclaration-after-statement -I/usr/local/include'
ccversion='', gccversion='4.2.1 20070719 [FreeBSD]', gccosandvers=''
intsize=4, longsize=4, ptrsize=4, doublesize=8, byteorder=12345678
d_longlong=define, longlongsize=8, d_longdbl=define, longdblsize=12
ivtype='long long', ivsize=8, nvtype='double', \
nvsize=8, Off_t='off_t', lseeksize=8
alignbytes=4, prototype=define
Linker and Libraries:
ld='cc', ldflags =' -Wl,-E

"We have no opinion on your Arab - Arab conflicts, such as your dispute with
Kuwait." -- Bush's Ambassador April Glaspie, giving Saddam Hussein
the greenlight to invade Kuwait.

Peter J. Holzer

unread,
Aug 11, 2008, 5:01:04 AM8/11/08
to
On 2008-08-11 08:33, Lars Eighner <use...@larseighner.com> wrote:
> In our last episode, <slrng9vrnp.2g...@hrunkner.hjp.at>, the
> lovely and talented Peter J. Holzer broadcast on comp.lang.perl.misc:
>
>> On 2008-08-11 03:55, Lars Eighner <use...@larseighner.com> wrote:
>>> Michael Carman:
>>>> Matt wrote:
>>>>> I don't know about // being some kind of operator other than a pattern
>>>>> match operator...but you could accomplish that like this
>>>
>>>> It's the defined-or operator that was added in Perl 5.10. It's like ||
>>>> but tests for definedness instead of truth.
>>>
>>> It's in 5.8.8 or my perl is lying.
>>>
>> I think your perl is lying. My perl 5.8.8 complains:
>
>> Search pattern not terminated at ./foo line 6.
>
> Curious. As I said, it works on mine, and perl -V has this
> to say for itself:
>
> Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
> Platform:

Here's my test program:

#!/usr/bin/perl
use warnings;
use strict;

my $lb_lang = $ARGV[0];


$lb_lang // ($lb_lang = 'en');

print "$lb_lang\n"
__END__

hp

Lars Eighner

unread,
Aug 11, 2008, 6:56:08 AM8/11/08
to
In our last episode, <slrng9vvug.36...@hrunkner.hjp.at>, the

runs perfectly (because there is a link to the real location of perl
in freeBSD)

The source my perl was compiled from is perl-5.8.8.tar.bz2 . I don't
even have source for 5.10.x on my machine.

I don't get it.

Due to a mixup in Urology, orange juice will not be served this morning.

Justin C

unread,
Aug 11, 2008, 9:42:47 AM8/11/08
to

Just an observation, I don't know if it makes a difference[1], the OP had
{} and not () after the //

Justin.


1. That's why I'm here, 'cos I don't know!
--
Justin C, by the sea.

Ben Morrow

unread,
Aug 11, 2008, 12:13:47 PM8/11/08
to

Quoth Lars Eighner <use...@larseighner.com>:

> In our last episode, <slrng9vrnp.2g...@hrunkner.hjp.at>, the
> lovely and talented Peter J. Holzer broadcast on comp.lang.perl.misc:
>
> > On 2008-08-11 03:55, Lars Eighner <use...@larseighner.com> wrote:
> >> Michael Carman:
> >>> Matt wrote:
> >>>> I don't know about // being some kind of operator other than a pattern
> >>>> match operator...but you could accomplish that like this
> >>
> >>> It's the defined-or operator that was added in Perl 5.10. It's like ||
> >>> but tests for definedness instead of truth.
> >>
> >> It's in 5.8.8 or my perl is lying.
> >>
> > I think your perl is lying. My perl 5.8.8 complains:
>
> > Search pattern not terminated at ./foo line 6.
>
> Curious. As I said, it works on mine, and perl -V has this
> to say for itself:
>
> Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
> Platform:
> osname=freebsd, osvers=7.0-stable, archname=i386-freebsd-64int
> uname='freebsd debranded.6dollardialup.com 7.0-stable freebsd \
> 7.0-stable #0: mon apr 21 11:15:06 cdt 2008 \
> to...@debranded.6dollardialup.com:usrobjusrsrcsysapr08 i386 '

This is the perl from ports, yes? ports perl applies the dor patch by
default.

Ben

--
For far more marvellous is the truth than any artists of the past imagined!
Why do the poets of the present not speak of it? What men are poets who can
speak of Jupiter if he were like a man, but if he is an immense spinning
sphere of methane and ammonia must be silent? [Feynmann] b...@morrow.me.uk

Lars Eighner

unread,
Aug 11, 2008, 5:57:23 PM8/11/08
to
In our last episode, <r256n5-...@osiris.mauzo.dyndns.org>, the lovely
and talented Ben Morrow broadcast on comp.lang.perl.misc:


> Quoth Lars Eighner <use...@larseighner.com>:
>> In our last episode, <slrng9vrnp.2g...@hrunkner.hjp.at>, the
>> lovely and talented Peter J. Holzer broadcast on comp.lang.perl.misc:
>>
>> > On 2008-08-11 03:55, Lars Eighner <use...@larseighner.com> wrote:
>> >> Michael Carman:
>> >>> Matt wrote:
>> >>>> I don't know about // being some kind of operator other than a pattern
>> >>>> match operator...but you could accomplish that like this
>> >>
>> >>> It's the defined-or operator that was added in Perl 5.10. It's like ||
>> >>> but tests for definedness instead of truth.
>> >>
>> >> It's in 5.8.8 or my perl is lying.
>> >>
>> > I think your perl is lying. My perl 5.8.8 complains:
>>
>> > Search pattern not terminated at ./foo line 6.
>>
>> Curious. As I said, it works on mine, and perl -V has this
>> to say for itself:
>>
>> Summary of my perl5 (revision 5 version 8 subversion 8) configuration:
>> Platform:
>> osname=freebsd, osvers=7.0-stable, archname=i386-freebsd-64int
>> uname='freebsd debranded.6dollardialup.com 7.0-stable freebsd \
>> 7.0-stable #0: mon apr 21 11:15:06 cdt 2008 \
>> to...@debranded.6dollardialup.com:usrobjusrsrcsysapr08 i386 '

> This is the perl from ports, yes? ports perl applies the dor patch by
> default.

Apparently the perl58 port does. the perl5 port is 5.6

Good judgment comes from experience. Experience comes from bad judgment.

comp.lang.c++

unread,
Aug 13, 2008, 7:51:32 PM8/13/08
to
On Aug 10, 6:35 pm, Lars Eighner <use...@larseighner.com> wrote:
> I may have asked this in another form recently, but I'm senile, so indulge
> me.
>
> What I want to do: if $lb_lang is undefined, I want to set it.
>
> What I wrote:
>
> $lb_lang // {$lb_lang = 'en'};
>
> What perl -w yelled:
>
> Useless use of single ref constructor in void context at etc.
> But it seems to work in spite of the yelling.

If you're able to upgrade, you'll get even more
yelling :)

# /opt/perl-5.10.0/bin/perl -w


$lb_lang // {$lb_lang = "en"}

^D
Useless use of anonymous hash ({}) in void context at -e line 1.
Odd number of elements in anonymous hash at -e line 1.
en


>
> If I omit the braces, it dies with:
> Can't modify defined or (//) at etc.
>

Hm, with 5.8.8, I see a different yell:

# /opt/perl-5.8.8/bin/perl -w


$lb_lang // $lb_lang = "en"

^D
Search pattern not terminated at - line 1

--
Charles DeRykus

0 new messages