[WpProQuiz 1]
[WpProQuiz_toplist 1]
Learning through sharing
[WpProQuiz 1]
[WpProQuiz_toplist 1]
It happens many times that you declare a variable name something and calling that variable name by something else. In case of hash in perl, calling that variable or assigning that variable will not give error. It will silently create that key value pair. This is called autovivification.
For ex:
my $name = "Jassi";
$self->{name} = "Sanjeev aka Jassi" # it will work as it is
$self->{Name} = "Jaiswal"; # It will silently create another key value pair.
# Now if you try to access $self->{name}, it will not give you
#Jaiswal. You will get "Sanjeev aka Jassi".
At above example you can see that our motto was to change the value of key “name” but by mistake we wrote “Name”, then also it got updated without any error. So being a human being, we do such typo mistakes and It will be very tough for us to find such bugs.
Before writing a program, lets keep in mind that what variables we are going to use and strict the program to use those variable name only. If I use any other variable than provided one, it should throw error. For this purpose we use “field” pragma.
#!c:/strawberry/perl/bin/perl.exe
{
package Student;
# You may like to give a list of attributes that are only allowed, and have Perl should exit with an error,
#if someone uses an unknown attribute.
# You can do this using the fields pragma i.e use fields fieldname1, fieldname2 etc"
# Isn't it nice feature to secure our code.
use fields 'name',
'registration',
'grades';
sub new {
my($class, $name) = @_;
$self = fields::new($class); # returns an empty "strict" object
$self->{name} = $name; # attributes get accessed as usual
return $self; # $self is already blessed
}
}
my $classname = Student->new('Jassi');
$classname->{name} = 'Jassi D\'Costa'; # works
$classname->{Name} = 'foo'; # blows up. If I will not use field pragma, It will silently create this key value pair
# for $classname object.
# End of the program
Here is the screen-shot of the error that you will get.
Our team always face email existence problem when they try to:
Well it’s not possible to know 100% whether given e-mail address exists in real or not but it will be handy in majority of the cases. You can do it manually also if you are in Linux box using host or nslookup and telnet command. I would prefer host command for this purpose.
How it works:
Here many things came into the picture while testing various mail ids from various mail handlers. I will discuss it under “possible ways of failures”
Steps to check the email existence manually
1. Get the mail id to verify (we can get domain-name from this format to use it in our next step userid@domain-name . For ex:admin@aliencoders.org)
2. Either use nslookup command or host command to get mail server address (Lists of mx records from DNS)
a. Using nslookup:
nslookup –type=mx domain-name
b. Using host :
host –t mx domain-name
3. Get the least digital value’s mx record to use it in telnet
a. telnet mxrecord port-no i.e. telnet mail.aliencoders.org 25
b. Once connected use SMTP commands to know if email id exists or not. It will return 250 as a return code with OK or Accepted. Depending upon mail server configuration and Linux flavors too.
Let’s see it practically with all combination of commands, inputs and outputs 😀
Output from nslookup (it will show many more things which I have not shown here)
$ nslookup -type=mx gmail.com
Non-authoritative answer:
gmail.com mail exchanger = 20 alt2.gmail-smtp-in.l.google.com.
gmail.com mail exchanger = 30 alt3.gmail-smtp-in.l.google.com.
gmail.com mail exchanger = 40 alt4.gmail-smtp-in.l.google.com.
gmail.com mail exchanger = 5 gmail-smtp-in-v4v6.l.google.com.
gmail.com mail exchanger = 10 alt1.gmail-smtp-in.l.google.com.
Output from host command ( I prefer this over nslookup )
$ host -t mx gmail.com
gmail.com mail is handled by 30 alt3.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 40 alt4.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 5 gmail-smtp-in-v4v6.l.google.com.
gmail.com mail is handled by 10 alt1.gmail-smtp-in.l.google.com.
gmail.com mail is handled by 20 alt2.gmail-smtp-in.l.google.com.
Get the value which has lowest ranked digit. In this case it is 5 so take gmail-smtp-in-v4v6.l.google.com for our purpose
Either use host or nslookup for the first stage.
Telnet outputs:
Step 1
telnet gmail-smtp-in-v4v6.l.google.com 25
Trying 173.194.77.27…
Connected to gmail-smtp-in-v4v6.l.google.com.
Escape character is '^]'.
220 mx.google.com ESMTP hk4si16050652obc.168
Step 2 (type helo, if it didn’t work try helo hi, if it didn’t work too try ehlo then )
helo
250 mx.google.com at your service
Step 3 (type any valid email id. Invalid email id will say ok but after rcpt to it will throw an error and don’t skip this step)
mail from: <sanjeev@aliencoders.org>
250 2.1.0 OK hk4si16050652obc.168
Step 4 (You need to provide email which needs to be verified. Ok means it may exists. Not 1005 guaranteed on existence but we can infer that this email id may work)
rcpt to: <jassics@gmail.com>
250 2.1.5 OK hk4si16050652obc.168
Telnet outputs on failure (Various reasons are there)
1. Network is unreachable. (sometimes it is reachable form one host but not from others)
telnet gmail-smtp-in-v4v6.l.google.com 25
Trying 74.125.127.26…
telnet: connect to address 74.125.127.26: Connection timed out
Trying 2001:4860:8005::1b…
telnet: connect to address 2001:4860:8005::1b: Network is unreachable
telnet: Unable to connect to remote host: Network is unreachable
2. Error at mail from:<mail-id> :
3. If email id exists but it doesn’t show up 250/ok then below reasons may justify this:
Steps to check email existence using Perl
Good thing with this script is:
Here is the fully working Perl script
Why still this trick doesn’t assure 100% proof that email id exists or not?
There can be many reasons for not trusting on this script or the above said manual steps. Best way is to send an email to the given email id. If it bounced back, means it doesn’t exist or can’t be delivered. (I chat with a person in gtalk but can’t send an email to that person. Strange isn’t it).
Note: This was just for educational purpose and found helpful for web masters or admins. Don’t use it as spam stuffs, you IP address and your mail id will be blacklisted. Like admin@aliencoders.org has been blacklisted from various mail servers.
What is chomp and chop?
chomp()
The chomp() function will remove (usually) any new line character from the end of a string. The reason we say usually is that it actually removes any character that matches the current value of $/ (the input record separator), and $/ defaults to a new line. It returns the total number of characters removed from all its arguments and If VARIABLE is omitted, it chomps $_
So, if $/ =”\t” then chomp would remove every tab instead of new line.
For more information on the $/ variable, try perldoc perlvar and see the entry for $/ and perldoc -f chomp
Note: if chomp doesn’t behave as it should by default, hint is to check record separator ($/)
chop()
Sometimes you will find you want to unconditionally remove the last character from a string. While you can easily do this with regular expressions, chop is more efficient. Check perldoc -f chop
The chop() function will remove the last character of a string (or group of strings) regardless of what that character is and return the last character chopped. In case of array it return the last character chopped of the last element of the array. Note, if you want to easily remove newlines or line separators see the chomp().
As chop return the last character chopped of the string , if you want it's reverse i.e. return all characters except the last from the string you could use substr($string, 0, -1)
Chomp and chop, both functions can be applied on strings,array,hash. We would see it by three examples for each functions.
Example 1. Chomping a string
Most often you will use chomp() when reading data from a file or from a user. When reading user input from the standard input stream (STDIN) for instance, you get a newline character with each line of data. chomp() is really useful in this case because you do not need to write a regular expression and you do not need to worry about it removing needed characters.
When running the example below, using the enter key on a line by itself will exit the program.
[perl]#!/usr/bin/perl
use strict;
use warnings;
my $username = <STDIN>;
print "before chomp: $username";
chomp $username; #or chomp($username); or chomp (my $username =<STDIN>); in one line
print "After chomp: $username";[/perl]
Output:
[vim][Sanjeev@Alien Coders]$ perl chomp_examples.pl
Alien Coders
before chomp: Alien Coders
After chomp: Alien Coders[Sanjeev@Alien Coders]$[/vim]
First print got printed with new line which you entered while typing. Yes, it took new line (pressed enter) also as user input. Second line removed that new line, so second print is displayed along with shell on the same line.
Example 2. Chomping an array
If you chomp an array, it will remove newline from the end of every element in the array:
[perl] #!/usr/bin/perl
use strict;
use warnings;
my @array = ("sanjeev\n", "Jassi", "AlienCoders\n");
print "Before chomp:\n";
print "@array\n";
chomp(@array);
print "After chomp:\n";
print "@array\n";[/perl]
Output:
[vim]Before chomp:
sanjeev
Jassi AlienCoders
After chomp:
sanjeev Jassi AlienCoders[/vim]
As you can see, the newlines have been removed from "sanjeev" and "AlienCoders", but no characters have been removed from "jassi".
Example 3. Chomping a hash
If you pass a hash into chomp() function, it will remove newlines from every value (not key) of the hash. Remember key is always unique and string (internally). So, if you add new line in key then use that new line char also while using that key. Better to avoid such nasty thing:
[perl] #!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %hash = (
'first' => "one\n",
'second' => "two\n",
'third' => "three\n",
);
print "before chomp:\n";
print Dumper(\%hash);
chomp(%hash);
print "after chomp:\n";
print Dumper(\%hash);[/perl]
Output:
[vim]before chomp:
$VAR1 = {
'first' => 'one
',
'second' => 'two
',
'third' => 'three
'
};
after chomp:
$VAR1 = {
'first' => 'one',
'second' => 'two',
'third' => 'three'
};[/vim]
It clearly shows that how new line is effective before chomp and it looks nice after chomp.
Example 4. Chopping a string
The chop() function removes and returns the last character from the given string whatever it is. So don’t do any mistake by assuming that it removed new line from the user input with its first use. When you will use it second time it will again remove one more character from user input but chomp only and only removes new line (or whatever is stored in $/).
[perl]#!/usr/bin/perl
use strict;
use warnings;
my $string = 'Perl';
my $char = chop($string); #to return the chopped character in a variable
print "String: $string\n";
print "Char: $char\n";[/perl]
Output:
[vim]String: Per
Char: l[/vim]
Example 5. Chopping an array
If you pass the chop() function to an array, it will remove the last character from every element in the array.
[perl]#!/usr/bin/perl
use strict;
use warnings;
my @arr = ('Jassi', 'Sanjeev', 'Alien Coders');
my $last_char = chop(@arr);
print "@arr\n";
print "Last Char: $last_char\n"; #it will store last character of last element in the array[/perl]
Output:
[vim]Jass Sanjee Alien Coder
Last Char: s[/vim]
Example 6. Chopping a hash
If you pass a hash into chop() function , it will remove the last character from the values (not the keys) in the hash. For example:
[perl] #!/usr/bin/perl
use strict;
use warnings;
use Data::Dumper;
my %hash = (
first => 'one',
second => 'two',
third => 'three',
);
print “Before chop:\n”;
print Dumper \%hash;
my $chr = chop(%hash);
print “After chop: \n”;
print Dumper(\%hash);
print "Char: $chr\n"; #it always have last character of last value in hash[/perl]
Output:
[vim]Before chop:
$VAR1 = {
'first' => 'one',
'second' => 'two',
'third' => 'three'
};
After chop:
$VAR1 = {
'first' => 'on',
'second' => 'tw',
'third' => 'thre'
};
Char: e[/vim]
Note:
Credit: http://perlmeme.org
Subscribe now to keep reading and get access to the full archive.