perl programming to make function calls [closed] - perl

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 8 years ago.
Improve this question
I want to call a function from my program.So can I save the function in another file instead of mixing with the script from where I am calling the function.If so ,with what name should I save the file with function.Please help..

You can write a module. Here is a small module:
File Foo.pm
# declare a namespace
package Foo;
# always use these, they help you find errors
use strict; use warnings;
sub foo {
print "Hello, this is Foo\n";
}
# a true value as last statement.
1;
You place that file into the same directory as your script:
File script.pl:
use strict; use warnings;
# load the module
use Foo;
Foo::foo(); # invoke the function via the fully qualified name
To create nicer modules, you'll probably look into object orientation, or the Exporter module.

Related

What does 'Cwd' and 'qw' mean in perl? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 2 years ago.
Improve this question
In perl, we import/use module we use Cwd;
qw as representation of list/array.
But, I see in some code it's mentioned as below.
use Cwd qw(realpath cwd);
realpath( $0 ) =~ /(.+)\/[^\/]+$/;
require "$1/some_file.pl"
What does this mean ? Can you please help me to understand ?
qw/STRING/ is the quote-word quote-like operator.
qw(realpath cwd)
is equivalent to
split(' ', q(realpath cwd))
and thus to
'realpath', 'cwd'
So,
use Cwd qw(realpath cwd);
is equivalent to
use Cwd 'realpath', 'cwd';
According to the documentation,
use Module LIST;
is equivalent to
BEGIN {
require Module;
Module->import(LIST);
}
so
use Cwd 'realpath', 'cwd';
is equivalent to
BEGIN {
require Cwd;
Cwd->import('realpath', 'cwd');
}
So what does import do? Well, that's entirely up to the module. It's common for modules to export the listed symbols into the caller's namespace. Cwd is no exception.
So, the following loads Cwd (if it's not already), and imports the functions realpath and cwd from it.
use Cwd qw(realpath cwd);
Finally,
$0, documented in perlvar, is the name of the script being executed.
realpath($0) is an absolute path to the script being executed, with symlinks resolved.
The regex match is used to extract everything up to the last /, i.e. the directory name in which the script is located.
Finally, require executes the specified file. (Although require is not the correct tool for this.)
A simpler version of your code:
use FindBin qw( $RealBin );
require("$RealBin/some_file.pl");

what does it mean 'push #{bla bla }' in Perl? [closed]

Closed. This question needs debugging details. It is not currently accepting answers.
Edit the question to include desired behavior, a specific problem or error, and the shortest code necessary to reproduce the problem. This will help others answer the question.
Closed 2 years ago.
Improve this question
I read try to read perl code of annovar and there is a line like this:
push #{$genedb{$chr, $nextbin}}, [$name, $dbstrand, $txstart, $txend, $cdsstart, $cdsend, [#exonstart], [#exonend], $name2];
Can someone explain what does it mean?
which values put which array or hash ?
Just run this program and Data::Dumper will show the results
#! /usr/bin/env perl
use warnings;
use strict;
use utf8;
use feature qw<say>;
use Data::Dumper;
my %genedb;
my $chr = 'G';
my $nextbin = 4143;
push #{$genedb{$chr, $nextbin}}, [1..10];
print Dumper(\%genedb);
exit(0);

while using this line use fields __PACKAGE__->SUPER::_praveen, qw(path); getting error SUPER.pm not found [closed]

Closed. This question needs details or clarity. It is not currently accepting answers.
Want to improve this question? Add details and clarify the problem by editing this post.
Closed 4 years ago.
Improve this question
use fields __PACKAGE__->SUPER::_praveen, qw(path);
while executing above line with perl_5.18.2 getting an error #INC SUPER.pm not found.
I am able to compile above line with Perl 5.8.8
can you please help me on this
That error shouldn't be produced by that line since the only module loaded by that line is fields.pm. Unsurprisingly, I can't replicate your error.
Foo.pm:
package Foo;
use strict;
use warnings qw( all );
sub _praveen { qw( id ) }
1;
Bar.pm:
package Bar;
use strict;
use warnings qw( all );
use parent 'Foo';
use fields __PACKAGE__->SUPER::_praveen, qw( path );
1;
a.pl:
use strict;
use warnings qw( all );
use feature qw( say );
use FindBin qw( $RealBin );
use lib $RealBin;
use Bar qw( );
say "ok";
Output:
$ perlbrew use 5.18.2t
$ perl a.pl
ok
Please provide a minimal, runnable demonstration of the problem.
I'm not surprised you're getting errors: the fields pragma expects a list of field names as parameters, and even if the inherited class provides something suitable through a call like that, it is far from clear whether the superior class is in scope at that point.
If you want more help then you must show a minimal code sample that reproduces the issue.

How to add optional argument in perl [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 7 years ago.
Improve this question
I want to do command line argument parsing for a directory path in perl. I want to make this argument as optional.
so when the user gives path, it showed be assigned to a variable $release_model else it will execute other code I have written for finding directory from main directory. I am new to perl but somehow coded following. Can anybody help?
Getopt::Long
my $command_line = $GetOptions(\%Opts, "help|h","email=s#","test","model");
if($command_line==0){
print "$PROGRAM: no arguments are given";
Usage{};
}
else die "No arguments were given"
But it doesn't accept model as optional argument and throws error.
I just started working with perl.
It is quite hard to guess what exactly you are after as the code provided contains lots of errors and other features not described. But to start learning with something simple, here is something that I hope matches your requirements.
#!/usr/bin/perl
use strict;
use warnings;
use Getopt::Long;
my $release_model = '/path/to/"main directory"'; # default value
GetOptions( 'model=s' => \$release_model )
or die("Error in command line arguments\n");
print "Release model is: $release_model\n";
If you save this to a file (e.g. my_program.pl) and make it executable then you can see it provides these features:
If you call it without arguments ./my_program.pl, the default value of $release_model will be used.
If you call it with argument model (e.g. ./my_program.pl --model /another/directory), the provided value will be assigned to $release_model.
If you call it with wrong arguments (e.g. ./my_program.pl --mdoel), it prints reasonable error message and exits.
Try it yourself. And go and read some tutorial on Perl if you want to do some serious work.

How can I set a Java-like "static variable" in Perl that can be accessed by other classes ? [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 years ago.
Improve this question
I have two classes say A and B, i need to set a static variable in Class A (like static variables in Java ), and access the variable from class B (using ClassName.variable name in Java ). Can i do something like this in Perl .
Thanks in advance
tree .
├── foo.pl
└── lib
├── A.pm
└── B.pm
cat lib/A.pm
package A;
use strict;
use warnings;
our $foo = 7;
1;
cat lib/B.pm
package B;
use strict;
use warnings;
use feature qw/ say /;
use A;
say $A::foo;
1;
cat foo.pl
#!/usr/bin/env perl
use strict;
use warnings;
use B;
perl -Ilib foo.pl
7
I don't know Java really so I'm guessing that what you mean by "static variable" is something to do with scoping? In perl 'my' and 'our' are the ways you can control scope, but I believe I am correct in saying hat packages/modules make variable's scope "private" to the .pm file they are declared in (correct this and/or elaborate further fellow perlistas!).
As for how to "access" them hmm my copy of Programming Perl (2nd Edition) covers this in chapter 2 in the section on Scoped Declarations. But here's a pithy (slightly edited) part of the first footnote from page 107:
Packages are used by libraries, modules, and classes to store their own private data so it doesn't conflict with data in your main program. If you see someone write $Some::stuff they're using the $stuff scalar variable from the package Some.
The Exporter documentation and this perlmonks node about global variables might help you get clearer in your thinking about variables and scope in perl. The classic perlmonks node - Variable Scoping in Perl: the basics - is a frequently consulted reference :-)
If you already know how to program (i.e. in Java) sometimes another good reference (only slightly dated) for "how to" do things in Perl is The Perl Cookbook - excerpts of which you can find online.
Cheers,