Beefy Boxes and Bandwidth Generously Provided by pair Networks
Don't ask to ask, just ask

reading values from file

by perlNinny (Beadle)
on Jan 24, 2006 at 17:28 UTC ( #525250=perlquestion: print w/replies, xml ) Need Help??

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

I don't want to release this code into the wild until I get it elegant enough. I don't want sand kicked in my face by some perlMage! Below does work but needs cleaning:
my $debug = shift @ARGV; if ( $debug == 1) { print "debug mode\n"; } while (<INFILE>) { if (/ver =/) { $old_version = $_; $old_version =~ s/ver = '//; $old_version =~ s/'//; chomp($old_version ); close INFILE; } my $projlabl = $projectPath; $projlabl =~ s/\.pro//; $projlabl = $projlabl . "labeler";

2006-01-24 Retitled by GrandFather, as per Monastery guidelines
Original title: 'This can be more elegant'

Replies are listed 'Best First'.
Re: reading values from file
by Aristotle (Chancellor) on Jan 24, 2006 at 17:38 UTC

    The loop in the middle is malformed (it appears to be missing a closing brace). In addition, youíre terminating the loop by closing the filehandle. That actually works as intended, but itís kind of surprising. Further, youíre extracting a piece of information from the middle of a line by substituting the bits in front and after with nothing. Why not just match the bits you want?

    The last three lines are kind of a non-sequitur. Where did $projectPath come from? Lastly, you never do anything with the values you calculate. Whatís the point of the excercise? Is this a snippet thatís just part of a larger script? (Or maybe homework?)

    Makeshifts last the longest.

      Nope, not homework. Yes, it is snippets of a larger script...these are just some examples of what I got to work but know there is a better way. Yes, I did forget the closing brace but, as I said, its just a snippet. However, I wonder why its bad to close the infile to exit the loop? It does two things, closes the file and exits the loop.

        It works, but itís surprising. I had to blink and stare for a moment before I noticed that it actually does do what you intended. All in all, considering the other things I mentioned, Iíd write the loop like this:

        while ( <INFILE> ) { next if not /ver = '(.*?)'/; $old_version = $1; close INFILE; last; }

        Better yet, if you just opened the file:

        { open my $fh, '<', $filename or die "Can't open $filename: $!\n"; while ( <$fh> ) { next if not /ver = '(.*?)'/; $old_version = $1; last; } }

        In which case $fh goes out of scope at the end of the block and the file is closed automatically; much nicer. I only explicitly close files that I write to (because they you want to check the return value of close, which may fail for such reasons as ďdisk fullĒ or the like).

        In fact, if the file is so small that you donít mind slurping it, you could just say

        use List::Util qw( first ); my $old_version = do { open my $fh, '<', $filename or die "Can't open $filename: $!\n"; first { /ver = '(.*?)'/ } <$fh>; };

        Makeshifts last the longest.

Re: reading values from file
by thedoe (Monk) on Jan 24, 2006 at 17:36 UTC
    How about:
    my $debug = shift @ARGV; print "debug mode\n" if $debug; # where is INFILE opened? while (<INFILE>) { # assuming you want what is between the '' after version chomp($old_version) && last if ($old_version) = /ver = '(.*?)'/; } close INFILE; # assuming .pro is end of projectPath my ($projlabl) = $projectPath =~ /(.*)\.pro$/; $projlabl .= 'labeler';

      Why are you chomping a value that cannot contain any newlines? Not to mention that sticking that all on a single line is unnecessarily dense.

      Makeshifts last the longest.

        In case my assumption was incorrect and there could be a newline at the end of version, only the regex needs to be modified.

        Should have thrown that in a comment as well, thanks for pointing it out to the poster.

      Hmmm...tried your version below:
      while (<INFILE>) { # assuming you want what is between the '' after version chomp($old_version) && last if ($old_version) = /ver = '(.*?)'/; } close INFILE;
      and it didn't retrieve the string and put it in $old_version...or at least it didn't show it on a subsequent print statement.
Re: reading values from file
by smokemachine (Hermit) on Jan 24, 2006 at 17:51 UTC
    #!/usr/bin/perl -n BEGIN{ print "debug mode\n" if shift == 1} $old_version=$1 if /ver = '(.*)'/; END{($projlabl = $projectPath)=~s/\.pro/labeler/}
    updated: i'd forgotten about the shift.
Re: reading values from file
by kwaping (Priest) on Jan 24, 2006 at 20:52 UTC
    Here's my go at it, with a nod to smokemachine:
    print "debug mode\n" if shift; read INFILE, my $infile, -s '/path/to/infile'; #<- not specified in OP close(INFILE); my ($old_version) = $infile =~ /^.*?ver = '(.*?)'/; (my $projlabl = $projectPath) =~ s/\.pro/labeler/;

Log In?

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

How do I use this? | Other CB clients
Other Users?
Others romping around the Monastery: (3)
As of 2022-08-09 07:16 GMT
Find Nodes?
    Voting Booth?

    No recent polls found