Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

Re^4: Make $^V and "my" implicit

by Tux (Monsignor)
on Feb 04, 2014 at 07:21 UTC ( #1073325=note: print w/ replies, xml ) Need Help??

in reply to Re^3: Make $^V and "my" implicit
in thread Make $^V and "my" implicit

The second example (preventing warnings) can just as easily use || and get the same results as //. That said, I have used your example many, many times when writing for a closed environment, mostly for the reasons you give.

No, it cannot, as 0 in most cases is a valid value, very valid. With || it would be replaced.

Just use it, and see how many unmeant problems suddenly disappear.

Where || does work well, is when the default for a value is 0, most of which are boolean-like values:

my $hidden = $cell->{Hidden} || 0;

true values stay, false values (undef, "", 0 and "0" all get replaced with 0. Replacing 0 with 0 does not hurt.

Enjoy, Have FUN! H.Merijn

Comment on Re^4: Make $^V and "my" implicit
Select or Download Code
Re^5: Make $^V and "my" implicit
by boftx (Chaplain) on Feb 04, 2014 at 07:38 UTC

    I was looking specifically at this line:

    my @sorted = sort { ($a->{order} // 0) <=> ($b->{order} // 0) } @aoh;

    In that case, || would indeed give the same result. The same goes if one were dealing with char data and used $foo || ''. Granted, in those cases the assignment would be redundant, but the warning would be avoided when the value in question is not defined. (Which ignores the question of whether or not it should be avoided since it might be an indication of working with bad data to begin with.)

    That said, you and I can both point to cases where what you are saying would be true, especially in the case of char data and having '' (empty string) replaced by a non-empty default would be an error. But in my experience that is the exception to the general use-case and is not a serious problem to deal with.

    It helps to remember that the primary goal is to drain the swamp even when you are hip-deep in alligators.

      In the sorted case example, you are right. But having the correct code as in the example makes a change from 0 to -1 or 999 as default a very easy edit, which is not to say when using ||.

         The same goes if one were dealing with char data and used $foo || ''

      Why do you want to change a perfectly valid string "0" to the empty string "". I seldom want such a change for defaults.

      Enjoy, Have FUN! H.Merijn
      Or save 4 ops and use the following :)
      my @sorted = sort { no warnings 'uninitialized'; $a->{order} <=> $b->{order} } @aoh;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (4)
As of 2014-09-02 04:51 GMT
Find Nodes?
    Voting Booth?

    My favorite cookbook is:

    Results (20 votes), past polls