Perl find all pow 2 numbers

This little script finds all pow 2 numbers in Perl :

use strict;
my $number = 0;

for(1..32){
    ++$number;
    print "$number - ".isPow2($number)."\n";
}


sub isPow2 {
    return(0) if($_[0] <= 0);
    return(1) if($_[0] == 1);
    for(1..$_[0]){
       return(1) if($_[0] == 2**$_)
    }
    return(0);
}

Our Result:

1 - 1  
2 - 1  
3 - 0  
4 - 1  
5 - 0  
6 - 0  
7 - 0  
8 - 1  
9 - 0  
10 - 0 
11 - 0 
12 - 0 
13 - 0 
14 - 0 
15 - 0 
16 - 1

Perl print String difference

This function prints the difference between two strings with Perl:

sub printStringDiff{
my ($s1, $s2) = @_;

my @s1 = split(//, $s1);
my @s2 = split(//, $s2);
while (@s1) {

    if (defined $s1[0] and defined $s2[0]) {
    	if($s1[0] eq $s2[0]){
			print shift @s1;
		}else{
			print color("red"),shift @s1, color("reset");
		}
    }elsif(defined $s1[0]){
    	print color("red"),shift @s1, color("reset");
    }
    shift @s2;
}
print "\n";

@s1 = split(//, $s1);
@s2 = split(//, $s2);
while (@s2) {

    if (defined $s2[0] and defined $s1[0]) {
		if($s2[0] eq $s1[0]){
			print shift @s2;
		}else{
			print color("red"),shift @s2, color("reset");
		}
    }elsif(defined $s2[0]){
    	print color("red"),shift @s2, color("reset");
    }
    shift @s1;
}
print "\n";
}

You need the Module Term::ANSIColor, to highlight the changes,use it like this:

#!/usr/bin/perl
use strict;
use Term::ANSIColor;

printStringDiff("1234","123456");
print "\n";

printStringDiff("123456","1234");
print "\n";

printStringDiff("ABAA","AABA");
print "\n";

And the Result is :

ColorStringDiff

Perl check if file handler is open

If you write a function in Perl and want to check if the file handler is already open you could use this function:

sub filehandlerOpen {
	my $fh = shift;
	no warnings 'uninitialized';
	return 0 if(!defined $fh || $fh !~ /^GLOB\(0x.+?\)$/);
	if(fileno($fh) >= 1){
		return(1);
	}
	return(0);
}

It returns 0 if its undefined,closed or not open, an 1 if the handler is open.

 

In this little example I open a file to read and another to write, I check the sub before and after the open function, and before and after the close function.

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

my ($fh_read,$fh_write);

print "Check undefined:\n";
print filehandlerOpen($fh_read).$/;
print filehandlerOpen($fh_write).$/;


open($fh_write, ">", './testfile') or die "Failed to open file: $!\n";
open($fh_read, "<", './testfile') or die "Failed to open file: $!\n";


print "Check defined:\n";
print filehandlerOpen($fh_read).$/;
print filehandlerOpen($fh_write).$/;


for my $num (1..6){
	print $fh_write "$num\n";
}
print "Check close write:\n";
print filehandlerOpen($fh_write).$/;
close($fh_write);
print filehandlerOpen($fh_write).$/;


while (my $row = <$fh_read>) {
  chomp $row;
}
print "Check close read:\n";
print filehandlerOpen($fh_read).$/;
close($fh_read);
print filehandlerOpen($fh_read).$/;


sub filehandlerOpen {
	my $fh = shift;
	no warnings 'uninitialized';
	return 0 if(!defined $fh || $fh !~ /^GLOB\(0x.+?\)$/);
	if(fileno($fh) >= 1){
		return(1);
	}
	return(0);
}

The Result, looks like expected:

Check undefined:
0
0
Check defined:
1
1
Check close write:
1
0
Check close read:
1
0

Perl print __DATA__ multiple times

Sometimes you have a lot of Data in Perl and you want to store in your script, for that you could you the __DATA__ token, you could simple read it with a file handle:

print <DATA>,"\n";

__DATA__
Test String
123456

If you use it more than one time you should seek for the start position:

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



my $data_start = tell DATA; # where __DATA__ begins
print <DATA>,"\n";
seek DATA, $data_start, 0;

print <DATA>,"\n";

__DATA__
Test String
123456

Output looks like this:

Test String
123456

Test String
123456

Perl read/write File

This is a simple example how to write into a File with Perl, and then read from it.At first we write the numbers from 1 to 6 into the file, in the second we read from the file and print it on the screen, if you don’t want the “\n” at the end use the function chomp.

If you open a File for read use ‘<‘, for write ‘>’ and for append ‘>>’.

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

#declare filehandler
my ($fh_read,$fh_write);


#open file for write
open($fh_write, ">", './testfile') or die "Failed to open file: $!\n";

#now write
for my $num (1..6){
	print $fh_write "$num\n";
}

#close the file
close($fh_write);


#open file for read
open($fh_read, "<", './testfile') or die "Failed to open file: $!\n";

#now read
while (my $row = <$fh_read>) {
  print $row;
}

#close the file
close($fh_read);

Result looks like:

1
2
3
4
5
6

you could check if a handler is open with this Perl handler function.