Beefy Boxes and Bandwidth Generously Provided by pair Networks
Keep It Simple, Stupid
 
PerlMonks  

Re^6: a *working* JSON module (Perl's Debugger), related issues

by hippo (Bishop)
on Oct 30, 2021 at 10:15 UTC ( #11138255=note: print w/replies, xml ) Need Help??


in reply to Re^5: a *working* JSON module (Perl's Debugger), related issues
in thread Can someone please write a *working* JSON module

Q4) What operator is this? I believe it takes my global value of 1 for $debug and makes it zero

It is the defined-or-equals operator. It assigns the RHS to the LHS only if the LHS is undefined. It is listed in perlop along with all the other operators.


🦛

  • Comment on Re^6: a *working* JSON module (Perl's Debugger), related issues

Replies are listed 'Best First'.
Re^7: a *working* JSON module (Perl's Debugger), related issues
by Aldebaran (Curate) on Nov 01, 2021 at 06:07 UTC
    It is the defined-or-equals operator. It assigns the RHS to the LHS only if the LHS is undefined. It is listed in perlop along with all the other operators.

    Thanks, I was reading it wrong. I wouldn't say the documentation is particularly thick about it:

    $ perldoc perlop | grep '//=' .= %= ^= ^.= //= $

    , but I see why bliako uses it, and think I'll borrow the scheme.

      Just to expand on previous answer with some example. An operator of the form $LHS <OPERATOR>= $RHS (I think the general name is "shorthand"?) is the shorthand for $LHS = $LHS <OPERATOR> $RHS. (Edit: s/LHW/LHS/) E.g., the long way is: my $x = undef; my $x = $x // 12;. The shorthand is $x //= 12;. Many other operators follow this style, for example $x = 12; $x += 3; # $x is 15. But also $x = 0; $x ||= 1; Also, $x = 10; $x /= 2; # 5

      my $debug = 1; sub get_elevation_from_coordinates { my ($lat, $lon, $debug) = @_; $debug //= 0; ... }

      Now, in the context of the above quoted code which I think you refer to. There are 2 $debug there! One belongs to the outer scope of the sub. It will be valid inside the sub unless the sub declares (my $debug) its own private one. In this case it's an optional sub parameter who is set to zero if left undef (by the caller of the sub, e.g. in this case: get_elevation_from_coordinates(1,2)). That code would have been better like this:

      our $debug = 1; # OUR! sub get_elevation_from_coordinates { my ($lat, $lon, $debug) = @_; $debug //= $main::debug; # if none specified, use "global" (main's +) ... }

      In which case there's a global debug which is used to set its sub's debug value only if the caller did not specify one.

      This document is helpful to me: https://perl.plover.com/FAQs/Namespaces.html

      bw, bliako

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: note [id://11138255]
help
Chatterbox?
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others musing on the Monastery: (2)
As of 2022-05-26 02:30 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?
    Do you prefer to work remotely?



    Results (92 votes). Check out past polls.

    Notices?