While reading a downloaded Perl module, I found several ways of defining input parameters, which are listed as following. What are the differences between them?
sub new{
my $class = shift;
my $self = {#_};
bless{$self, $class};
}
sub count1{
my ($self, $lab1) = #_;
}
sub new1{
my ($class, $lab1) = #_;
my $self = {};
bless $class, $self;
}
sub setpath{
my $self = shift;
}
When a subroutine is called, the passed parameters are put into a special array #_. One can consume this array by shifting values out my $foo = shift or by direct array assignment my ($foo,$bar)=#_; It is even possible to use the values directly from the array: $_[0]
Why one versus the others? Direct array assignment is the most standard and common. Sometimes the shift way is used when there are optional trailing values. Direct array usage is discouraged except in few small niches: wrapper functions that are calling other functions, especially inside of objects. functions that wrap other functions and and modify the inputs. Also the special form of goto &func which immediately drops the current call stack and calls func on the current value of #_.
# use shift for optional trailing values
use v5.10;
my $foo = shift;
my $bar = shift // 'default bar value';
my $baz = shift // 'default baz value';
#obj method to call related non-object function.
sub bar { my $self = shift; _bar(#_) }
sub longname { shortname(#_) }
sub get { return $_[0]->$_[1]; }
#1 and #3 are examples of associating an object with a class (Object Oriented Perl).
In #2, #_ is the list of parameters passed to the function, so $self and $lab1 get the values of the first 2 passed parameters.
In #4, shift() is a built in Perl subroutine that takes an array as an argument, then returns and deletes the first item in that array. If it has no argument, it is executed implicitly on #_. So $self gets the value of the first passed parameter.
Related
A while ago, I wrote a routine that parses a given string and returns a record in the form of a hash (field => value, field2 => value2). Great, except requirements have changed and I now need to return more data and offer getter methods to get at this data. So, I adjusted the routine to return a Record object which stores this same hash in a data attribute.
However, this will break legacy code that expects a hash so that it can get at the data using $record->{field}. With the new Record object, the path to this data is now $record->{data}->{field} or $record->getByShortName('field').
My idea was to overload the object's FETCH method and return the corresponding field. However, this does not seem to work. It looks like FETCH is never called.
I'm looking for three pieces of advice:
How can I overload my object correctly so that hash access attempts are redirected to a custom method of the object?
Is this an advisable way of working or will there be a massive speed penalty?
Are there better methods to keep backward compatibility in my case?
Here's an MVE:
Record.pm
package Record;
use strict;
use warnings;
use Data::Dumper;
use overload fallback => 1, '%{}' => \&access_hash;
sub new {
my ($class, %args) = #_;
my %fields = (answer => 42, question => 21);
$args{fields} = \%fields;
return bless { %args }, $class;
}
sub access_hash {
my ($self) = shift;
return $self; # cannot return $self->{fields} because that would recurse ad infinitum
}
sub FETCH {
print(Dumper(#_)); # does not return anything, is this method not being called
}
test.pl
use Record;
my $inst = Record->new();
print($inst->{answer}."\n");
print($inst->{question}."\n");
Record is a blessed hash reference, so if you overload the %{} operator, you will have trouble accessing the fields of the underlying hash.
The overload authors thought about this, and provided the overloading pragma as a way to disable overloading for this and some other use cases.
use overload '%{}' => \&access_hash;
...
sub access_hash {
no overloading '%{}';
my ($self) = shift;
return $self->{fields};
}
Prior to Perl 5.10, the workaround was to disable overloading by temporarily reblessing your object to something that wouldn't activate your overloaded operators.
sub access_hash {
my ($self) = shift;
my $orig_ref = ref($self);
bless $self, "#$%^&*()";
my $fields = $self->{fields};
bless $self, $orig_ref;
return $fields;
}
You don't neccessarily need a dedicated constructor for Perl objects. You can define your Record class, and then simply return bless $hashref, 'Record'; where you are now doing return $hashref;. All code that operates directly on the hashref will continue to work, but you will also be able to call methods on it.
I'm learning the OOP concept in Perl where the syntax is different from Java, which I used to learn the OOP concept.
I have an example to declare a class Person, but I'm a bit confused.
The code is as follows
package Person;
sub new {
my $class = shift;
my $self = {
_firstName => shift,
_lastName => shift,
_ssn => shift,
};
# Print all the values just for clarification.
print "First Name is $self->{_firstName}\n";
print "Last Name is $self->{_lastName}\n";
print "SSN is $self->{_ssn}\n";
bless $self, $class;
return $self;
}
From the example above, is my $self a scalar variable or a hash variable?
As far as I know, hash variables in Perl are declared with % while $ is used for scalar variables.
And what is the use of the bless function? Why does it return $self?
From the example above, is my $self a scalar variable or a hash variable?
$self is a scalar variable. You can tell that because it starts with a $.
(Parenthetical update: In the comments, brian points out that this rule is flawed. And he's right of course - as he usually is. The $ at the start of $self shows that it's a scalar value, not a scalar variable. But if you take the $ at the start together with the lack of look-up brackets - like [...] or {...} - following the variable name, then you can be sure this is a scalar variable.)
But your complete line of code is this:
my $self = {
_firstName => shift,
_lastName => shift,
_ssn => shift,
};
Here, the { ... } is an "anonymous hash constructor". It creates a hash and returns a reference to it. References are always scalar values, so they can be stored in scalar variables (that's one of the major reasons for their existence).
So $self is still a scalar variable, but it contains a reference to a hash.
And what is the use of the bless function?
The call to bless() effectively assign a type to the $self variable. Perl needs to know the type of your object in order to know which symbol table to search for the object's methods. When I'm running classes in this, I like to say that bless() is like writing the type of the object on a post-it note and slapping it on the object's forehead - so, later on, people can know what the type is.
Why does it return $self?
You will call this method something like this:
my $person = Person->new(...); # passing various parameters
The new() method needs to return the newly created object in order that you can store it in a variable and manipulate it in some way later on.
But a Perl subroutine returns the value of the last expression in the subroutine, and bless() returns the "blessed" object, so it would be fine to end the subroutine with the previous line:
bless $self, $class;
But it's traditional to be more explicit about return values, so most people would add the return line. It makes for better documented code.
$self is a hash reference, so it's considered as a scalar in most manipulations.
bless is used to say that this hash reference is an object of the given class (which will be Person here unless you have child classe), so that you can call functions of this class using the $object->function notation.
And the return $self is necessary so you can actually retrieve the created object when you call new!
I want to create a hash of optional query parameters that are sometimes passed to my subroutine. Sometimes a query parameter called welcome is passed in, and the value is either 1 or 0.
If that variable exists, I want to add it to a hash.
I've created a configuration value called OPTIONAL_URL_PARAMS which is a list of expected parameter names that can be passed in:
use constant OPTIONAL_URL_PARAMS => ("welcome")
So far I have:
my $tempParams = {};
if ( $optionalParam ) {
foreach my $param (#{&OPTIONAL_URL_PARAMS}) {
if ($optionalParam eq $self->{$param}) {
$tempParams->{$param} = $optionalParam;
$tempParams->{$param} =~ s/\s+//g; # strip whitespace
}
}
}
But this tries to use the value of $self->{$param} instead of its name. i.e. I want welcome to match welcome, but it's trying to match 1 instead.
I know when it comes to keys in a hash you can use keys %hash, but is there a way you can do this with regular variables?
Edit
My subroutine is being called indirectly:
my $url_handler = URL::Handler->new($param, $optionalParam);
sub new {
my $class = shift;
my $args = #_;
my $self = {
param => $args{param},
optionalParams => $args{optionalParam}
};
}
If $optionalParam's variable name is 'welcome', then I want to try and map it to the constant welcome.
This is not an answer any more, but I cannot remove it yet as there is still a discussion going on to clarify the question.
foreach my $param (#{&OPTIONAL_URL_PARAMS}) {
# ...
}
The return value of OPTIONAL_URL_PARAMS (you already got an error here and that's why you have the &, that should have told you something...) is simply a list, not an array ref. Actually at this point it should throw an error because you cannot use 1 as an array reference.
Edit
In Perl, when you pass arguments to a subroutine, all the values are flattened into a single list (reference). Specifically, if you are passing parameters to a sub, the sub doesn't know the names of the variables you originally used. It only knows their values. Therefore, if you want names as well as values, you have to pass them separately. An easy way is using a hash. E.g., in new():
my $class = shift;
my $param = shift; # The first, required parameter
my %therest = (#_); # Optional parameters, if any
Then you can say URL::Handler->new($param, 'welcome' => 1), and $therest{welcome} will have the value 1. You can also say
URL::Handler->new($param, 'welcome'=>1, 'another'=>42);
and %therest will have the two keys welcome and another.
See also some further discussion of passing whole hashes as parameters
Original
This also probably doesn't answer the question!
Some thoughts on the code from your comment.
my $url_handler = URL::Handler->new($param, $optionalParam);
sub new {
my $class = shift; # OK; refers to URL::Handler
my $args = #_; # Problematic: now $args is the _number_ of args passed (list #_ evaluated in scalar context).
my $self = {
# There is no %args hash, so the next two lines are not doing what you expect.
# I actually don't know enough perl to know what they do! :)
param => $args{param},
optionalParams => $args{optionalParam}
};
}
Some thoughts:
use strict; and use warnings; at the top of your source file, if you haven't yet.
I can think of no languages other than Algol 60 that support this idea. It goes against the idea of encapsulation, and prevents you from using an array or hash element, a function call, a constant, or an expression as the actual parameter to a call
Variable names are purely for the convenience of the programmer and have no part in the functionality of any running program. If you wished so, you could write your code using a single array #memory and have "variables" $memory[0], $memory[1] etc. But you would be bypassing the most useful part of compiler technology that allows us to relate readable text to memory locations. It is best to consider those names to be lost once the program is running
The called code should be interested only in the values passed, and it would be a nightmare if the name of a variable passed as an actual parameter were significant within the subroutine
If you were able to access the name of a variable passed as a parameter, what do you suppose would be provided to subroutine stats if the call looked like this
stats( ( $base[$i] + 0.6 + sum(#offset{qw/ x y /}) + sum(#aa) ) / #aa )
In summary, it cannot be done in general. If you need to associate a value with a name then you should probably be looking at hashes
Your code
my $url_handler = URL::Handler->new($param, $optionalParam);
sub new {
my $class = shift;
my $args = #_;
my $self = {
param => $args{param},
optionalParams => $args{optionalParam}
};
}
has a number of problems
You correctly shift the name of the class from parameter array #_, but then set my $args = #_, which sets $args to the number of elements remaining in #_. But the value of $args is irrelevant because you never use it again
You then set $self to a new anonymous hash, which is created with two elements, using the values from hash %args. But %args doesn't exist, so the value of both elements will be undef. Had you put use strict and use warnings 'all' in place you would have been alerted to this
The keys that you're using to access this non-existent hash are param and optionalParam, and I think it's more than a coincidence that they match the names of the actual parameters of the call to new
While Perl is unusual in that it allows programmatic access to its symbol tables, it is an arcane and unrecommended method. Those names are essentially hidden from the program and the programmer and while modules like Exporter must manipulate symbol tables to do their job, any such behaviour inside base-level software is very much to be avoided
Finally, you never use $self again after defining it. You should be blessing it into a class according to the $class variable (which contains the string URL::Handler) and returning it from the constructor
I hope this helps
My program creates an object, which, in turn, creates another object
MainScript.pm
use module::FirstModule qw ($hFirstModule);
$hFirstModule->new(parametres);
$hFirstModule->function();
FirstModule.pm
use Exporter ();
#EXPORT = qw($hFirstModule);
use module::SecondModule qw ($hSecondModule);
sub new {
my $className = shift;
my $self = { key => 'val' };
bless $self, $classname;
return $self;
}
sub function{
$hSecondModule->new(parametres);
#some other code here
}
I want to acces $hSecondModule from MainScript.pm.
It depends.
We would have to see the actual code. What you've shown is a bit ambiguous. However, there are two scenarios.
You can't
If your code is not exactly like what you have shown as pseudo-code, then there is no chance to do that. Consider this code in &module1::function.
sub function {
my $obj = Module2->new;
# ... more stuff
return;
}
In this case, you are not returning anything, and the $obj is lexically scoped. A lexical scope means that it only exists inside of the closest {} block (and all blocks inside that). That's the block of the function sub. Once the program returns out of that sub, the variable goes out of scope and the object is destroyed. There is no way to get to it afterwards. It's gone.
Even if it was not destroyed, you cannot reach into a different scope.
You can
If you however return the object from the function, then you'd have to assign it in your script, and then you can access it later. If the code is exactly what you've shown above, this works.
sub function {
my $obj = Module2->new;
# nothing here
}
In Perl, subs always return the last true statement. If you don't have a return and the last statement is the Module2->new call, then the result of that statement, which is the object, is returned. Of course it also works if you actually return explicitly.
sub function {
return Module2->new;
}
So if you assign that to a variable in your script, you can access it in the script.
my $obj = module1->function();
This is similar to the factory pattern.
This is vague, but without more information it's impossible to answer the question more precicely.
Here is a very hacky approach that takes your updated code into consideration. It uses Sub::Override to grab the return value of the constructor call to your SecondModule thingy. This is something that you'd usually maybe do in a unit test, but not in production code. However, it should work. Here's an example.
Foo.pm
package Foo;
use Bar;
sub new {
return bless {}, $_[0];
}
sub frobnicate {
Bar->new;
return;
}
Bar.pm
package Bar;
sub new {
return bless {}, $_[0];
}
sub drink {
return 42; # because.
}
script.pl
package main;
use Foo; # this will load Bar at compile time
use Sub::Override;
my $foo = Foo->new;
my $bar; # this is lexical to the main script, so we can use it inside
my $orig = \&Bar::new; # grab the original function
my $sub = Sub::Override->new(
"Bar::new" => sub {
my $self = shift;
# call the constructor of $hSecondModule, grab the RV and assign
# it to our var from the main script
$bar = $self->$orig(#_);
return $bar;
}
);
$foo->frobnicate;
# restore the original sub
$sub->restore;
# $bar is now assigend
print $bar->drink;
Again, I would not do this in production code.
Let's take a look at the main function. It first creates a new Foo object. Then it grabs a reference to the Bar::new function. We need that as the original, so we can call it to create the object. Then we use Sub::Override to temporarily replace the Bar::new with our sub that calls the original, but takes the return value (which is the object) and assigns it to our variable that's lexical to the main script. Then we return it.
This function will now be called when $foo->frobnicate calls Bar->new. After that call, $bar is populated in our main script. Then we restore Bar::new so we don't accidentally overwrite our $bar in case that gets called again from somewhere else.
Afterwards, we can use $bar.
Note that this is advanced. I'll say again that I would not use this kind of hack in production code. There is probably a better way to do what you want. There might be an x/y problem here and you need to better explain why you need to do this so we can find a less crazy solution.
I have the following sub-routine.
sub Function{
my $ref = \($_[0]);
if(ref($ref) eq 'SCALAR'){
. . .
}
}
I am trying to get it to work such that passing a list, scalar or hash to it, it converts that into a reference and depending upon whether that reference is of type ARRAY, SCALAR or HASH, different actions are performed.
It could be called as
Function(%a)
Function($a)
Function(#a)
How can I make $ref be a reference to whatever is passed to the Function? My current approach isn't working.
Function(%a)
Function($a)
Function(#a)
You can't do what you are asking. There is no way inside Function to determine whether an array or a hash was passed, because, in fact, you can't pass an array or a hash to a sub in the first place. You can only pass a list; the hashes and arrays are converted to lists in the sub calls above.
You should pass a reference to the function in the first place:
Function(\%a)
Function($a)
Function(\#a)
Then you can check what it is easily:
sub Function {
my $param = shift;
if (not ref $param) {
...
} elsif (ref $param eq 'HASH') {
...
} elsif (ref $param eq 'ARRAY') {
...
}
You can use a prototyped function. Although Perl 5 prototypes are a total mess, they can be used quite well in this case. As I understand them the prototype
sub Function (\[$#%]) {...}
should solve your problem, and the reference be in $_[0].
The big disadvantage is that the variable you pass has to start with the $, #, or % character, so you can't call Function with a constant argument directly:
Function(1)
fails, but
my $temp = 1;
Function($temp)
works.
Your approach failes, because #_ is a list, and all elements are scalars.
Why not just check to see if the argument passed in is a reference and act accordingly?
sub gimme_a_ref
{
my $arg=shift;
if(ref $arg ne ref "")
{
return $arg;
}
else
{
return \$arg;
}
}