Beefy Boxes and Bandwidth Generously Provided by pair Networks
Clear questions and runnable code
get the best and fastest answer

Config::ApacheFormat and $/ strangeness

by InfiniteLoop (Hermit)
on Nov 28, 2006 at 12:11 UTC ( #586522=perlquestion: print w/replies, xml ) Need Help??

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

Greetings Monks,
  I have tried to use Config::ApacheFormat, to parse a apache style conf file, as below
sub read_conf { my $self = shift; my $conf_file = shift; local $/ = "\n"; my $conf = Config::ApacheFormat->new(); $conf->read($conf_file); .... }
 This code is used in a legacy system, when this method is called, the "read()" throws the following error:
Error in config file , line 1: unable to parse line at ....

Even, though I have localised the $/ and reset it to newline, I get this error. However, I can't duplicate this error anywhere else. Even doing the following, doesn't result in a error:
perl -MMyPackage -e 'local $/;my $obj = MyPackge->new;$obj->read_conf +("/path/to/conffile");'
After reading perlmod, I assumed "local" would modify $/ till the end of the "read_conf()"'s code block. I tried to enclose the local and call to "read()" in a code block, but that didn't work either. Any help in solving this is appreciated. Thanks.
 Adding a: local $/="\n";, in Config::ApacheFormat->_read() seems to solve the problem. However the strange thing is even though I had localized $/ in my method, the value was reset when Config::ApacheFormat->read() was invoked.

Replies are listed 'Best First'.
Re: Config::ApacheFormat and $/ strangeness
by perrin (Chancellor) on Nov 28, 2006 at 15:37 UTC
    Changing $/ throughout your program, as you seem to have done here, is a Bad Idea. Lots of things will break. You should consider changing it only where you really have to, and for the shortest possible amount of code. In other words, use local $/ in the place where you set it something strange, not in the place where you need it to be normal.
Re: Config::ApacheFormat and $/ strangeness
by InfiniteLoop (Hermit) on Nov 28, 2006 at 15:44 UTC
    Thanks for all the suggestion here. I did find the piece of code where there was "local $/ = undef;" , before the "read_conf" was invoked. Enclosing that piece of code in a code block solved this problem.
Re: Config::ApacheFormat and $/ strangeness
by madbombX (Hermit) on Nov 28, 2006 at 12:37 UTC
    There is no reason to change $/ if you are using a standard Apache configuration format. $/ is not used by Config::ApacheFormat in the literal sense of setting the variable. Config::ApacheFormat parses the files line by line and puts them into an array. It then goes through the lines by pop'ing them off the stack.
      Config::ApacheFormat does not explicitly make use of $/. However it does try to read the conf file, line by line, via the angle operator (<>), which is affected by value of $/ (look for $INPUT_RECORD_SEPARATOR in perlvar)

Log In?

What's my password?
Create A New User
Node Status?
node history
Node Type: perlquestion [id://586522]
Approved by ww
and the web crawler heard nothing...

How do I use this? | Other CB clients
Other Users?
Others avoiding work at the Monastery: (10)
As of 2020-06-03 10:24 GMT
Find Nodes?
    Voting Booth?
    Do you really want to know if there is extraterrestrial life?

    Results (22 votes). Check out past polls.