cpp-style include for perl?

M

Mike Ballard

Howdy -

I have a number of scripts where many have identical blocks of code
(subroutines, some variable setting, etc.); have been reading through some
posts on packages/use/require and am getting a little lost in the
intricacies when really all I'm looking for is a cpp-style "#include" stmt
("C preprocessor", not C++) - is there anything like that for perl?


Mike
--
 
U

Uri Guttman

MB> I have a number of scripts where many have identical blocks of
MB> code (subroutines, some variable setting, etc.); have been reading
MB> through some posts on packages/use/require and am getting a little
MB> lost in the intricacies when really all I'm looking for is a
MB> cpp-style "#include" stmt ("C preprocessor", not C++) - is there
MB> anything like that for perl?

what is confusing you? require is the proper way to load common source
in perl. use is a higher level form which works fine too. if you don't
use any package commands, they will all load into main:: and you should
see your subs just fine. variables are a different matter if you want
them to be lexical. but you need to show some of the data you want to
include and why you want to do that. in many cases that is a bad design
and can be done in other ways.

<and yes, i am back! :)>

uri
 
M

Mike Ballard

MB> I have a number of scripts where many have identical blocks of
MB> code (subroutines, some variable setting, etc.); have been reading
MB> through some posts on packages/use/require and am getting a little
MB> lost in the intricacies when really all I'm looking for is a
MB> cpp-style "#include" stmt ("C preprocessor", not C++) - is there
MB> anything like that for perl?

what is confusing you? require is the proper way to load common source
in perl. use is a higher level form which works fine too. if you don't
use any package commands, they will all load into main:: and you should
see your subs just fine. variables are a different matter if you want
them to be lexical. but you need to show some of the data you want to
include and why you want to do that. in many cases that is a bad design
and can be done in other ways.

Thanks on "use/require/pkg" info since I was very unclear on the
suitability of each for what I'm trying to do; "require" seems to be it.

The scripts are meant to be run from $path and within a dir containing
various input files. So I have this (in multiple scripts):

@filelist = `/bin/ls -1 abs*`;

Using "require" I always get a shell error:

/bin/ls: abs*: No such file or directory


Fearing divergence into var scope issues that (truthfully) I'm just not
interested in, felt using something I already know (cpp-type include) was
quickest to getting back to what's really important to me...

Mike
--
 
A

A. Sinan Unur

....

....

The scripts are meant to be run from $path and within a dir containing
various input files. So I have this (in multiple scripts):

@filelist = `/bin/ls -1 abs*`;

Using "require" I always get a shell error:

/bin/ls: abs*: No such file or directory


Fearing divergence into var scope issues that (truthfully) I'm just
not interested in,

This has nothing to with scoping. The error you get is from ls. It
indicates that the current directory is not what you think it is.

See

perldoc -f chdir
perldoc -f opendir
perldoc -f readdir
perldoc -f closedir
perldoc -f glob

Sinan
 
R

Randal L. Schwartz

Mike> Using "require" I always get a shell error:

Mike> /bin/ls: abs*: No such file or directory

Apparently, your current directory is wrong. Or a whole bunch
of other things.

Also, why the -1 on the ls command?

Also, why use an external command when Perl's glob does the job just fine?
 
A

axel

Mike Ballard said:
I have a number of scripts where many have identical blocks of code
(subroutines, some variable setting, etc.); have been reading through some
posts on packages/use/require and am getting a little lost in the
intricacies when really all I'm looking for is a cpp-style "#include" stmt
("C preprocessor", not C++) - is there anything like that for perl?

Yes. The cpp :)

Command line switch -P... but see the warnings in perldoc perlrun.

Axel
 
M

Mike Ballard

That's not the right way to do it.
@filelist = glob 'abs*';


Ok; you've made it too easy to not switch so I will. Thank you.

I also discovered that I'm only getting the shell error 'file not found'
when using cperl-db/emacs; run from c/l everything works acceptably.

And if anyone wouldn't mind answering a follow-up, enough code is
duplicated to want to not have numerous copies of the same thing but its
position is critical; and unlike cpp #include which lets you place code in
specific locations, "require" evidently hasn't that capacity. So can
someone tell me what mechanism exists to do so with perl?

What I mean is:

$vars
while...
if... # this part is much-duplicated
end

What perl mechanism can I use to pull out the "if" block ala the
subroutines I've now moved out but see that it's inserted right there
where it is now, in the "while" block?

Mike
--
 
A

A. Sinan Unur

And if anyone wouldn't mind answering a follow-up, enough code is
duplicated to want to not have numerous copies of the same thing but
its position is critical; and unlike cpp #include which lets you place
code in specific locations, "require" evidently hasn't that capacity.
So can someone tell me what mechanism exists to do so with perl?

What I mean is:

$vars
while...
if... # this part is much-duplicated
end

What perl mechanism can I use to pull out the "if" block ala the
subroutines I've now moved out but see that it's inserted right there
where it is now, in the "while" block?

Hmmm ... I have a feeling you have been misusing header files in C all
your life.

The short answer is, factor common code to subroutines instead of
talking about "a la subroutines".

Sinan
 
P

Paul Lalli

Mike said:
And if anyone wouldn't mind answering a follow-up, enough code is
duplicated to want to not have numerous copies of the same thing but its
position is critical; and unlike cpp #include which lets you place code in
specific locations, "require" evidently hasn't that capacity.

I love when people don't understand how something works and just assume
the language is somehow defficient. Did it even *occur* to you to ask
*how* to do it, rather than just stating that it can't be done because
you haven't figured it out yet?

The problem here is that you don't understand scoping in Perl. You
should Google for "Coping with Scoping".

Short answer: Declare your variables as package variables instead of
lexical (ie, with our instead of my), and make sure you declare them in
both the "main" and included file.

Correct answer:
Define a configuration subroutine which returns a hash of values, and
call that subroutine in any file which "use"s your module.

in MyConfig.pm:
package MyConfig;
use strict;
use warnings;

sub get_config {
my $config = {
path=> '/usr/bin/',
user=> 'jsmith',
#etc
};
return $config;
}

1;

in main.pl:
#!/usr/bin/perl
use strict;
use warnings;
use MyConfig;

my $cfg = MyConfig::get_config();
print "Path: $cfg->{path}\n";
__END__


For nicer ways to get access to the subroutine, learn about modules:
perldoc perlmod
perldoc Exporter

Paul Lalli
 
T

Tad McClellan

Joe Smith said:
That's not the right way to do it.


What is wrong with it?


Or did you instead mean to say that there is a _better_ way?

(which I would agree with.)

@filelist = glob 'abs*';


The array contents will not be the same as with the original code.
 
J

Joe Smith

Mike said:
I also discovered that I'm only getting the shell error 'file not found'
when using cperl-db/emacs; run from c/l everything works acceptably.

Last time I had something like this happen to me, I used the editor
to create and run a single-line program:
system("pwd");

The editor was using /tmp instead of $HOME.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,226
Members
46,815
Latest member
treekmostly22

Latest Threads

Top