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

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

Related

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.

Perl - Global symbol $DIR requires explicit package name (why can't I print the output?) [closed]

Closed. This question is not reproducible or was caused by typos. It is not currently accepting answers.
This question was caused by a typo or a problem that can no longer be reproduced. While similar questions may be on-topic here, this one was resolved in a way less likely to help future readers.
Closed 6 years ago.
Improve this question
#!/db/pub/infra/CPAN/perl/5.8.8/bin/perl
use lib '/db/pub/eq/arina/global/perl/lib';
use lib '/db/pub/infra/CPAN/perl/5.8.8/lib/site_perl/5.8.8';
use DBI;
use strict;
sub main {
my $dir = "/data/dbxpc2_archive/BookingManager/2017-01-12/data/PC1/millennium.ignore.ftp.noencrypt.DB_USD";
my #files = glob "${DIR}/*.csv";
print #files;
}
main();
You get the error because you are using a variable ($DIR) you never declared. You do have a declared a variable named $dir which appears to be the one you intended to use.
glob "${dir}/*.csv"
glob "$dir/*.csv"
glob $dir."/*.csv"
Your code mishandles paths with spaces, *, ?, etc. Fix the injection bug using
glob "\Q${dir}\E/*.csv"
glob "\Q$dir\E/*.csv"
glob quotemeta($dir)."/*.csv"

How can I export or use a variable in 1 file to a function in another file in perl? [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 8 years ago.
Improve this question
I am very new to perl, so don't have much knowledge in perl scripting.
I have two files test1.pl and testfinal.pl.
testfinal.pl
for loop{
$var = $_;
my $out = `perl test1.pl -p $var`;
}
test1.pl
Foo();
sub Foo(){
# I want to get $var from testfinal.pl so that I can perform some functions of that perticular varaible.
$elt = `mkdir $var`;
}
I checked some links, but I found for exporting I need to make the file in '.pm' format (testfinal.pm) which is not possible as I need to get the final output but executing testfinal.pl.
Can anyone help me here quickly.Please...
Pathak has covered some fine ways of passing your information through the file system, but I also note that you've passed $var through the command line. test1.pl should already have that info in #_, specifically as $_[1].
Examples:
Foo();
sub Foo(){
$elt = `mkdir $_[1]`;
}
or better
Foo($_[1]);
sub Foo{ #prototype deleted, probably should stay that way...
my $dir = shift;
$elt = `mkdir $dir`;
}
For cleaner handling of command line parameters, the GetOpt::Long module is core.
If you aren't attached to launching a shell & a 2nd instance of the perl executable, some other approches for running 2 files as a single program are the keywords use and require. (check perldoc for details.) These approaches allow you to share package variables or to directly pass parameters to the target subrutines.
I suggest that you create a module as documented in perlmod and Exporter.
The following demonstrates how to do this with your setup:
MyModule.pm:
package MyModule;
use strict;
use warnings;
use Exporter qw(import);
sub Foo {
my $var = shift;
print "mkdir $var\n";
}
testfinal.pl:
use strict;
use warnings;
use MyModule qw(Foo);
my #array = (...);
for my $var (#array) {
my $out = Foo($var);
}
Also note that perl has a native mkdir function, so there is no need to shell out to the system for that functionality.

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,

perl programming to make function calls [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 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.