Found 157 Articles for PERL

What are Object and Class in Perl?

Mohd Mohtashim
Updated on 29-Nov-2019 12:18:19

104 Views

There are three main terms, explained from the point of view of how Perl handles objects. The terms are object, class, and method.An object within Perl is merely a reference to a data type that knows what class it belongs to. The object is stored as a reference in a scalar variable. Because a scalar only contains a reference to the object, the same scalar can hold different objects in different classes.A class within Perl is a package that contains the corresponding methods required to create and manipulate objects.A method within Perl is a subroutine, defined with the package. The first argument to ... Read More

Sending an Attachment with email using Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:15:51

2K+ Views

If you want to send an attachment in your email using Perl, then following script serves the purpose −#!/usr/bin/perl use MIME::Lite; $to = 'abcd@gmail.com'; $cc = 'efgh@mail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = 'This is test email sent by Perl Script'; $msg = MIME::Lite-=>new(    From => $from,    To => $to,    Cc => $cc,    Subject => $subject,    Type => 'multipart/mixed' ); # Add your text message. $msg->attach(    Type => 'text',    Data => $message ); # Specify your file as attachement. $msg->attach(Type => 'image/gif',    Path => '/tmp/logo.gif',    Filename => 'logo.gif', ... Read More

Sending an HTML Message using Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:14:02

474 Views

If you want to send HTML formatted email using sendmail, then you simply need to add Content-type: text/html in the header part of the email as follows −#!/usr/bin/perl $to = 'abcd@gmail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = 'This is test email sent by Perl Script'; open(MAIL, "|/usr/sbin/sendmail -t"); # Email Header print MAIL "To: $to"; print MAIL "From: $from"; print MAIL "Subject: $subject"; print MAIL "Content-type: text/html"; # Email Body print MAIL $message; close(MAIL); print "Email Sent Successfully";

Sending a Plain Message using Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:12:41

199 Views

If you are working on Linux/Unix machine then you can simply use sendmail utility inside your Perl program to send email. Here is a sample script that can send an email to a given email ID. Just make sure the given path for sendmail utility is correct. This may be different for your Linux/Unix machine.#!/usr/bin/perl $to = 'abcd@gmail.com'; $from = 'webmaster@yourdomain.com'; $subject = 'Test Email'; $message = 'This is test email sent by Perl Script'; open(MAIL, "|/usr/sbin/sendmail -t"); # Email Header print MAIL "To: $to"; print MAIL "From: $from"; print MAIL "Subject: $subject"; # Email Body print MAIL $message; close(MAIL); ... Read More

The G Assertion in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:10:09

218 Views

The \G assertion in Perl allows you to continue searching from the point where the last match occurred. For example, in the following code, we have used \G so that we can search to the correct position and then extract some information, without having to create a more complex, single regular expression −Example Live Demo#!/usr/bin/perl $string = "The time is: 12:31:02 on 4/12/00"; $string =~ /:\s+/g; ($time) = ($string =~ /\G(\d+:\d+:\d+)/); $string =~ /.+\s+/g; ($date) = ($string =~ m{\G(\d+/\d+/\d+)}); print "Time: $time, Date: $date";When the above program is executed, it produces the following result −Time: 12:31:02, Date: 4/12/00The \G assertion is ... Read More

Grouping Matching in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:07:15

1K+ Views

From a regular-expression point of view in Perl, there is no difference between the following two expressions except that the former is slightly clearer.$string =~ /(\S+)\s+(\S+)/; and $string =~ /\S+\s+\S+/;However, the benefit of grouping is that it allows us to extract a sequence from a regular expression. Groupings are returned as a list in the order in which they appear in the original. For example, in the following fragment we have pulled out the hours, minutes, and seconds from a string.my ($hours, $minutes, $seconds) = ($time =~ m/(\d+):(\d+):(\d+)/);As well as this direct method, matched groups are also available within the ... Read More

Matching Boundaries & Selecting Alternatives in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:04:17

196 Views

Matching Boundaries in PerlThe \b matches at any word boundary in Perl, as defined by the difference between the \w class and the \W class. Because \w includes the characters for a word, and \W the opposite, this normally means the termination of a word. The \B assertion matches any position that is not a word boundary. For example −/\bcat\b/ # Matches 'the cat sat' but not 'cat on the mat' /\Bcat\B/ # Matches 'verification' but not 'the cat on the mat' /\bcat\B/ # Matches 'catatonic' but not 'polecat' /\Bcat\b/ # Matches 'polecat' but not 'catatonic'Selecting Alternatives in PerlThe | character is just like ... Read More

The Translation Operator in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:03:02

496 Views

Translation is similar, but not identical, to the principles of substitution in Perl, but unlike substitution, translation (or transliteration) does not use regular expressions for its search on replacement values. The translation operators are −tr/SEARCHLIST/REPLACEMENTLIST/cds y/SEARCHLIST/REPLACEMENTLIST/cdsThe translation replaces all occurrences of the characters in SEARCHLIST with the corresponding characters in REPLACEMENTLIST. For example, using the "The cat sat on the mat." string we have been using in this chapter −Example Live Demo#/user/bin/perl $string = 'The cat sat on the mat'; $string =~ tr/a/o/; print "$string";When above program is executed, it produces the following result −The cot sot on the mot.Standard Perl ... Read More

The Substitution Operator in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 12:00:43

2K+ Views

The substitution operator s/// in Perl is really just an extension of the match operator that allows you to replace the text matched with some new text. The basic form of the operator is −s/PATTERN/REPLACEMENT/;The PATTERN is the regular expression for the text that we are looking for. The REPLACEMENT is a specification for the text or regular expression that we want to use to replace the found text with. For example, we can replace all occurrences of dog with cat using the following regular expression −Example Live Demo#/user/bin/perl $string = "The cat sat on the mat"; $string =~ s/cat/dog/; print "$string";When the above program ... Read More

Matching Only Once in Perl

Mohd Mohtashim
Updated on 29-Nov-2019 11:58:55

185 Views

There is a simpler version of the match operator in Perl - the ?PATTERN? operator. This is basically identical to the m// operator except that it only matches once within the string you are searching between each call to reset.For example, you can use this to get the first and last elements within a list −Example Live Demo#!/usr/bin/perl @list = qw/food foosball subeo footnote terfoot canic footbrdige/; foreach (@list) {    $first = $1 if /(foo.*?)/;    $last = $1 if /(foo.*)/; } print "First: $first, Last: $last";When the above program is executed, it produces the following result −First: foo, Last: footbrdige

Previous 1 ... 3 4 5 6 7 ... 16 Next
Advertisements