Beefy Boxes and Bandwidth Generously Provided by pair Networks
Just another Perl shrine
 
PerlMonks  

Error in Camel Book Ver2?

by Anonymous Monk
on Dec 21, 2000 at 09:55 UTC ( [id://47738]=perlquestion: print w/replies, xml ) Need Help??

Anonymous Monk has asked for the wisdom of the Perl Monks concerning the following question:

Hello,

I'm perplexed on this line, from the camel book, second edition, pg 540:

Similarily, set default values with:

$pi || = 3;

Which, doesn't really makes sense, and doesn't well, work anyways, am I missing something?

I personally am trying to shorten this piece of code:

unless($list_info{subscribed_message} ne "" || exists $list_info{subsc +ribed_message}) {$list_info{subscribed_message} = $subscribed_message}

Which is trying to set a default variable to something (a copy of a tied hash) that may not be there. There's about 10 of those nasty lines, so a for loop looks inticing.

Any help will be apreciated :)

Replies are listed 'Best First'.
Re: Error in Camel Book Ver2?
by redcloud (Parson) on Dec 21, 2000 at 10:14 UTC

    Well, look at that example line this way:

    $pi ||= 3;

    is like saying:

    $pi = $pi || 3;

    Now, the || operator short circuits if the first operand is true, so if $pi has any value that is true (i.e. it is not 0, "", or undef -- any others?) then the value of the right hand side will be $pi, otherwise it'll be 3. So, this is a way to say that the default value of $pi is 3, because that's what it'll be set to if it hasn't been set to something else already.

    So, in your case, you might do:

    $list_info{subscribed_message} ||= $subscribed_message;

    Make sense?

    Update:This doesn't work, though if "" is a valid value.

Re: Error in Camel Book Ver2?
by chipmunk (Parson) on Dec 21, 2000 at 10:23 UTC
    Nope, no error there. $pi ||= 3 is basically equivalent to $pi = ($pi || 3) which could be described as "If $pi doesn't already have a true value, then set $pi's value to 3." (The 'true' part should not be overlooked.)

    So, you could write your assignment as: $list_info{subscribed_message} ||= $subscribed_message; That will set the value of $list_info{subscribed_message} to the value of $subscribed_message, if $list_info{subscribed_message} doesn't exist, or is undef, "", or 0.

      if I make a script that says:

      $pi ||= 3 print $pi;

      I'll get back something like

      syntax error at or.pl line 1, near "|| ="

      So the way its written won't work

      I wonder if this will be different if your testing a hash, since you use exists() instead of defined();

        syntax error at or.pl line 1, near "|| ="
        That's a Perl4 (or earlier) error message. Please upgrade to a version of Perl that was released sometime after the Spice Girls hit the pop charts.

        -- Randal L. Schwartz, Perl hacker

        The code that you provide there is not the code giving you that error message. The error message you list indicates that you have an extraneous space in ||=. ||= should not have any spaces.

        The code you provided will give you an entirely different error, namely syntax error at - line 2, near "print" since you didn't have a semicolon at the end of the first line.

Re: Error in Camel Book Ver2?
by Dominus (Parson) on Dec 22, 2000 at 08:31 UTC
    Says nobody in particular:
    unless($list_info{subscribed_message} ne "" || exists $list_info{subscribed_message}) { $list_info{subscribed_message} = $subscribed_message }

    The first thing I notice here is that your test is redundant. If x ne "" is true, then exists x must also be true. (Or equivalently, if exists $h{k} is not true, then k is not in the hash, so $h{k} must be equal to "".) So you should rewrite this as:

    if ($list_info{subscribed_message} eq "") { $list_info{subscribed_message} = $subscribed_message; }
    which is something of an improvement already.

    Now, perhaps this would be a good place to use a mutator subroutine?

    sub set_if_not_set_already { $_[0] = $_[1] if $_[0] eq ""; } set_if_not_set_already($list_info{subscribed_message}, $subscribed_message);
    Or you could abbreviate a little futher and get rid of the mutator:
    sub initialize_list_info { $list_info{$_[0]} = $_[1] if list_info{$_[0]} eq ""; } initialize_list_info('subscribed_message', $subscribed_message);
    This is neither a recommendation nor a disrecommendation. Just something to consider.

Log In?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others browsing the Monastery: (2)
As of 2024-04-26 04:13 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found