DEV Community

loading...

Fun with dot

Tib
403 Forbidden
Updated on ・5 min read

I don't know how to introduce this post 😃 I will just play with you using various usage of . in Perl but not only (see second part).

Table Of Contents

  1. Fun with dot in Perl
  2. Fun with dot in everything but Perl

Fun with dot in Perl

Concat strings with .

In Perl like in a lot of other languages, you can concat strings with .

print "Fun" . " with " . "." . "\n";
Enter fullscreen mode Exit fullscreen mode

But you can also concat without quotes which is less usual:

print Dota.All.Star;
Enter fullscreen mode Exit fullscreen mode

It's less flexible and if you want my opinion I don't recommend to use this form.

Match a lot with .

. normally matches almost any character (including a dot itself).
You can even extend what is matched by . using /s modifier.

while(<>) {
    $_ =~ s/(...)./$1/g;
    print $_;
}
Enter fullscreen mode Exit fullscreen mode

That will match 4 characters and keep only 3 and repeat:

$ echo "foo bar baz" | perl keep3.pl
foobarbaz
Enter fullscreen mode Exit fullscreen mode

You can make your dot match more ("greedy dot"):

while(<>) {
    $_ =~ s/(.*) foo/$1/g;
    print $_;
}
Enter fullscreen mode Exit fullscreen mode

That will match as much as possible:

$ echo "foo foo foo" | perl greedy.pl
foo foo
Enter fullscreen mode Exit fullscreen mode

But you can also make the contrary ("non greedy dot"):

while(<>) {
    $_ =~ s/(.*?) foo/$1/g;
    print $_;
}
Enter fullscreen mode Exit fullscreen mode

That will match the minimum possible valid match:

$ echo "foo foo foo" | perl nongreedy.pl 
foo
Enter fullscreen mode Exit fullscreen mode

v-strings

Another use of dot can be the version strings (or "v-strings"):

$ perl -e 'print v74.65.80.72'
JAPH
Enter fullscreen mode Exit fullscreen mode

Or even mixing vstring with "non quote" trick:

$ perl -e 'print etc.v46.46.46'
etc...
Enter fullscreen mode Exit fullscreen mode

If you read "version" in version strings and you are interested in creating and comparing versions in Perl please read this great blog post about versions.

Terminates format

The format is an old and powerful (but less and less commonly used) feature of Perl. It is used to do formatting as the name suggests 😃

format STDOUT =
@<<  @||||||   @>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>
"Fun",   "with",   "dot !"
.
write;
Enter fullscreen mode Exit fullscreen mode

The output is:

$ perl format.pl 
Fun   with                                  dot !
Enter fullscreen mode Exit fullscreen mode

The range operator

A great Perl feature is the range operator .. or ...:

my @r = aa...bb;
print join " ", @r;
Enter fullscreen mode Exit fullscreen mode

(Here ... is equivalent to ..)

That produces:

aa ab ac ad ae af ag ah ai aj ak al am an ao ap aq ar as at au av aw ax ay az ba bb
Enter fullscreen mode Exit fullscreen mode

I wanted to find a python equivalent to show, but it seems not straighforward at all (not an operator or even a one line thing). Seems like range is for integers only, and something naive like:

import itertools

for it in itertools.product('abcdefghijklmnopqrstuvwxyz', repeat=2):
    print it
Enter fullscreen mode Exit fullscreen mode

Is not producing the right thing, because I don't want the first character to vary outside "a" and "b"...

If you have a simple equivalent, feel free to comment!

Now, a much more strange range:

#!/usr/bin/env perl
use strict;
use warnings;

while(<>) {
    if(/>>>/../<<</) {
        if(!/^>>>$/ and !/^<<<$/) {
            chomp; print;
        }
    }
}
Enter fullscreen mode Exit fullscreen mode

To apply on this file:

Ignore me
>>>
Kawa
<<<
Ignore
me
>>>
bunga
<<<

>>>
!
<<<
Enter fullscreen mode Exit fullscreen mode

Execute with:

$ cat file.txt | perl sections.pl 
Kawabunga!
Enter fullscreen mode Exit fullscreen mode

The ellipsis

The ellipsis or "yada-yada" or "triple-dot" is a way to mark a place as unimplemented.

$ perl -e '... if 1;'
Unimplemented at -e line 1.
Enter fullscreen mode Exit fullscreen mode

It is resolved at runtime so will only throw the Unimplemented if the code actually reach the ellipsis.

Unsafe dot

Starting Perl 5.26.0, the current directory (.) was removed from default INC. You can still add them with -I. or use lib "." or compile the interpreter with PERL_USE_UNSAFE_INC=1

Fun with dot in all but Perl

Calling methods

The dot is used to call methods in several languages (e.g. python or ruby).

Un point c'est tout

For a very long time, when you asked "how to concat 2 string in PHP?" on google.fr, the first answer was a forum where a guy answered tersely "un point c'est tout" (meaning "a dot and that's all!" or "this is like this!").

<?php 
$a = "hello" . "world";
print $a;
?>
Enter fullscreen mode Exit fullscreen mode

Since it is also a french expression, it has a double sense and was not well understood by newbies despite being completely correct 😃

The dotfiles

Dotfiles are user config in files starting with dot (e.g. .vimrc). People sometimes put them and share them in git.

Nasty dot

On GNU/Linux, if you want to be a hacker script kiddie like Kim DotCom you can "hide" files by naming them . or ....

touch ". " ...
Enter fullscreen mode Exit fullscreen mode

As they start with dot, an usual ls won't print them.

Of course if you really want to hide a file, you need a more robust method.

Safe Navigation operator

For instance in groovy .?

See Safe Navigation operator

Dota

Completely off topic but it starts with "dot" and I played it when I was younger 😀
Dota

What is the sense of this post after all? I don't know...

Git

Git also propose .. and ... for commit ranges. e.g.
git diff abc..def or git diff abc...def the secong is more strict (introduce ordering).

There is the kind of thing (with different meaning) for git log.

C structs

Access C struct elements with .:

/* Declare the struct with integer members x, y */
struct point {
   int    x;
   int    y;
};
/* Define a variable p of type point, and set members using designated initializers*/
struct point p;
p.x = 0;
p.y = 0;
Enter fullscreen mode Exit fullscreen mode

Or the less common set with designated variables:

/* Declare the struct with integer members x, y */
struct point {
   int    x;
   int    y;
};
/* Define a variable p of type point, and set members using designated initializers*/
struct point p = { .y = 2, .x = 1 };
Enter fullscreen mode Exit fullscreen mode

The final dot in url

Do you know that example.com can also be written example.com. ?

The same way we can access my "Fun with perl shebang" article using perl.com.

But I read that it is not equivalent.

I don't know much about this, if you have details, please comment!

The more expensive dot of history?

One of my teacher told me one day about the rocket explosion caused by a bug where a dot was used instead a comma.

He was referencing the FORTRAN punctuation error occured on Mariner 1 in 1962. It's seems to be an urban legend, but still interesting to mention right?

The printf of Brainfuck

Do you know Brainfuck? If you don't, you should maybe not try to 😀 for you mental sanity

The dot is the printf in Brainfuck (putchar(*ptr);​)

Execute and source

Execution with ./a.out and source with . config.sh (or source ./config.sh)

Conclusion

This post was not published on slashdot but on dev.to that is a blog platform not running dotclear.

The majority of samples are written in Perl 5.32, don't expect them to run in dotnet.

Sorry for this conclusion... 🙊 😵

Do you have more . usages in Perl or other fields? Or more IT related puns with the word "dot"? 😀

Discussion (0)

Forem Open with the Forem app