cpp-style include for perl?

Discussion in 'Perl Misc' started by Mike Ballard, Jan 22, 2006.

  1. Mike Ballard

    Mike Ballard Guest

    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
    --
     
    Mike Ballard, Jan 22, 2006
    #1
    1. Advertising

  2. Mike Ballard

    Uri Guttman Guest

    >>>>> "MB" == Mike Ballard <dont_w@nt_spam.org> writes:

    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

    --
    Uri Guttman ------ -------- http://www.stemsystems.com
    --Perl Consulting, Stem Development, Systems Architecture, Design and Coding-
    Search or Offer Perl Jobs ---------------------------- http://jobs.perl.org
     
    Uri Guttman, Jan 22, 2006
    #2
    1. Advertising

  3. Mike Ballard

    Mike Ballard Guest

    On Sun Jan 22, I was peacefully napping until Uri Guttman said:

    > >>>>> "MB" == Mike Ballard <dont_w@nt_spam.org> writes:

    >
    > 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
    --
     
    Mike Ballard, Jan 22, 2006
    #3
  4. [OT] Re: cpp-style include for perl?

    Uri Guttman <> wrote in
    news::

    > <and yes, i am back! :)>


    It was about time :) Great to have you back.

    Sinan
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jan 22, 2006
    #4
  5. Mike Ballard <dont_w@nt_spam.org> wrote in
    news:m2r770m1ml.fsf@west_f1.net:

    >
    > On Sun Jan 22, I was peacefully napping until Uri Guttman said:
    >
    >> >>>>> "MB" == Mike Ballard <dont_w@nt_spam.org> writes:

    >>
    >> MB> I have a number of scripts where many have identical blocks of
    >> MB> code (subroutines, some variable setting, etc.);


    ....

    >> what is confusing you?


    ....

    > 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

    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jan 22, 2006
    #5
  6. >>>>> "Mike" == Mike Ballard <dont_w@nt_spam.org> writes:

    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?

    --
    Randal L. Schwartz - Stonehenge Consulting Services, Inc. - +1 503 777 0095
    <> <URL:http://www.stonehenge.com/merlyn/>
    Perl/Unix/security consulting, Technical writing, Comedy, etc. etc.
    See PerlTraining.Stonehenge.com for onsite and open-enrollment Perl training!
     
    Randal L. Schwartz, Jan 22, 2006
    #6
  7. Mike Ballard

    Anno Siegel Guest

    Uri Guttman <> wrote in comp.lang.perl.misc:

    > <and yes, i am back! :)>


    Oh... good you mention it, otherwise I would have.

    Welcome back!

    Anno
    --
    If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers.
     
    Anno Siegel, Jan 22, 2006
    #7
  8. Mike Ballard

    Guest

    Mike Ballard <dont_w@nt_spam.org> wrote:

    > 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
     
    , Jan 23, 2006
    #8
  9. Mike Ballard

    Joe Smith Guest

    Mike Ballard wrote:

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


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

    -Joe
     
    Joe Smith, Jan 23, 2006
    #9
  10. Mike Ballard

    Mike Ballard Guest

    On Mon Jan 23, I was peacefully napping until Joe Smith said:

    > Mike Ballard wrote:
    >
    > > @filelist = `/bin/ls -1 abs*`;

    >
    > 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
    --
     
    Mike Ballard, Jan 23, 2006
    #10
  11. Mike Ballard <dont_w@nt_spam.org> wrote in
    news:m2u0buh7ek.fsf@west_f1.net:

    > 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
    --
    A. Sinan Unur <>
    (reverse each component and remove .invalid for email address)

    comp.lang.perl.misc guidelines on the WWW:
    http://mail.augustmail.com/~tadmc/clpmisc/clpmisc_guidelines.html
     
    A. Sinan Unur, Jan 23, 2006
    #11
  12. Mike Ballard

    Paul Lalli Guest

    Mike Ballard wrote:
    > 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
     
    Paul Lalli, Jan 24, 2006
    #12
  13. Joe Smith <> wrote:
    > Mike Ballard wrote:
    >
    >> @filelist = `/bin/ls -1 abs*`;

    >
    > 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.


    --
    Tad McClellan SGML consulting
    Perl programming
    Fort Worth, Texas
     
    Tad McClellan, Jan 24, 2006
    #13
  14. Mike Ballard

    Joe Smith Guest

    Mike Ballard wrote:

    > 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.
     
    Joe Smith, Jan 29, 2006
    #14
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rolf Magnus
    Replies:
    2
    Views:
    608
    Karl Heinz Buchegger
    Nov 28, 2003
  2. Elie Nader
    Replies:
    1
    Views:
    646
  3. DrUg13
    Replies:
    1
    Views:
    482
    DrUg13
    Feb 10, 2004
  4. Alex Vinokur
    Replies:
    7
    Views:
    414
    Greg Comeau
    Nov 15, 2004
  5. Kceiw
    Replies:
    3
    Views:
    1,006
    Jim Langston
    Mar 14, 2006
Loading...

Share This Page