Beefy Boxes and Bandwidth Generously Provided by pair Networks
Syntactic Confectionery Delight
 
PerlMonks  

comment on

( [id://3333]=superdoc: print w/replies, xml ) Need Help??

In general, where X is an existing operator, then:

$foo X= $bar; # is short for: $foo = $foo X $bar;

Practical examples:

$foo += $bar; # is short for: $foo = $foo + $bar; $foo ||= $bar; # is short for: $foo = $foo || $bar; $foo .= $bar; # is short for: $foo = $foo . $bar; $foo *= $bar; # is short for: $foo = $foo * $bar;

This convention originally comes from Algol 68, but was made popular by C/C++ and is also available in many other languages (Java, Ruby, Python, Javascript, PHP, etc).

I would suppose that the reason these aren't documented in detail in perlop would be that people are expected to be already familiar with the convention. The Perl documentation probably shouldn't assume familiarity with other programming languages, but (especially for the older parts of the manual) they often seem to.

By the way, what the code you originally posted does is this: it creates a file handle $FH by opening the file with IO::File, but uses a hash %OUTFH in order to cache the file handles, and avoid opening the same file twice.

A longer way of writing the same thing would be:

$OUTFH{$name} = $OUTFH{$name} || IO::File->new(">g:\\perl_scripts\\$na +me.log"); $FH = $OUTFH{$name} or die $!;

It's actually a fairly common construct. The poor man's memoization...

my $result = $cache{$input} ||= my_function($input);

... and probably ought to be documented in some of those lists of Perl idioms.

perl -E'sub Monkey::do{say$_,for@_,do{($monkey=[caller(0)]->[3])=~s{::}{ }and$monkey}}"Monkey say"->Monkey::do'

In reply to Re: ||= (poorly documented?) by tobyink
in thread ||= (poorly documented?) by live4tech

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post; it's "PerlMonks-approved HTML":



  • Are you posting in the right place? Check out Where do I post X? to know for sure.
  • Posts may use any of the Perl Monks Approved HTML tags. Currently these include the following:
    <code> <a> <b> <big> <blockquote> <br /> <dd> <dl> <dt> <em> <font> <h1> <h2> <h3> <h4> <h5> <h6> <hr /> <i> <li> <nbsp> <ol> <p> <small> <strike> <strong> <sub> <sup> <table> <td> <th> <tr> <tt> <u> <ul>
  • Snippets of code should be wrapped in <code> tags not <pre> tags. In fact, <pre> tags should generally be avoided. If they must be used, extreme care should be taken to ensure that their contents do not have long lines (<70 chars), in order to prevent horizontal scrolling (and possible janitor intervention).
  • Want more info? How to link or How to display code and escape characters are good places to start.
Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
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