Beefy Boxes and Bandwidth Generously Provided by pair Networks
Your skill will accomplish
what the force of many cannot

operation inside reference

by hegotf (Novice)
on May 18, 2013 at 13:30 UTC ( #1034095=perlquestion: print w/replies, xml ) Need Help??
hegotf has asked for the wisdom of the Perl Monks concerning the following question:

Hi Monks

Can anyone explain what does that do? @{$doc->{links} // []}

I don't understand the // [] part.

here is the link to entire code:

Replies are listed 'Best First'.
Re: operation inside reference
by AnomalousMonk (Chancellor) on May 18, 2013 at 13:45 UTC

    $doc->{links} is assumed to be either an array reference or undefined. If it is undefined, the  // defined-or operator (new with Perl version 5.10, see Logical Defined-Or in perlop) will evaluate the  [] anonymous array constructor to create a reference to an empty array. The  @{ } expression will de-reference to a (possibly empty) list either the valid  $doc->{links} reference or the empty reference. (Any expression may be evaluated within a  @{ } dereference.) If the  // operator had not been available, the  || operator would probably have served as well since all genuine references are true and the undefined value is false.

Re: operation inside reference
by LanX (Chancellor) on May 18, 2013 at 14:21 UTC
    just to add some code example to the excellent explanation by AnomalousMonk

    DB<116> use warnings; $a=undef; print "< @{$a} >" Use of uninitialized value in array dereference at (eval 49)[multi_per] line 2. < > DB<117> use warnings; $a=[]; print "< @{$a} >" < > DB<118> use warnings; $a=undef//[]; print "< @{$a} >" < > DB<120> use warnings; $a=[1..3]//[]; print "< @{$a} >" < 1 2 3 >

    As you can see generating a list from an nonexistent or undef value causes a warning!

    Defaulting to an empty array solves this issue.

    Cheers Rolf

    ( addicted to the Perl Programming Language)

Re: operation inside reference
by hegotf (Novice) on May 18, 2013 at 14:49 UTC

    Thanks for your help :)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://1034095]
Approved by Athanasius
Front-paged by LanX
[marto]: Corion, not bringing the kids would make it ideal :P
[marto]: a suggestion, have one project in the list that isn't programming/perl related
[Corion]: marto: Yeah, things are more parent-friendly the way they are ;))
[marto]: some hardware/makey thing
[Corion]: marto: Ah, I'm currently drifting to that again, over"painting" (well with Inkscape) one of my photographs and thinking about vectorizing some other images ...
[marto]: Corion, yes something 'artistic', so that or some music. Even combine some things, build a musical instrument :)
[Corion]: marto: Naah, I'm avoiding that rabbit hole ;) Sometimes I think of using Perl to create MIDI to generate music, or at least the harmonies to which I could jam, but then that would eat even more into available time ;-))

How do I use this? | Other CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (4)
As of 2017-07-27 07:47 GMT
Find Nodes?
    Voting Booth?
    I came, I saw, I ...

    Results (404 votes). Check out past polls.