Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Chriss Miller on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Rarely used operators.

Status
Not open for further replies.

Coderifous

Programmer
Dec 13, 2001
782
US
I've been reading my Perl book and have noted some operators which aren't explained very well. I'm sure they are second nature to more experienced programmers, but the the young and naive like myself, are foreign.

Could someone please explain and provide and example of use for the following operators:

>> << ~

The first two I understand are related. Basically they return the left operand shifted (bitwise) by [right operand] places. SO 4 << 2 returns 16 by doing this:
00100 shift two places to the left to become: 10000
Is this correct? And if so... how in the heck does 'print <<EOF' work? I understand that the 'EOF' part can is arbitrary, but how does the bitshift operator make this 'majic' happen?

And the ~ operator - I'm lost on.

--Jim
 
as operators '>>' and '<<' do work as bitwise binary shift operators, as you suspected. However, in the case of the print this is a hangover from the UNIX days and is part of the 'here document' concept. It's a special case due to there being a space preceeding the '<<'.

The '~' operator, is a bitwise not, such that

$myvar = ~1010;

would equal 0101 in binary.

Barbie. Leader of Birmingham Perl Mongers
 
ahhh hmmmm... I'm getting closer to understanding these operators. Can you give me an example of any practical application they might have?
 
Not sure of their use in web type stuff, but in embedded C, which is where I last used then, they were used to shift patterns on LEDs, and lamp arrays. I suppose they could be used in image manipulation, for creating some strange patterns.

Barbie. Leader of Birmingham Perl Mongers
 
They are usually used in maniplating bit strings. There are not many cgi applications where you need to do this, but then perl wasn't originally developed for cgi applications, it was originally developed to assist *nix system operators in performing their jobs. It just turned out to be one of the most useful and flexible languages available for cgi applications as well.

Missbarbell: The space in front of the << shouldn't really have much to do with it's functionality in the &quot;here doc&quot; usage, it's the context. These are all bit-shift context:
Code:
$val = $val << $bits;
$val = $val <<$bits;
$val = $val<<$bits;
These are all &quot;here doc&quot; context:
Code:
print <<EOF;
...
EOF
print<<EOF;
...
EOF
print <<1;
...
1
print<<1;
...
1
Perl is very flexible about spacing, and really good about doing what you want based on context.

By the way, here's another handy use of the &quot;here doc&quot; context:
Code:
$text = <<EOF;
All of this text
Will be put in the variable
Including the line end characters
At the end of each line!
EOF
It's a handy way of getting multiple lines of text into a single variable. Tracy Dryden
tracy@bydisn.com

Meddle not in the affairs of dragons,
For you are crunchy, and good with mustard. [dragon]
 
Or what i really like about it.... it can be the ultimate batch file:

<<`EOB`; # back ticks
date >> tmp.txt
w -l >> tmp.txt
ps ax >> tmp.txt
EOB

or of course you can save it to a variable or an array for processing.

Then you could add that baby to cron and your all set! [smile]

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top