You're going to have to be more specific about how to tell the key from the value in your file? Are they just seperated by whitespace?
Although his post was poorly formatted I think that they're separated by comma. But then there's no indication whatsoever whether there may be commas in the key and/or in the value respectively -suitably quoted, of course-, in which case Text::CSV_XS may be the best option.
unless ( open( FILE, "name_of_file" ) ) { die "Could not open file for reading\n"; }
Well, nothing to say about this, but the "open or die" idiom is so typically perlish that it seems strange to see anything different...

As a possibly minor point, I always prefer the three args form of open and I prefer to use "lexical filehandles". I have to say that wrt this issue Abigail himself in clpmisc once supplied good arguments in favour of the two-args form. But as general rule my indications should be preferable in most situations, with modern enough perls.

Also, it is generally recommended, and is indeed sentible, to include $! in the error message.

while (<FILE>) { if ($_ =~ /^(maintenance_mode)\s([\w,=]*)$/) {
You see, $_ is the "topicalizer". Its whole point (well, not the whole point, but a good portion of it) is that it is the implicit argument of many "actions". So
$var =~ /whatever/;
is OK, but
$_ =~ /whatever/;
is plainly equivalent to
/whatever/;
with the only difference that the latter is clearer. Well, to a perl programmer, of course! Occasionally people even uses a for for its aliasing properties only, e.g.:
for ($foo) { s/^"//; s/"$//; s/\W+/-/; } # or also: # s/^"//, s/"$//, s/\W+/-/ for $foo;
BTW: your regexp will only match one key, so that I wouldn't regard it as especially useful for filling a hash;
$hash{$1} = $2; } }

Also, what you did is certainly legitimate, but since here we're talking about breaking a string in two parts, perhaps split could have been better suited for the job. This, of course, modulo the caveat I already took care of above. En passant it occurs to me that it may be worth to remind you of the LIMIT parameter of split, that is not that known after all...

PS: for such minimal examples (and for much more) IMHO the ARGV filehandle is invaluable. See also the discussion going on at How would you do it? Re ARGV, fundamentally!, and especially merlyn's answer


In reply to Re^2: create a hash by reading file by blazar
in thread create a hash by reading file by s_gaurav1091

Title:
Use:  <p> text here (a paragraph) </p>
and:  <code> code here </code>
to format your post, it's "PerlMonks-approved HTML":



  • Posts are HTML formatted. Put <p> </p> tags around your paragraphs. Put <code> </code> tags around your code and data!
  • Titles consisting of a single word are discouraged, and in most cases are disallowed outright.
  • Read Where should I post X? if you're not absolutely sure you're posting in the right place.
  • Please read these before you post! —
  • Posts may use any of the Perl Monks Approved HTML tags:
    a, abbr, b, big, blockquote, br, caption, center, col, colgroup, dd, del, details, div, dl, dt, em, font, h1, h2, h3, h4, h5, h6, hr, i, ins, li, ol, p, pre, readmore, small, span, spoiler, strike, strong, sub, summary, sup, table, tbody, td, tfoot, th, thead, tr, tt, u, ul, wbr
  • You may need to use entities for some characters, as follows. (Exception: Within code tags, you can put the characters literally.)
            For:     Use:
    & &amp;
    < &lt;
    > &gt;
    [ &#91;
    ] &#93;
  • Link using PerlMonks shortcuts! What shortcuts can I use for linking?
  • See Writeup Formatting Tips and other pages linked from there for more info.