Found 448 Articles for Programming Scripts

How to create Database Connection in Perl?

Mohd Mohtashim
Updated on 02-Dec-2019 07:45:13

318 Views

Assuming we are going to work with MySQL database with Perl. Before connecting to a database make sure of the followings. You can take help of our MySQL tutorial in case you are not aware about how to create database and tables in MySQL database.You have created a database with a name TESTDB.You have created a table with a name TEST_TABLE in TESTDB.This table is having fields FIRST_NAME, LAST_NAME, AGE, SEX and INCOME.User ID "testuser" and password "test123" are set to access TESTDB.Perl Module DBI is installed properly on your machine.You have gone through MySQL tutorial to understand MySQL Basics.Following ... Read More

Destructors and Garbage Collection in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:43:59

261 Views

If you have programmed using object oriented programming before, then you will be aware of the need to create a destructor to free the memory allocated to the object when you have finished using it. Perl does this automatically for you as soon as the object goes out of scope.In case you want to implement your destructor, which should take care of closing files or doing some extra processing then you need to define a special method called DESTROY. This method will be called on the object just before Perl frees the memory allocated to it. In all other respects, the ... Read More

Default Autoloading in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:42:17

97 Views

Perl offers a feature which you would not find in any other programming languages: a default subroutine. Which means, if you define a function called AUTOLOAD(),  then any calls to undefined subroutines will call AUTOLOAD() function automatically. The name of the missing subroutine is accessible within this subroutine as $AUTOLOAD.Default autoloading functionality is very useful for error handling. Here is an example to implement AUTOLOAD, you can implement this function in your own way.sub AUTOLOAD {    my $self = shift;    my $type = ref ($self) || croak "$self is not an object";    my $field = $AUTOLOAD;    $field ... Read More

Method Overriding in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:37:42

298 Views

You can add your additional functions in child class or you can add or modify the functionality of an existing methods in its parent class. It can be done as follows −#!/usr/bin/perl package Employee; use Person; use strict; our @ISA = qw(Person);    # inherits from Person # Override constructor sub new {    my ($class) = @_;    # Call the constructor of the parent class, Person.    my $self = $class->SUPER::new( $_[1], $_[2], $_[3] );    # Add few more attributes    $self->{_id} = undef;    $self->{_title} = undef;    bless $self, $class;    return $self; } # ... Read More

Understanding Inheritance in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:35:16

786 Views

Object-oriented programming has very good and useful concept called inheritance. Inheritance simply means that properties and methods of a parent class will be available to the child classes. So you don't have to write the same code again and again, you can just inherit a parent class.For example, we can have a class Employee, which inherits from Person. This is referred to as an "isa" relationship because an employee is a person. Perl has a special variable, @ISA, to help with this. @ISA governs (method) inheritance.Following are the important points to be considered while using inheritance −Perl searches the class ... Read More

Defining Class Methods in Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:32:13

316 Views

Other object-oriented languages have the concept of security of data to prevent a programmer from changing an object data directly and they provide accessor methods to modify object data. Perl does not have private variables but we can still use the concept of helper methods to manipulate object data.Lets define a helper method to get person’s first name −sub getFirstName {    return $self->{_firstName}; }Another helper function to set person’s first name −sub setFirstName {    my ( $self, $firstName ) = @_;    $self->{_firstName} = $firstName if defined($firstName);    return $self->{_firstName}; }Now lets have a look into complete example: ... Read More

Creating and Using Objects using Perl

Mohd Mohtashim
Updated on 02-Dec-2019 07:29:20

132 Views

To create an instance of a class (an object) we need an object constructor in Perl. This constructor in Perl is a method defined within the package. Most programmers choose to name this object constructor method new, but in Perl you can use any name.You can use any kind of Perl variable as an object in Perl. Most Perl programmers choose either references to arrays or hashes.Let's create our constructor for our Person class using a Perl hash reference. When creating an object, you need to supply a constructor, which is a subroutine within a package that returns an object ... Read More

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

477 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";

Previous 1 ... 5 6 7 8 9 ... 45 Next
Advertisements