Beefy Boxes and Bandwidth Generously Provided by pair Networks
more useful options
 
PerlMonks  

distribution config file

by mifflin (Curate)
on Jun 18, 2006 at 15:37 UTC ( [id://556105]=perlquestion: print w/replies, xml ) Need Help??

mifflin has asked for the wisdom of the Perl Monks concerning the following question:

I want to include a configuration file in my distribution.

So far I've come up with the following in my class to get a config file name and pass it the the super class constructor. The config file will be in the same directory as the class but with a .txt extension.

package GetPaid::FlatFile::Artran; use warnings; use strict; use File::Spec; use base qw(GetPaid::FlatFile); our $VERSION = '0.01'; my @Path = split /::/, __PACKAGE__; my $Configfile = File::Spec->rel2abs((join '/', @Path). ".txt"); sub new { my ($class, $dir) = @_; $dir = '.' unless defined $dir; my $this = $class->SUPER::new( "$dir/" . uc(pop @Path) . '.TXT', # data file to create $Configfile ); bless $this, ref($class) || $class; } 1;
Is this the correct way to do this?
Will there be any conditions under which this will not work?

Replies are listed 'Best First'.
Re: distribution config file
by jhourcle (Prior) on Jun 18, 2006 at 15:57 UTC

    I personally use __FILE__ to determine where the module is, and then determine the path seperator from that (although, I've never tested it on a non-unix system), and build the path to look at from there.

    if (!defined($__registryDir)) { $__registryDir = __FILE__; ($__pathSeperator) = ($__registryDir =~ m/\bVSO\b(.*?)\bRegist +ry/ ); $__registryDir =~ s/\.pm$/${__pathSeperator}data/; }

    (the module is 'Physics::Solar::VSO::Registry', so I know that the characters between 'VSO' and 'Registry' should be the path seperator).

    I think there's a way to get the path seperator from the perl config, also.

      There are two approaches to writing portable code to deal with paths. All systems running perl can cope with POSIX filenames, i.e. using '/' as a path separator. This works except when performing textual manipulation on a file spec in native format, though Windows can sometimes cope with a mixture of forward slashes and backslashes.

      The other approach is to use File::Spec, Cwd and friends in conjunction with the native syntax. If you do this, stick to native syntax throughout. Note that some OSes e.g. VMS, don't have a single 'path separator' but something more complicated.

      For more on this, see perldoc perlport and my YAPC portability talk slides.

      By the way, I don't think your approach will work in the OP's case.__FILE__ is an alias for $0 as far as I am aware, so this may not contain any path separators at all.

      --

      Oh Lord, won’t you burn me a Knoppix CD ?
      My friends all rate Windows, I must disagree.
      Your powers of persuasion will set them all free,
      So oh Lord, won’t you burn me a Knoppix CD ?
      (Missquoting Janis Joplin)

        "By the way, I don't think your approach will work in the OP's case.__FILE__ is an alias for $0 as far as I am aware, so this may not contain any path separators at all."

        "__FILE__ is the filename at that point in the program." -- Programming Perl.
        "$0 is the name of the program being executed." -- perldoc perlvar
Re: distribution config file
by IOrdy (Friar) on Jun 19, 2006 at 07:39 UTC
    How about a combination of rel2abs and splitpath to get the directory.
    #!/usr/bin/perl use strict; use warnings; use File::Spec::Functions qw(rel2abs splitpath); print rel2abs((splitpath(__FILE__))[1]);

Log In?
Username:
Password:

What's my password?
Create A New User
Domain Nodelet?
Node Status?
node history
Node Type: perlquestion [id://556105]
Approved by McDarren
help
Chatterbox?
and the web crawler heard nothing...

How do I use this?Last hourOther CB clients
Other Users?
Others having an uproarious good time at the Monastery: (7)
As of 2024-03-28 10:48 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found