Beefy Boxes and Bandwidth Generously Provided by pair Networks
No such thing as a small change
 
PerlMonks  

Config::ApacheFormat and $/ strangeness

by InfiniteLoop (Hermit)
on Nov 28, 2006 at 17:11 UTC ( [id://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.
Update:
 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 20: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 20: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 17: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?
Username:
Password:

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

How do I use this?Last hourOther CB clients
Other Users?
Others drinking their drinks and smoking their pipes about the Monastery: (5)
As of 2024-04-19 22:36 GMT
Sections?
Information?
Find Nodes?
Leftovers?
    Voting Booth?

    No recent polls found