Sponsored Content
Full Discussion: Inheritance in Perl
Top Forums Shell Programming and Scripting Inheritance in Perl Post 302625007 by Skrynesaver on Tuesday 17th of April 2012 07:11:48 AM
Old 04-17-2012
You need to return a blessed reference from your new() method, see below for a very ugly adaptation of your class, then have a read through perldoc perlboot
Code:
#! /usr/bin/perl

use strict;
use warnings;

package Inventory_item;

sub new {
        my ($proto,%args) = @_;           # Make inheritable by checking what we were called as
        my $class = ref($proto) || $proto;# otherwise we cannot extend $class
        my $self = {};
        bless ($self, $class); # return a blessed reference
}

sub declare
{
    my ($self) = shift;
    my $arg= shift;
    $self->{'abc'}=$arg;
    print "$self->{'abc'} is set to $self->{'abc'}\n";

}

sub retrieve
{
    my ($self)=shift;
    my ($param)=shift;
    if (! defined $self->{$param}){
        die ("No such field $param\n");
    }
    else{
        print "$param is set to $self->{$param}\n";

    }
}
package main;

my  $item = Inventory_item->new();
$item->declare(3);
$item->retrieve('abc');

 

9 More Discussions You Might Find Interesting

1. Shell Programming and Scripting

perl - variable inheritance

Hey Everyone, Does anyone know how - or if it's even possible - for a child perl script to inherit the variables of a parent perl script? In a shell script, you would use "export" for example. I am running Perl 5.8. Basically, let's say "perl1.pl" calls "perl2.pl" and I want "perl2.pl" to... (2 Replies)
Discussion started by: gsatch
2 Replies

2. Shell Programming and Scripting

Makefile: Parent - Child Inheritance and export

Hi, I have a number of Makefiles, including a couple of files that I include in Makefiles, a few scripts that are executed through Makefiles, and I have problems with environment variables that are not inherited to the scripts properly. Simplified scenario: rootdir/Makefile: all: ... (1 Reply)
Discussion started by: Shompis
1 Replies

3. AIX

WLM inheritance

Is none of you using WLM? A search gives no matches.... Anyway, I have set up a class with inheritance = yes. In the rules characteristics I have chosen a shell script as an application. This script is caught by the class, but not the child processes, which have the PID of the script as the... (6 Replies)
Discussion started by: firefox111
6 Replies

4. Programming

C++ Inheritance problem??????

Hi friends, I hope u people are ok and doing fine. I have this small problem with the derived class. I have created te base class and there is a small problem with the definition of the derived class which the compiler is pointing out, could you please help me. Here is my code #ifndef... (2 Replies)
Discussion started by: gabam
2 Replies

5. Programming

Inheritance

whats the use of inheriting with access specifier as private..? Please specify the answer with a simple example... Regards -- Madhu (5 Replies)
Discussion started by: MadhuM
5 Replies

6. Programming

Difference in multiple inheritance and multilevel inheritance: same method name ambiguity problem

Hi, In multi-level inheritance: class A { public: void fun() { cout << "A" << endl; } }; class B : public A { public: void fun() { cout << "A" << endl; } }; class C : public B { }; int main() { C c; c.fun(); // Ans: A } (1 Reply)
Discussion started by: royalibrahim
1 Replies

7. Red Hat

Ulimit Inheritance

Hi , If i start mysqld or httpd as root user which inturn starts them as "mysql" or "apache" user, will the ulimit of "root" user or ulimit of "mysql/apache" user be set for the mysql/apache processes. My understanding is that the ulimit of the user who initiates the process(root in this... (2 Replies)
Discussion started by: Hari_Ganesh
2 Replies

8. Programming

Size of derived class, in case of multiple inheritance

Why, here the size of class 'Derived' is 8 ? class Base1 { public: virtual void f() { } }; class Base2 { public: virtual void f() { } }; class Derived : public Base1, Base2 { public: virtual void f() { } }; (1 Reply)
Discussion started by: techmonk
1 Replies

9. Programming

What is wrong with below python inheritance code?

I am using python 3.4. Below is the exception I am getting- Traceback (most recent call last): File "./oop.py", line 20, in <module> y = DerivedClass("Manu") File "./oop.py", line 15, in __init__ super().__init__(self,value) TypeError: __init__() takes 2 positional arguments but... (2 Replies)
Discussion started by: Tanu
2 Replies
Net::protoent(3perl)					 Perl Programmers Reference Guide				      Net::protoent(3perl)

NAME
Net::protoent - by-name interface to Perl's built-in getproto*() functions SYNOPSIS
use Net::protoent; $p = getprotobyname(shift || 'tcp') || die "no proto"; printf "proto for %s is %d, aliases are %s ", $p->name, $p->proto, "@{$p->aliases}"; use Net::protoent qw(:FIELDS); getprotobyname(shift || 'tcp') || die "no proto"; print "proto for $p_name is $p_proto, aliases are @p_aliases "; DESCRIPTION
This module's default exports override the core getprotoent(), getprotobyname(), and getnetbyport() functions, replacing them with versions that return "Net::protoent" objects. They take default second arguments of "tcp". This object has methods that return the similarly named structure field name from the C's protoent structure from netdb.h; namely name, aliases, and proto. The aliases method returns an array reference, the rest scalars. You may also import all the structure fields directly into your namespace as regular variables using the :FIELDS import tag. (Note that this still overrides your core functions.) Access these fields as variables named with a preceding "p_". Thus, "$proto_obj->name()" corresponds to $p_name if you import the fields. Array references are available as regular array variables, so for example "@{ $proto_obj->aliases() }" would be simply @p_aliases. The getproto() function is a simple front-end that forwards a numeric argument to getprotobyport(), and the rest to getprotobyname(). To access this functionality without the core overrides, pass the "use" an empty import list, and then access function functions with their full qualified names. On the other hand, the built-ins are still available via the "CORE::" pseudo-package. NOTE
While this class is currently implemented using the Class::Struct module to build a struct-like class, you shouldn't rely upon this. AUTHOR
Tom Christiansen perl v5.14.2 2010-12-30 Net::protoent(3perl)
All times are GMT -4. The time now is 03:18 AM.
Unix & Linux Forums Content Copyright 1993-2022. All Rights Reserved.
Privacy Policy